Documentation
¶
Index ¶
- Constants
- Variables
- type ChatResult
- type Client
- type FinishReason
- type FunctionDefinition
- type HTTPDoer
- type Message
- type MessagesLogged
- type Option
- func WithAPIKey(apiKey string) Option
- func WithBaseURL(baseURL string) Option
- func WithHTTPClient(client HTTPDoer) Option
- func WithHeaders(headers map[string]string) Option
- func WithMaxTokens(maxTokens int) Option
- func WithMessages(messages []Message) Option
- func WithModel(model string) Option
- func WithProvider(provider string) Option
- func WithStream(stream bool) Option
- func WithTemperature(temperature float64) Option
- func WithTimeout(timeout time.Duration) Option
- func WithTools(tools []ToolDefinition) Option
- type Response
- type StreamOptions
- type StreamResult
- type ToolCall
- type ToolCallFunc
- type ToolDefinition
- type ToolLogs
- type Tools
- type Usage
Constants ¶
View Source
const ( RoleSystem = "system" RoleUser = "user" RoleAssistant = "assistant" RoleTool = "tool" )
MessageRole 消息角色常量
Variables ¶
View Source
var ErrUnsupportedProvider = errors.New("unsupported provider")
ErrUnsupportedProvider 不支持的 provider
Functions ¶
This section is empty.
Types ¶
type ChatResult ¶
ChatResult 聊天结果
type Client ¶
type Client interface {
// Chat 发送聊天请求,返回完整响应
Chat(ctx context.Context, messages []Message, tools []ToolDefinition) (*ChatResult, error)
// StreamChat 发送流式聊天请求,返回流式响应
StreamChat(ctx context.Context, messages []Message, tools []ToolDefinition) (<-chan StreamResult, error)
// Generate 简单文本生成(用于关键词提取等)
Generate(ctx context.Context, prompt string) (string, *Usage, error)
// Embedding 向量化文本
Embedding(ctx context.Context, texts []string) ([]float64, error)
}
Client LLM 客户端接口
type FinishReason ¶
type FinishReason string
FinishReason 聊天完成原因
const ( FinishReasonStop FinishReason = "stop" FinishReasonLength FinishReason = "length" FinishReasonFunctionCall FinishReason = "function_call" FinishReasonToolCalls FinishReason = "tool_calls" FinishReasonContentFilter FinishReason = "content_filter" FinishReasonNull FinishReason = "null" )
type FunctionDefinition ¶
type FunctionDefinition struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters any `json:"parameters,omitempty"`
}
FunctionDefinition 函数定义
type Message ¶
type Message struct {
Role string `json:"role"`
Content string `json:"content"` // 不可省略
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"` // only for role=tool
}
Message 表示聊天消息
type MessagesLogged ¶
type MessagesLogged []Message
MessagesLogged 是 []Message 的自定义类型,用于日志输出
func (MessagesLogged) String ¶
func (z MessagesLogged) String() string
String 返回每条消息的前30个字的文本,用于日志记录
type StreamOptions ¶ added in v0.2.4
type StreamOptions struct {
IncludeUsage bool `json:"include_usage,omitempty"`
}
type StreamResult ¶
type StreamResult struct {
Delta string
Think string
ToolCalls []ToolCall
Done bool `json:",omitempty"`
FinishReason FinishReason
Error error `json:",omitempty"`
Model string
ResponseID string
Usage *Usage
}
StreamResult 流式响应结果
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function ToolCallFunc `json:"function"`
}
ToolCall 表示工具调用请求
type ToolCallFunc ¶
type ToolCallFunc struct {
Name string `json:"name"`
Arguments json.RawMessage `json:"arguments"`
Results any `json:"results,omitempty"`
}
ToolCallFunc 工具调用函数
func (ToolCallFunc) MarshalJSON ¶
func (f ToolCallFunc) MarshalJSON() ([]byte, error)
MarshalJSON 自定义序列化,将 Arguments 转为字符串(OpenAI API 要求)
type ToolDefinition ¶
type ToolDefinition struct {
Type string `json:"type"`
Function FunctionDefinition `json:"function"`
}
ToolDefinition 工具定义
Click to show internal directories.
Click to hide internal directories.