llm

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

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

type ChatResult struct {
	Content   string
	ToolCalls []ToolCall
	Usage     *Usage
}

ChatResult 聊天结果

func (*ChatResult) HasToolCalls

func (r *ChatResult) HasToolCalls() bool

HasToolCalls 判断是否有工具调用

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 客户端接口

func NewClient

func NewClient(opts ...Option) (Client, error)

NewClient 创建新的 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 HTTPDoer

type HTTPDoer interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPDoer HTTP 请求接口

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 Option

type Option func(*config)

Option 函数式选项

func WithAPIKey

func WithAPIKey(apiKey string) Option

WithAPIKey 设置 API Key

func WithBaseURL

func WithBaseURL(baseURL string) Option

WithBaseURL 设置 base URL

func WithHTTPClient

func WithHTTPClient(client HTTPDoer) Option

WithHTTPClient 设置自定义 HTTP Client

func WithHeaders

func WithHeaders(headers map[string]string) Option

WithHeaders 设置自定义头

func WithMaxTokens

func WithMaxTokens(maxTokens int) Option

WithMaxTokens 设置最大 token 数

func WithMessages

func WithMessages(messages []Message) Option

WithMessages 设置消息列表(用于 Chat 方法)

func WithModel

func WithModel(model string) Option

WithModel 设置模型

func WithProvider

func WithProvider(provider string) Option

WithProvider 设置 provider

func WithStream

func WithStream(stream bool) Option

WithStream 设置是否流式

func WithTemperature

func WithTemperature(temperature float64) Option

WithTemperature 设置温度

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout 设置超时

func WithTools

func WithTools(tools []ToolDefinition) Option

WithTools 设置工具定义

type Response

type Response struct {
	Content          string
	ToolCalls        []ToolCall
	Usage            Usage
	CompletionTokens int
}

Response 完整响应

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 工具定义

type ToolLogs

type ToolLogs []ToolDefinition

ToolLogs 是 []ToolDefinition 的自定义类型,用于日志输出

func (ToolLogs) String

func (z ToolLogs) String() string

String 返回工具调用的函数名列表,用于日志记录

type Tools

type Tools []ToolDefinition

Tools 工具定义列表

func (Tools) Names

func (z Tools) Names() []string

Names 返回工具列表中的函数名

type Usage

type Usage struct {
	InputTokens  int
	OutputTokens int
	TotalTokens  int
}

Usage token 使用统计

Jump to

Keyboard shortcuts

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