anthropic

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnthropicModel

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

AnthropicModel Anthropic Messages API 模型

func NewAnthropicModel

func NewAnthropicModel(modelName, apiKey, baseURL string, httpClient *http.Client, noSystemRole bool) *AnthropicModel

NewAnthropicModel 创建 Anthropic 模型

func (*AnthropicModel) GenerateContent

func (m *AnthropicModel) GenerateContent(ctx context.Context, req *model.LLMRequest, stream bool) iter.Seq2[*model.LLMResponse, error]

GenerateContent 实现 model.LLM 接口

func (*AnthropicModel) Name

func (m *AnthropicModel) Name() string

Name 返回模型名称

type ContentBlock

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

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

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

	// tool_use
	ID    string          `json:"id,omitempty"`
	Name  string          `json:"name,omitempty"`
	Input json.RawMessage `json:"input,omitempty"`

	// tool_result
	ToolUseID  string          `json:"tool_use_id,omitempty"`
	RawContent json.RawMessage `json:"-"` // 自定义序列化,不走默认 tag
	IsError    bool            `json:"is_error,omitempty"`
}

ContentBlock 内容块(多态) 使用自定义 MarshalJSON 按 Type 输出不同字段,避免序列化冲突

func (ContentBlock) MarshalJSON

func (b ContentBlock) MarshalJSON() ([]byte, error)

MarshalJSON 按 Type 输出对应字段,避免多余字段导致 Anthropic 拒绝

type Delta

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

Delta 增量内容

type Message

type Message struct {
	Role    string         `json:"role"` // user / assistant
	Content []ContentBlock `json:"content"`
}

Message 消息

type MessageDelta

type MessageDelta struct {
	StopReason   string  `json:"stop_reason,omitempty"`
	StopSequence *string `json:"stop_sequence,omitempty"`
}

MessageDelta 消息级增量

type MessagesRequest

type MessagesRequest struct {
	Model         string    `json:"model"`
	Messages      []Message `json:"messages"`
	System        string    `json:"system,omitempty"`
	MaxTokens     int       `json:"max_tokens"`
	Temperature   *float64  `json:"temperature,omitempty"`
	TopP          *float64  `json:"top_p,omitempty"`
	Stream        bool      `json:"stream,omitempty"`
	Tools         []Tool    `json:"tools,omitempty"`
	StopSequences []string  `json:"stop_sequences,omitempty"`
}

Anthropic Messages API 请求

type MessagesResponse

type MessagesResponse struct {
	ID           string         `json:"id"`
	Type         string         `json:"type"` // message
	Role         string         `json:"role"` // assistant
	Content      []ContentBlock `json:"content"`
	Model        string         `json:"model"`
	StopReason   string         `json:"stop_reason"` // end_turn / max_tokens / tool_use
	StopSequence *string        `json:"stop_sequence"`
	Usage        Usage          `json:"usage"`
}

MessagesResponse 非流式响应

type SSEContentBlockDelta

type SSEContentBlockDelta struct {
	Type  string `json:"type"`
	Index int    `json:"index"`
	Delta Delta  `json:"delta"`
}

SSEContentBlockDelta content_block_delta 事件

type SSEContentBlockStart

type SSEContentBlockStart struct {
	Type         string       `json:"type"`
	Index        int          `json:"index"`
	ContentBlock ContentBlock `json:"content_block"`
}

SSEContentBlockStart content_block_start 事件

type SSEContentBlockStop

type SSEContentBlockStop struct {
	Type  string `json:"type"`
	Index int    `json:"index"`
}

SSEContentBlockStop content_block_stop 事件

type SSEError

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

SSEError error 事件

type SSEMessageDelta

type SSEMessageDelta struct {
	Type  string       `json:"type"`
	Delta MessageDelta `json:"delta"`
	Usage *Usage       `json:"usage,omitempty"`
}

SSEMessageDelta message_delta 事件

type SSEMessageStart

type SSEMessageStart struct {
	Type    string           `json:"type"`
	Message MessagesResponse `json:"message"`
}

SSEMessageStart message_start 事件

type Tool

type Tool struct {
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	InputSchema json.RawMessage `json:"input_schema"`
}

Tool 工具定义

type Usage

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

Usage token 用量

Jump to

Keyboard shortcuts

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