anthropic

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIMessage

type APIMessage struct {
	Role    string         `json:"role"`    // "user" 或 "assistant"
	Content []ContentBlock `json:"content"` // 内容块数组
}

APIMessage Anthropic API 消息格式

type APIRequest

type APIRequest struct {
	Model       string       `json:"model"`
	Messages    []APIMessage `json:"messages"`
	MaxTokens   int          `json:"max_tokens"`
	System      string       `json:"system,omitempty"` // 顶层系统提示
	Temperature float64      `json:"temperature,omitempty"`
	TopP        float64      `json:"top_p,omitempty"`
	TopK        int          `json:"top_k,omitempty"`
	Stream      bool         `json:"stream,omitempty"`
	Tools       []APITool    `json:"tools,omitempty"`
	Thinking    *Thinking    `json:"thinking,omitempty"`
}

APIRequest Anthropic API 请求结构

type APIResponse

type APIResponse struct {
	ID         string         `json:"id"`
	Type       string         `json:"type"`
	Role       string         `json:"role"`
	Content    []ContentBlock `json:"content"`
	Model      string         `json:"model"`
	StopReason string         `json:"stop_reason"`
	Usage      Usage          `json:"usage"`
}

APIResponse Anthropic API 非流式响应

type APITool

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

APITool Anthropic 工具定义

type ChatModel

type ChatModel struct {
	// contains filtered or unexported fields
}

ChatModel Anthropic 聊天模型(实现 chatmodel.BaseModel)

func NewChatModel

func NewChatModel(config *ChatModelConfig) (*ChatModel, error)

NewChatModel 创建 Anthropic 聊天模型

func (*ChatModel) Generate

func (m *ChatModel) Generate(ctx context.Context, input []*schema.Message) (*schema.Message, error)

Generate 非流式生成

func (*ChatModel) GetModelName

func (m *ChatModel) GetModelName() string

GetModelName 返回模型名称

func (*ChatModel) Stream

func (m *ChatModel) Stream(ctx context.Context, input []*schema.Message) (*schema.StreamReader, error)

Stream 流式生成

type ChatModelConfig

type ChatModelConfig struct {
	BaseURL string
	APIKey  string
	Model   string

	// 模型参数
	MaxTokens   int     `json:"max_tokens"`
	Temperature float64 `json:"temperature,omitempty"`
	TopP        float64 `json:"top_p,omitempty"`
	TopK        int     `json:"top_k,omitempty"`

	// 工具
	Tools []schema.Tool `json:"-"`

	// Thinking(扩展思考)
	Thinking *Thinking `json:"thinking,omitempty"`

	// 流式
	Stream bool `json:"stream,omitempty"`

	// HTTP
	TimeOut    time.Duration
	HTTPClient *http.Client `json:"-"`
}

ChatModelConfig Anthropic 模型配置

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client Anthropic API 客户端

func NewClient

func NewClient(config *ChatModelConfig) *Client

NewClient 创建 Anthropic 客户端

func (*Client) Generate

func (c *Client) Generate(ctx context.Context, messages []*schema.Message) (*schema.Message, error)

Generate 非流式生成

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, messages []*schema.Message) (*schema.StreamReader, error)

Stream 流式生成

type ContentBlock

type ContentBlock struct {
	Type string `json:"type"` // "text", "tool_use", "tool_result", "thinking", "image", "document", "audio", "video"

	// type=text
	Text string `json:"text,omitempty"`

	// type=tool_use
	ID    string         `json:"id,omitempty"`
	Name  string         `json:"name,omitempty"`
	Input map[string]any `json:"input,omitempty"`

	// type=tool_result
	ToolUseID string         `json:"tool_use_id,omitempty"`
	Content   string         `json:"content,omitempty"` // tool_result 的文本内容
	IsError   bool           `json:"is_error,omitempty"`
	CntBlocks []ContentBlock `json:"content_blocks,omitempty"` // tool_result 的多模态内容

	// type=thinking
	Thinking string `json:"thinking,omitempty"`

	// type=image, document, audio, video
	Source *ContentSource `json:"source,omitempty"`
}

ContentBlock 内容块(多态结构)

type ContentSource added in v1.1.0

type ContentSource struct {
	Type      string `json:"type"`                 // "base64" 或 "url"
	MediaType string `json:"media_type,omitempty"` // "image/png", "application/pdf" 等
	Data      string `json:"data,omitempty"`       // base64 数据(type=base64 时)
	URL       string `json:"url,omitempty"`        // URL(type=url 时)
}

ContentSource Anthropic 多模态内容源

type StreamDelta

type StreamDelta struct {
	Type        string `json:"type"` // "text_delta", "thinking_delta", "input_json_delta"
	Text        string `json:"text,omitempty"`
	Thinking    string `json:"thinking,omitempty"`
	PartialJSON string `json:"partial_json,omitempty"`
}

StreamDelta 流式增量

type StreamEvent

type StreamEvent struct {
	Type         string        `json:"type"`
	ContentBlock *ContentBlock `json:"content_block,omitempty"`
	Delta        *StreamDelta  `json:"delta,omitempty"`
	Usage        *StreamUsage  `json:"usage,omitempty"`
}

StreamEvent 流式事件

type StreamUsage

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

StreamUsage 流式 Usage

type Thinking

type Thinking struct {
	Type         ThinkingType `json:"type"`                    // "enabled"
	BudgetTokens int          `json:"budget_tokens,omitempty"` // 思考预算
}

Thinking Anthropic 扩展思考配置

type ThinkingType

type ThinkingType string

ThinkingType 思考能力开关

const (
	Enabled  ThinkingType = "enabled"
	Disabled ThinkingType = "disabled"
)

type Usage

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

Usage Token 使用情况

Jump to

Keyboard shortcuts

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