anthropic

package
v0.0.0-...-bde3139 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultBaseURL is the default base URL for the Anthropic API
	DefaultBaseURL = "https://api.anthropic.com/v1"

	// DefaultRPM is the default rate limit for requests per minute
	DefaultRPM = 100

	// DefaultTPM is the default rate limit for tokens per minute
	DefaultTPM = 100000

	// DefaultMaxRetries is the default number of retries for rate limited requests
	DefaultMaxRetries = 5

	// DefaultRetryAfter is the default time to wait before retrying a rate limited request
	DefaultRetryAfter = 1 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AnthropicContent

type AnthropicContent struct {
	Type  string                 `json:"type"`
	Text  string                 `json:"text,omitempty"`
	ID    string                 `json:"id,omitempty"`
	Name  string                 `json:"name,omitempty"`
	Input map[string]interface{} `json:"input,omitempty"`
}

AnthropicContent represents content in a message

type AnthropicDelta

type AnthropicDelta struct {
	Type         string `json:"type"`
	Text         string `json:"text,omitempty"`
	StopReason   string `json:"stop_reason,omitempty"`
	StopSequence string `json:"stop_sequence,omitempty"`
}

AnthropicDelta represents a delta in a streaming response

type AnthropicMessage

type AnthropicMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

AnthropicMessage represents a message in a conversation

type AnthropicMessageRequest

type AnthropicMessageRequest struct {
	Model         string             `json:"model"`
	Messages      []AnthropicMessage `json:"messages"`
	System        string             `json:"system,omitempty"`
	MaxTokens     int                `json:"max_tokens,omitempty"`
	Temperature   float64            `json:"temperature,omitempty"`
	TopP          float64            `json:"top_p,omitempty"`
	TopK          int                `json:"top_k,omitempty"`
	Stream        bool               `json:"stream,omitempty"`
	Tools         []AnthropicTool    `json:"tools,omitempty"`
	ToolChoice    interface{}        `json:"tool_choice,omitempty"`
	StopSequences []string           `json:"stop_sequences,omitempty"`
}

AnthropicMessageRequest represents a request to the messages API

type AnthropicMessageResponse

type AnthropicMessageResponse struct {
	ID           string                 `json:"id"`
	Type         string                 `json:"type"`
	Role         string                 `json:"role"`
	Content      []AnthropicContent     `json:"content"`
	Model        string                 `json:"model"`
	StopReason   string                 `json:"stop_reason"`
	StopSequence string                 `json:"stop_sequence"`
	Usage        AnthropicUsage         `json:"usage"`
	ToolUse      []AnthropicToolUse     `json:"tool_use,omitempty"`
	Error        map[string]interface{} `json:"error,omitempty"`
}

AnthropicMessageResponse represents a response from the messages API

type AnthropicStreamResponse

type AnthropicStreamResponse struct {
	Type         string                   `json:"type"`
	Message      AnthropicMessageResponse `json:"message,omitempty"`
	Index        int                      `json:"index,omitempty"`
	ContentBlock *AnthropicContent        `json:"content_block,omitempty"`
	Delta        *AnthropicDelta          `json:"delta,omitempty"`
	Error        map[string]interface{}   `json:"error,omitempty"`
}

AnthropicStreamResponse represents a streaming response from the messages API

type AnthropicTool

type AnthropicTool struct {
	Name        string                 `json:"name"`
	Description string                 `json:"description"`
	InputSchema map[string]interface{} `json:"input_schema"`
}

AnthropicTool represents a tool in Anthropic's API

type AnthropicToolUse

type AnthropicToolUse struct {
	ID        string                 `json:"id"`
	Name      string                 `json:"name"`
	Input     map[string]interface{} `json:"input"`
	Type      string                 `json:"type"`
	Submitted bool                   `json:"submitted"`
}

AnthropicToolUse represents a tool use in Anthropic's API

type AnthropicUsage

type AnthropicUsage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
}

AnthropicUsage represents token usage in a response

type ErrorResponse

type ErrorResponse struct {
	Error struct {
		Type    string `json:"type"`
		Message string `json:"message"`
	} `json:"error"`
}

ErrorResponse represents an error response from the API

type Model

type Model struct {
	// Configuration
	ModelName           string
	Provider            *Provider
	MaxHistoryMessages  int  // Maximum number of previous messages to include in request
	IncludeToolMessages bool // Whether to include tool result messages in the history limit
}

Model implements the model.Model interface for Anthropic

func (*Model) GetResponse

func (m *Model) GetResponse(ctx context.Context, request *model.Request) (*model.Response, error)

GetResponse gets a single response from the model with retry logic

func (*Model) StreamResponse

func (m *Model) StreamResponse(ctx context.Context, request *model.Request) (<-chan model.StreamEvent, error)

StreamResponse streams a response from the model with retry logic

type Provider

type Provider struct {
	// Configuration
	BaseURL    string
	APIKey     string
	HTTPClient *http.Client

	// Model configuration
	DefaultModel string

	// Message history configuration
	MaxHistoryMessages  int  // Maximum number of previous messages to include
	IncludeToolMessages bool // Whether to include tool result messages in the history limit

	// Rate limiting configuration
	RPM        int           // Requests per minute
	TPM        int           // Tokens per minute
	MaxRetries int           // Maximum number of retries
	RetryAfter time.Duration // Time to wait before retrying
	// contains filtered or unexported fields
}

Provider implements model.Provider for Anthropic

func NewAnthropicProvider

func NewAnthropicProvider(apiKey string) *Provider

NewAnthropicProvider creates a new Provider with default settings

func NewProvider

func NewProvider(apiKey string) *Provider

NewProvider creates a new provider with default settings

func (*Provider) GetModel

func (p *Provider) GetModel(name string) (model.Model, error)

GetModel returns a model by name

func (*Provider) SetBaseURL

func (p *Provider) SetBaseURL(baseURL string) *Provider

SetBaseURL sets the base URL for the provider

func (*Provider) SetDefaultModel

func (p *Provider) SetDefaultModel(modelName string) *Provider

SetDefaultModel sets the default model for the provider

func (*Provider) UpdateTokenCount

func (p *Provider) UpdateTokenCount(tokens int)

UpdateTokenCount updates the token count for rate limiting

func (*Provider) WaitForRateLimit

func (p *Provider) WaitForRateLimit()

WaitForRateLimit waits for the rate limiter to allow a new request

func (*Provider) WithAPIKey

func (p *Provider) WithAPIKey(apiKey string) *Provider

WithAPIKey sets the API key for the provider

func (*Provider) WithDefaultModel

func (p *Provider) WithDefaultModel(modelName string) *Provider

WithDefaultModel sets the default model for the provider

func (*Provider) WithHTTPClient

func (p *Provider) WithHTTPClient(client *http.Client) *Provider

WithHTTPClient sets the HTTP client for the provider

func (*Provider) WithMaxHistoryMessages

func (p *Provider) WithMaxHistoryMessages(maxMessages int) *Provider

WithMaxHistoryMessages sets the maximum number of previous messages to include in each request

func (*Provider) WithRateLimit

func (p *Provider) WithRateLimit(rpm, tpm int) *Provider

WithRateLimit sets the rate limit configuration for the provider

func (*Provider) WithRetryConfig

func (p *Provider) WithRetryConfig(maxRetries int, retryAfter time.Duration) *Provider

WithRetryConfig sets the retry configuration for the provider

func (*Provider) WithToolMessagesInHistory

func (p *Provider) WithToolMessagesInHistory(include bool) *Provider

WithToolMessagesInHistory configures whether tool messages count toward the history limit

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL