anthropic

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package anthropic implements Anthropic-compatible streaming model clients.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AssistantMessage

type AssistantMessage struct {
	ID     string
	Blocks []ContentBlock
	Usage  Usage
}

type Client

type Client struct {
	HTTP                  *http.Client
	BaseURL               string
	APIKey                string
	AuthToken             string
	RateLimit             RateLimitOptions
	Sleep                 func(context.Context, time.Duration) error
	Fallbacks             ProviderFallbackOptions
	ForceOpenAICompatible bool
}

func New

func New(baseURL, apiKey, authToken string) *Client

func NewWithOptions

func NewWithOptions(baseURL, apiKey, authToken string, options ClientOptions) *Client

func NewWithRateLimit

func NewWithRateLimit(baseURL, apiKey, authToken string, rateLimit RateLimitOptions) *Client

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, req Request, onText func(string)) (AssistantMessage, error)

type ClientOptions

type ClientOptions struct {
	RateLimit             RateLimitOptions
	RequestTimeout        time.Duration
	ConnectTimeout        time.Duration
	Fallbacks             ProviderFallbackOptions
	ForceOpenAICompatible bool
}

type ContentBlock

type ContentBlock struct {
	Type      string          `json:"type"`
	Text      string          `json:"text,omitempty"`
	Thinking  string          `json:"thinking,omitempty"`
	Data      string          `json:"data,omitempty"`
	Source    *ContentSource  `json:"source,omitempty"`
	Title     string          `json:"title,omitempty"`
	Signature string          `json:"signature,omitempty"`
	ID        string          `json:"id,omitempty"`
	Name      string          `json:"name,omitempty"`
	Input     json.RawMessage `json:"input,omitempty"`
	ToolUseID string          `json:"tool_use_id,omitempty"`
	Content   string          `json:"content,omitempty"`
	IsError   bool            `json:"is_error,omitempty"`
}

type ContentSource

type ContentSource struct {
	Type      string `json:"type"`
	MediaType string `json:"media_type,omitempty"`
	Data      string `json:"data,omitempty"`
	URL       string `json:"url,omitempty"`
}

type Message

type Message struct {
	ID      string         `json:"id,omitempty"`
	Role    string         `json:"role"`
	Content []ContentBlock `json:"content"`
}

func TextMessage

func TextMessage(role, text string) Message

func ToolResultMessage

func ToolResultMessage(id, content string, isError bool) Message

type MissingCredentialsError

type MissingCredentialsError struct {
	Provider string
	EnvVars  []string
	Hint     string
}

func (MissingCredentialsError) Error

func (e MissingCredentialsError) Error() string

type ProviderFallbackOptions

type ProviderFallbackOptions struct {
	Primary string   `json:"primary,omitempty"`
	Models  []string `json:"models,omitempty"`
}

type RateLimitOptions

type RateLimitOptions struct {
	MaxRetries     int           `json:"max_retries"`
	InitialBackoff time.Duration `json:"initial_backoff"`
	MaxBackoff     time.Duration `json:"max_backoff"`
}

func DefaultRateLimitOptions

func DefaultRateLimitOptions() RateLimitOptions

func (RateLimitOptions) Report

func (o RateLimitOptions) Report() RateLimitReport

type RateLimitReport

type RateLimitReport struct {
	MaxRetries        int   `json:"max_retries"`
	InitialBackoffMS  int   `json:"initial_backoff_ms"`
	MaxBackoffMS      int   `json:"max_backoff_ms"`
	RetryableStatuses []int `json:"retryable_statuses"`
}

type Request

type Request struct {
	Model           string           `json:"model"`
	MaxTokens       int              `json:"max_tokens"`
	Temperature     *float64         `json:"temperature,omitempty"`
	ReasoningEffort string           `json:"-"`
	ExtraBody       map[string]any   `json:"-"`
	System          string           `json:"system,omitempty"`
	Messages        []Message        `json:"messages"`
	Tools           []ToolDefinition `json:"tools,omitempty"`
	Stream          bool             `json:"stream"`
}

type ToolDefinition

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

type Usage

type Usage struct {
	InputTokens              int `json:"input_tokens,omitempty"`
	OutputTokens             int `json:"output_tokens,omitempty"`
	CacheCreationInputTokens int `json:"cache_creation_input_tokens,omitempty"`
	CacheReadInputTokens     int `json:"cache_read_input_tokens,omitempty"`
}

Jump to

Keyboard shortcuts

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