Documentation
¶
Index ¶
- Constants
- func ExampleAdvancedConfiguration()
- func ExampleBasicUsage()
- func ExampleChainedConfiguration()
- func ExampleConfigMigration()
- func ExampleEnvironmentBased()
- func ExampleProviderSpecific()
- func ExampleUseCaseOptimized()
- func ExampleWithPresets()
- func HasEmbedding(client Client) bool
- func HasStreaming(client Client) bool
- func HasTokenStreaming(client Client) bool
- func HasToolCalling(client Client) bool
- func NeedsEnhancedFeatures(opts *LLMOptions) bool
- func PrepareConfig(opts *LLMOptions) error
- type BatchEmbeddingClient
- type Capability
- type CapabilityChecker
- type Client
- type ClientFactory
- type ClientOption
- func WithAPIKey(apiKey string) ClientOption
- func WithBaseURL(baseURL string) ClientOption
- func WithCache(enabled bool, ttl time.Duration) ClientOption
- func WithCustomHeaders(headers map[string]string) ClientOption
- func WithMaxTokens(maxTokens int) ClientOption
- func WithModel(model string) ClientOption
- func WithOrganizationID(orgID string) ClientOption
- func WithPreset(preset PresetOption) ClientOption
- func WithProvider(provider constants.Provider) ClientOption
- func WithProviderPreset(provider constants.Provider) ClientOption
- func WithProxy(proxyURL string) ClientOption
- func WithRateLimiting(requestsPerMinute int) ClientOption
- func WithRetryCount(retryCount int) ClientOption
- func WithRetryDelay(delay time.Duration) ClientOption
- func WithStreamingEnabled(enabled bool) ClientOption
- func WithSystemPrompt(prompt string) ClientOption
- func WithTemperature(temperature float64) ClientOption
- func WithTimeout(timeout time.Duration) ClientOption
- func WithTopP(topP float64) ClientOption
- func WithUseCase(useCase UseCase) ClientOption
- type CompletionRequest
- type CompletionResponse
- type EmbeddingClient
- type FullClient
- type LLMOptions
- type Message
- type MockStreamClient
- func (m *MockStreamClient) Chat(ctx context.Context, messages []Message) (*CompletionResponse, error)
- func (m *MockStreamClient) ChatStream(ctx context.Context, messages []Message) (<-chan *StreamChunk, error)
- func (m *MockStreamClient) Complete(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)
- func (m *MockStreamClient) CompleteStream(ctx context.Context, req *CompletionRequest) (<-chan *StreamChunk, error)
- func (m *MockStreamClient) IsAvailable() bool
- func (m *MockStreamClient) Provider() constants.Provider
- type PresetOption
- type StreamChunk
- type StreamClient
- type StreamOptions
- type StreamReader
- type StreamWriter
- type TokenStreamingClient
- type ToolCall
- type ToolCallResponse
- type ToolCallingClient
- type ToolChunk
- type Usage
- type UseCase
Examples ¶
Constants ¶
const ( // DefaultTemperature is the default temperature value DefaultTemperature = 0.7 // DefaultMaxTokens is the default max tokens value DefaultMaxTokens = 2048 // DefaultTopP is the default top_p value DefaultTopP = 1.0 // DefaultFrequencyPenalty is the default frequency penalty DefaultFrequencyPenalty = 0.0 // DefaultPresencePenalty is the default presence penalty DefaultPresencePenalty = 0.0 )
Default Values
Variables ¶
This section is empty.
Functions ¶
func ExampleAdvancedConfiguration ¶
func ExampleAdvancedConfiguration()
ExampleAdvancedConfiguration 展示高级配置选项
func ExampleChainedConfiguration ¶
func ExampleChainedConfiguration()
ExampleChainedConfiguration 展示链式配置
func ExampleConfigMigration ¶
func ExampleConfigMigration()
ExampleConfigMigration 展示如何从旧的 Config 结构迁移到选项模式
func HasEmbedding ¶
HasEmbedding checks if a client supports embeddings.
func HasStreaming ¶
HasStreaming checks if a client supports streaming.
func HasTokenStreaming ¶
HasTokenStreaming checks if a client supports simple token streaming.
func HasToolCalling ¶
HasToolCalling checks if a client supports tool calling.
func NeedsEnhancedFeatures ¶
func NeedsEnhancedFeatures(opts *LLMOptions) bool
NeedsEnhancedFeatures 检查是否需要增强功能(导出给 providers 包使用)
Types ¶
type BatchEmbeddingClient ¶
type BatchEmbeddingClient interface {
EmbeddingClient
// EmbedBatch generates embeddings for multiple texts in a single request.
// This is more efficient than calling Embed in a loop.
//
// Parameters:
// - ctx: Context for cancellation and timeout
// - texts: The texts to embed
//
// Returns:
// - [][]float64: The embedding vectors, one per input text
// - error if batch embedding fails
EmbedBatch(ctx context.Context, texts []string) ([][]float64, error)
}
BatchEmbeddingClient extends EmbeddingClient with batch embedding support. This is more efficient than calling Embed multiple times.
type Capability ¶
type Capability string
Capability represents a provider capability that can be checked at runtime.
const ( // CapabilityCompletion indicates basic completion support (all providers) CapabilityCompletion Capability = "completion" // CapabilityChat indicates chat/conversation support (all providers) CapabilityChat Capability = "chat" // CapabilityStreaming indicates streaming response support CapabilityStreaming Capability = "streaming" // CapabilityToolCalling indicates function/tool calling support CapabilityToolCalling Capability = "tool_calling" // CapabilityEmbedding indicates text embedding support CapabilityEmbedding Capability = "embedding" // CapabilityBatchEmbedding indicates batch embedding support CapabilityBatchEmbedding Capability = "batch_embedding" // CapabilityVision indicates vision/image understanding support CapabilityVision Capability = "vision" // CapabilityJSON indicates JSON mode/structured output support CapabilityJSON Capability = "json_mode" )
Standard provider capabilities
type CapabilityChecker ¶
type CapabilityChecker interface {
// HasCapability checks if the provider supports the given capability.
HasCapability(cap Capability) bool
// Capabilities returns all supported capabilities.
Capabilities() []Capability
}
CapabilityChecker allows runtime capability discovery. Providers can implement this to advertise their capabilities.
Example usage:
if checker, ok := client.(CapabilityChecker); ok {
if checker.HasCapability(CapabilityToolCalling) {
// Use tool calling
}
}
type Client ¶
type Client interface {
// Complete 生成文本补全
Complete(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)
// Chat 进行对话
Chat(ctx context.Context, messages []Message) (*CompletionResponse, error)
// Provider 返回提供商类型
Provider() constants.Provider
// IsAvailable 检查 LLM 是否可用
IsAvailable() bool
}
Client 定义 LLM 客户端接口
func NewClientWithOptions ¶
func NewClientWithOptions(opts ...ClientOption) (Client, error)
NewClientWithOptions 使用选项模式创建 LLM 客户端 注意:实际的客户端创建需要在应用层或 providers 包中实现
Example ¶
ExampleNewClientWithOptions 展示如何使用 option 模式创建客户端
package main
import (
"context"
"github.com/kart-io/goagent/llm"
"github.com/kart-io/goagent/llm/constants"
)
func main() {
// 基本使用
client, err := llm.NewClientWithOptions(
llm.WithProvider(constants.ProviderOpenAI),
llm.WithAPIKey("your-api-key"),
llm.WithModel("gpt-4"),
llm.WithMaxTokens(2000),
llm.WithTemperature(0.7),
)
if err != nil {
panic(err)
}
ctx := context.Background()
response, err := client.Chat(ctx, []llm.Message{
llm.SystemMessage("You are a helpful assistant"),
llm.UserMessage("Hello!"),
})
if err != nil {
panic(err)
}
_ = response
}
Output:
Example (Preset) ¶
ExampleNewClientWithOptions_preset 展示使用预设配置
package main
import (
"time"
"github.com/kart-io/goagent/llm"
"github.com/kart-io/goagent/llm/constants"
)
func main() {
// 使用生产预设
client, err := llm.NewClientWithOptions(
llm.WithProvider(constants.ProviderOpenAI),
llm.WithAPIKey("your-api-key"),
llm.WithPreset(llm.PresetProduction),
llm.WithCache(true, 30*time.Minute),
)
if err != nil {
panic(err)
}
_ = client
}
Output:
Example (UseCase) ¶
ExampleNewClientWithOptions_useCase 展示针对使用场景优化
package main
import (
"context"
"github.com/kart-io/goagent/llm"
"github.com/kart-io/goagent/llm/constants"
)
func main() {
// 为代码生成优化
client, err := llm.NewClientWithOptions(
llm.WithProvider(constants.ProviderOpenAI),
llm.WithAPIKey("your-api-key"),
llm.WithUseCase(llm.UseCaseCodeGeneration),
llm.WithModel("gpt-4"), // 覆盖使用场景的默认模型
)
if err != nil {
panic(err)
}
ctx := context.Background()
response, err := client.Chat(ctx, []llm.Message{
llm.UserMessage("Write a function to sort an array in Go"),
})
if err != nil {
panic(err)
}
_ = response
}
Output:
type ClientFactory ¶
type ClientFactory interface {
CreateClient(opts *LLMOptions) (Client, error)
}
ClientFactory 用于创建 LLM 客户端的工厂接口
type ClientOption ¶
type ClientOption func(*LLMOptions)
ClientOption 定义 LLM 客户端配置选项
func WithCustomHeaders ¶
func WithCustomHeaders(headers map[string]string) ClientOption
WithCustomHeaders 设置自定义 HTTP 头
func WithOrganizationID ¶
func WithOrganizationID(orgID string) ClientOption
WithOrganizationID 设置组织 ID(用于 OpenAI)
func WithProvider ¶
func WithProvider(provider constants.Provider) ClientOption
WithProvider 设置 LLM 提供商
func WithProviderPreset ¶
func WithProviderPreset(provider constants.Provider) ClientOption
ProviderPreset 针对特定提供商的预设配置
func WithRateLimiting ¶
func WithRateLimiting(requestsPerMinute int) ClientOption
WithRateLimiting 配置速率限制
func WithStreamingEnabled ¶
func WithStreamingEnabled(enabled bool) ClientOption
WithStreamingEnabled 启用流式响应
func WithTemperature ¶
func WithTemperature(temperature float64) ClientOption
WithTemperature 设置温度参数
type CompletionRequest ¶
type CompletionRequest struct {
Messages []Message `json:"messages"` // 消息列表
Temperature float64 `json:"temperature,omitempty"` // 温度参数 (0.0-2.0)
MaxTokens int `json:"max_tokens,omitempty"` // 最大 token 数
Model string `json:"model,omitempty"` // 模型名称
Stop []string `json:"stop,omitempty"` // 停止序列
TopP float64 `json:"top_p,omitempty"` // Top-p 采样
}
CompletionRequest 定义补全请求
type CompletionResponse ¶
type CompletionResponse struct {
Content string `json:"content"` // 生成的内容
Model string `json:"model"` // 使用的模型
TokensUsed int `json:"tokens_used,omitempty"` // 使用的 token 数 (deprecated: use Usage instead)
FinishReason string `json:"finish_reason,omitempty"` // 结束原因
Provider string `json:"provider,omitempty"` // 提供商
Usage *interfaces.TokenUsage `json:"usage,omitempty"` // 详细的 Token 使用统计
}
CompletionResponse 定义补全响应
type EmbeddingClient ¶
type EmbeddingClient interface {
// Embed generates a vector embedding for the given text.
// The embedding can be used for semantic search, similarity, clustering, etc.
//
// Parameters:
// - ctx: Context for cancellation and timeout
// - text: The text to embed
//
// Returns:
// - []float64: The embedding vector
// - error if embedding generation fails
Embed(ctx context.Context, text string) ([]float64, error)
}
EmbeddingClient defines the interface for LLM providers that support text embeddings. Implement this interface in addition to Client to enable embedding generation.
Not all providers support embeddings - use CapabilityChecker.HasCapability("embedding") to check at runtime.
Implementations:
- providers.OpenAIProvider
- providers.DeepSeekProvider
func AsEmbedder ¶
func AsEmbedder(client Client) EmbeddingClient
AsEmbedder safely casts a client to EmbeddingClient. Returns nil if the client doesn't support embeddings.
type FullClient ¶
type FullClient interface {
Client
StreamClient
ToolCallingClient
EmbeddingClient
CapabilityChecker
}
FullClient combines all optional client interfaces. Use this for providers that support all capabilities.
type LLMOptions ¶
type LLMOptions struct {
// 基础配置
Provider constants.Provider `json:"provider"` // 提供商
APIKey string `json:"api_key"` // API 密钥
BaseURL string `json:"base_url,omitempty"` // 自定义 API 端点
Model string `json:"model"` // 默认模型
// 生成参数
MaxTokens int `json:"max_tokens"` // 默认最大 token 数
Temperature float64 `json:"temperature"` // 默认温度 (0.0-2.0)
TopP float64 `json:"top_p,omitempty"` // Top-P 采样参数 (0.0-1.0)
// 网络配置
Timeout int `json:"timeout"` // 请求超时(秒)
ProxyURL string `json:"proxy_url,omitempty"` // 代理 URL
// 重试配置
RetryCount int `json:"retry_count,omitempty"` // 重试次数
RetryDelay time.Duration `json:"retry_delay,omitempty"` // 重试延迟
// 速率限制
RateLimitRPM int `json:"rate_limit_rpm,omitempty"` // 每分钟请求数限制
// 缓存配置
CacheEnabled bool `json:"cache_enabled,omitempty"` // 是否启用缓存
CacheTTL time.Duration `json:"cache_ttl,omitempty"` // 缓存 TTL
// 流式响应
StreamingEnabled bool `json:"streaming_enabled,omitempty"` // 是否启用流式响应
// 其他配置
OrganizationID string `json:"organization_id,omitempty"` // 组织 ID (用于 OpenAI)
SystemPrompt string `json:"system_prompt,omitempty"` // 默认系统提示
CustomHeaders map[string]string `json:"custom_headers,omitempty"` // 自定义 HTTP 头
}
Config 定义 LLM 配置
func ApplyOptions ¶
func ApplyOptions(config *LLMOptions, opts ...ClientOption) *LLMOptions
ApplyOptions 应用选项到配置
func NewLLMOptionsWithOptions ¶
func NewLLMOptionsWithOptions(opts ...ClientOption) *LLMOptions
NewConfigWithOptions 使用选项创建新配置
type Message ¶
type Message struct {
Role string `json:"role"` // "system", "user", "assistant"
Content string `json:"content"` // 消息内容
Name string `json:"name,omitempty"` // 可选的消息名称
}
Message 定义聊天消息
type MockStreamClient ¶
type MockStreamClient struct {
// contains filtered or unexported fields
}
MockStreamClient 模拟流式客户端(用于测试)
func NewMockStreamClient ¶
func NewMockStreamClient() *MockStreamClient
NewMockStreamClient 创建模拟流式客户端
func (*MockStreamClient) Chat ¶
func (m *MockStreamClient) Chat(ctx context.Context, messages []Message) (*CompletionResponse, error)
Chat 进行对话
func (*MockStreamClient) ChatStream ¶
func (m *MockStreamClient) ChatStream(ctx context.Context, messages []Message) (<-chan *StreamChunk, error)
ChatStream 流式对话
func (*MockStreamClient) Complete ¶
func (m *MockStreamClient) Complete(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)
Complete 生成文本补全
func (*MockStreamClient) CompleteStream ¶
func (m *MockStreamClient) CompleteStream(ctx context.Context, req *CompletionRequest) (<-chan *StreamChunk, error)
CompleteStream 流式补全
func (*MockStreamClient) IsAvailable ¶
func (m *MockStreamClient) IsAvailable() bool
IsAvailable 检查 LLM 是否可用
func (*MockStreamClient) Provider ¶
func (m *MockStreamClient) Provider() constants.Provider
Provider 返回提供商类型
type PresetOption ¶
type PresetOption int
PresetOption 预设配置选项类型
const ( // PresetDevelopment 开发环境预设 PresetDevelopment PresetOption = iota // PresetProduction 生产环境预设 PresetProduction // PresetLowCost 低成本预设 PresetLowCost // PresetHighQuality 高质量预设 PresetHighQuality // PresetFast 快速响应预设 PresetFast )
type StreamChunk ¶
type StreamChunk struct {
// Content 完整内容(累积)
Content string `json:"content"`
// Delta 增量内容
Delta string `json:"delta"`
// Role 角色
Role string `json:"role,omitempty"`
// FinishReason 结束原因
FinishReason string `json:"finish_reason,omitempty"`
// Usage 使用情况(仅在最后一个块)
Usage *Usage `json:"usage,omitempty"`
// Index 块序号
Index int `json:"index"`
// Timestamp 时间戳
Timestamp time.Time `json:"timestamp"`
// Done 是否完成
Done bool `json:"done"`
// Error 错误信息
Error error `json:"error,omitempty"`
}
StreamChunk LLM 流式响应块
type StreamClient ¶
type StreamClient interface {
Client // 继承基础接口
// CompleteStream 流式补全
CompleteStream(ctx context.Context, req *CompletionRequest) (<-chan *StreamChunk, error)
// ChatStream 流式对话
ChatStream(ctx context.Context, messages []Message) (<-chan *StreamChunk, error)
}
StreamClient 流式 LLM 客户端接口
扩展 Client 接口,提供流式补全能力
func AsStreamClient ¶
func AsStreamClient(client Client) StreamClient
AsStreamClient safely casts a client to StreamClient. Returns nil if the client doesn't support streaming.
type StreamOptions ¶
type StreamOptions struct {
// BufferSize 缓冲区大小
BufferSize int
// IncludeUsage 是否包含使用情况
IncludeUsage bool
// OnChunk 块处理回调
OnChunk func(*StreamChunk) error
// OnComplete 完成回调
OnComplete func(string) error
// OnError 错误回调
OnError func(error) error
}
StreamOptions 流式选项
func DefaultStreamOptions ¶
func DefaultStreamOptions() *StreamOptions
DefaultStreamOptions 返回默认流式选项
type StreamReader ¶
type StreamReader struct {
// contains filtered or unexported fields
}
StreamReader 流式读取器
提供便捷的流式读取方法
func NewStreamReader ¶
func NewStreamReader(stream <-chan *StreamChunk) *StreamReader
NewStreamReader 创建流式读取器
func (*StreamReader) CollectDeltas ¶
func (r *StreamReader) CollectDeltas(ctx context.Context) ([]string, error)
CollectDeltas 收集所有增量
func (*StreamReader) ReadAll ¶
func (r *StreamReader) ReadAll(ctx context.Context) (string, error)
ReadAll 读取所有内容
func (*StreamReader) ReadChunks ¶
func (r *StreamReader) ReadChunks(ctx context.Context, handler func(*StreamChunk) error) error
ReadChunks 逐块读取
type StreamWriter ¶
type StreamWriter struct {
// contains filtered or unexported fields
}
StreamWriter 流式写入器
将标准输出转换为流式块
func NewStreamWriter ¶
func NewStreamWriter(out chan<- *StreamChunk) *StreamWriter
NewStreamWriter 创建流式写入器
func (*StreamWriter) WriteChunk ¶
func (w *StreamWriter) WriteChunk(chunk *StreamChunk) error
WriteChunk 写入完整块
type TokenStreamingClient ¶
type TokenStreamingClient interface {
// Stream generates tokens for the given prompt and streams them as strings.
// This is simpler than CompleteStream but provides less metadata.
//
// Parameters:
// - ctx: Context for cancellation and timeout
// - prompt: The input prompt
//
// Returns:
// - channel of string tokens
// - error if stream setup fails
Stream(ctx context.Context, prompt string) (<-chan string, error)
}
TokenStreamingClient defines the interface for simple token-based streaming. This is a simpler alternative to StreamClient's chunk-based streaming.
Implementations:
- providers.DeepSeekProvider
- providers.CohereProvider
- providers.HuggingFaceProvider
- providers.AnthropicProvider
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"` // "function"
Function struct {
Name string `json:"name"`
Arguments string `json:"arguments"` // JSON string
} `json:"function"`
}
ToolCall represents a function/tool call by the LLM
type ToolCallResponse ¶
type ToolCallResponse struct {
Content string `json:"content"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
Usage *interfaces.TokenUsage `json:"usage,omitempty"`
}
ToolCallResponse represents the response from tool-enabled completion
type ToolCallingClient ¶
type ToolCallingClient interface {
// GenerateWithTools sends a prompt with available tools and returns the response.
// The LLM may respond with content, tool calls, or both.
//
// Parameters:
// - ctx: Context for cancellation and timeout
// - prompt: The user's input prompt
// - tools: Available tools that the LLM can call
//
// Returns:
// - ToolCallResponse containing content and/or tool calls
// - error if the request fails
GenerateWithTools(ctx context.Context, prompt string, tools []interfaces.Tool) (*ToolCallResponse, error)
// StreamWithTools is like GenerateWithTools but streams the response.
// Tool calls are streamed as ToolChunk with type "tool_call", "tool_name", "tool_args".
//
// Parameters:
// - ctx: Context for cancellation and timeout
// - prompt: The user's input prompt
// - tools: Available tools that the LLM can call
//
// Returns:
// - channel of ToolChunk for streaming response
// - error if stream setup fails
StreamWithTools(ctx context.Context, prompt string, tools []interfaces.Tool) (<-chan ToolChunk, error)
}
ToolCallingClient defines the interface for LLM providers that support tool/function calling. Implement this interface in addition to Client to enable tool-based interactions.
Not all providers support tool calling - use CapabilityChecker.HasCapability("tool_calling") to check at runtime.
Implementations:
- providers.OpenAIProvider
- providers.DeepSeekProvider
- providers.GeminiProvider
func AsToolCaller ¶
func AsToolCaller(client Client) ToolCallingClient
AsToolCaller safely casts a client to ToolCallingClient. Returns nil if the client doesn't support tool calling.
type ToolChunk ¶
type ToolChunk struct {
Type string `json:"type"` // "content", "tool_call", "tool_name", "tool_args", "error"
Value interface{} `json:"value"` // Content string, ToolCall, or error
}
ToolChunk represents a streaming chunk from tool-enabled completion
type Usage ¶
type Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
Usage 使用情况