llm

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseSSEResponse

func ParseSSEResponse(reader io.Reader, responseCh chan<- string) error

ParseSSEResponse 解析 SSE 响应流

func SetProxy

func SetProxy(proxyURL string) error

SetProxy 设置代理

Types

type ChatRequest

type ChatRequest struct {
	Model       string    `json:"model"`
	Messages    []Message `json:"messages"`
	Stream      bool      `json:"stream"`
	Temperature float64   `json:"temperature,omitempty"`
	Tools       []Tool    `json:"tools,omitempty"`
}

ChatRequest 聊天请求

type ChatResponse

type ChatResponse struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Created int64  `json:"created"`
	Model   string `json:"model"`
	Choices []struct {
		Index   int `json:"index"`
		Message struct {
			Role      string     `json:"role"`
			Content   string     `json:"content"`
			ToolCalls []ToolCall `json:"tool_calls,omitempty"`
		} `json:"message"`
		Delta struct {
			Role      string     `json:"role,omitempty"`
			Content   string     `json:"content,omitempty"`
			ToolCalls []ToolCall `json:"tool_calls,omitempty"`
		} `json:"delta"`
		FinishReason string `json:"finish_reason"`
	} `json:"choices"`
}

ChatResponse 聊天响应

type Client

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

Client LLM 客户端

func NewClient

func NewClient(config *Config, mcpServer MCPServer) *Client

NewClient 创建 LLM 客户端

func (*Client) Chat

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

Chat 与 LLM 对话 (非流式)

func (*Client) ChatStream

func (c *Client) ChatStream(ctx context.Context, messages []Message) (<-chan string, error)

ChatStream 与 LLM 流式对话

func (*Client) ChatWithMCPTools

func (c *Client) ChatWithMCPTools(ctx context.Context, userMessage string) (<-chan string, error)

ChatWithMCPTools 使用 MCP 工具与 LLM 对话

func (*Client) ChatWithToolsAndStream

func (c *Client) ChatWithToolsAndStream(ctx context.Context, userMessage string) (<-chan string, error)

ChatWithToolsAndStream 支持工具调用的流式对话(Client 方法)

func (*Client) ChatWithToolsAndStreamMessages added in v0.2.0

func (c *Client) ChatWithToolsAndStreamMessages(ctx context.Context, historyMessages []Message) (<-chan string, error)

ChatWithToolsAndStreamMessages 使用完整的消息历史与 LLM 对话

type Config

type Config struct {
	Model   string `mapstructure:"model"`
	APIKey  string `mapstructure:"api_key"`
	BaseURL string `mapstructure:"base_url"`
}

Config LLM 配置

type Function

type Function struct {
	Name        string         `json:"name"`
	Description string         `json:"description"`
	Parameters  map[string]any `json:"parameters"`
}

Function 函数定义

type LLMResponse

type LLMResponse struct {
	Content   string
	ToolCalls []ToolCall
}

LLMResponse LLM 响应结构

type MCPServer

type MCPServer interface {
	ListTools(ctx context.Context) (*mcp.ListToolsResult, error)
	ListEnabledTools(ctx context.Context) (*mcp.ListToolsResult, error)
	CallTool(ctx context.Context, name string, arguments map[string]any) (*mcp.CallToolResult, error)
}

MCPServer MCP服务器接口(避免循环导入)

type Message

type Message struct {
	Role       string     `json:"role"`
	Content    any        `json:"content"`
	ToolCalls  []ToolCall `json:"tool_calls,omitempty"`   // 用于 assistant 角色的工具调用
	ToolCallID string     `json:"tool_call_id,omitempty"` // 用于 tool 角色的响应
	Name       string     `json:"name,omitempty"`         // 用于 tool 角色的函数名
}

Message 消息结构

type OpenAIClient

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

OpenAIClient OpenAI 兼容的客户端

func NewOpenAIClient

func NewOpenAIClient(config *Config) *OpenAIClient

NewOpenAIClient 创建新的 OpenAI 客户端

func (*OpenAIClient) Chat

func (c *OpenAIClient) Chat(ctx context.Context, req *ChatRequest) (*ChatResponse, error)

Chat 普通对话(非流式)

func (*OpenAIClient) ChatStream

func (c *OpenAIClient) ChatStream(ctx context.Context, req *ChatRequest) (<-chan string, <-chan error, error)

ChatStream 流式对话

func (*OpenAIClient) ChatWithTools

func (c *OpenAIClient) ChatWithTools(ctx context.Context, messages []Message, tools []Tool) (*ChatResponse, error)

ChatWithTools 支持工具调用的对话(非流式)

type StreamResult

type StreamResult struct {
	Content   string
	ToolCalls []ToolCall
}

StreamResult 流式响应的累积结果

type Tool

type Tool struct {
	Type     string   `json:"type"`
	Function Function `json:"function"`
}

Tool 工具定义

type ToolCall

type ToolCall struct {
	ID       string `json:"id"`
	Type     string `json:"type"`
	Function struct {
		Name      string `json:"name"`
		Arguments string `json:"arguments"`
	} `json:"function"`
}

ToolCall 工具调用

Jump to

Keyboard shortcuts

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