Documentation
¶
Overview ¶
Package anthropic provides a provider implementation for Anthropic's Claude API.
Anthropic uses a different API format than OpenAI, so this package implements custom parsing, enrichment, and extraction logic.
Key differences from OpenAI:
- Endpoint: /v1/messages
- Auth: x-api-key header (not Bearer token)
- Required header: anthropic-version
- System prompt is a separate field (not a message with role "system")
- Response uses content array instead of choices
- Token usage fields: input_tokens/output_tokens
Basic usage:
provider, _ := anthropic.New("sk-ant-your-key")
proxy := llmproxy.NewProxy(provider)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CacheCreationInfo ¶ added in v0.0.3
type CacheCreationInfo struct {
Ephemeral5mInputTokens int `json:"ephemeral_5m_input_tokens,omitempty"`
Ephemeral1hInputTokens int `json:"ephemeral_1h_input_tokens,omitempty"`
}
CacheCreationInfo tracks cache creation token breakdown.
type Content ¶
type Content struct {
Text string `json:"-"`
Parts []ContentPart `json:"-"`
}
Content can be either a string or an array of content blocks.
func (*Content) UnmarshalJSON ¶
UnmarshalJSON handles Anthropic's flexible content format (string or array).
type ContentBlock ¶
ContentBlock represents a content block in an Anthropic response.
type ContentPart ¶
ContentPart represents a single content block.
type Enricher ¶
type Enricher struct {
// APIKey is the Anthropic API key.
APIKey string
// Version is the Anthropic API version (defaults to 2023-06-01).
Version string
}
Enricher implements llmproxy.RequestEnricher for Anthropic's API. It sets the required x-api-key and anthropic-version headers.
func NewEnricher ¶
NewEnricher creates a new Anthropic enricher with the given API key. The API version defaults to 2023-06-01 if not specified.
func NewEnricherWithVersion ¶
NewEnricherWithVersion creates a new Anthropic enricher with a specific API version.
type Extractor ¶
type Extractor struct{}
Extractor implements llmproxy.ResponseExtractor for Anthropic responses.
func NewExtractor ¶
func NewExtractor() *Extractor
NewExtractor creates a new Anthropic response extractor.
type Parser ¶
type Parser struct{}
Parser implements llmproxy.BodyParser for Anthropic's request format.
func (*Parser) Parse ¶
func (p *Parser) Parse(body io.ReadCloser) (llmproxy.BodyMetadata, []byte, error)
Parse reads an Anthropic request body and extracts metadata. It handles Anthropic-specific fields like the system prompt.
type Provider ¶
type Provider struct {
*llmproxy.BaseProvider
}
Provider is an Anthropic provider implementation.
func New ¶
New creates a new Anthropic provider with the given API key. The provider is configured to use Anthropic's API endpoint (https://api.anthropic.com).
Example:
provider, _ := anthropic.New("sk-ant-your-api-key")
func NewWithVersion ¶
NewWithVersion creates a new Anthropic provider with a specific API version.
Example:
provider, _ := anthropic.NewWithVersion("sk-ant-your-api-key", "2024-01-01")
type Request ¶
type Request struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
MaxTokens int `json:"max_tokens,omitempty"`
System string `json:"system,omitempty"`
Custom map[string]interface{} `json:"-"`
}
Request represents an Anthropic messages API request.
func (*Request) UnmarshalJSON ¶
UnmarshalJSON captures unknown fields into Custom.
type Resolver ¶
Resolver implements llmproxy.URLResolver for Anthropic's API. It constructs the messages endpoint URL.
func NewResolver ¶
NewResolver creates a new resolver with the given base URL.
type Response ¶
type Response struct {
ID string `json:"id"`
Type string `json:"type"`
Role string `json:"role"`
Model string `json:"model"`
Content []ContentBlock `json:"content"`
StopReason string `json:"stop_reason"`
StopSequence string `json:"stop_sequence,omitempty"`
Usage UsageInfo `json:"usage"`
CacheCreation *CacheCreationInfo `json:"cache_creation,omitempty"`
}
Response represents an Anthropic messages API response.