client

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatOptions

type ChatOptions struct {
	Model             string                                          // 模型名称
	Messages          []openai.ChatCompletionMessageParamUnion        // 消息列表
	Stream            bool                                            // 是否流式输出
	Temperature       *float64                                        // 采样温度 [0, 2)
	TopP              *float64                                        // 核采样概率 (0, 1.0]
	MaxTokens         *int64                                          // 最大输出token数
	PresencePenalty   *float64                                        // 内容重复度惩罚 [-2.0, 2.0]
	ResponseFormat    *openai.ChatCompletionNewParamsResponseFormat   // 响应格式
	Tools             []openai.ChatCompletionToolParam                // 工具列表
	ToolChoice        openai.ChatCompletionToolChoiceOptionUnionParam // 工具选择策略
	ParallelToolCalls *bool                                           // 是否并行工具调用
	EnableSearch      *bool                                           // 是否启用联网搜索
	Seed              *int64                                          // 随机种子
	Stop              []string                                        // 停止词
	N                 *int64                                          // 生成响应数量
}

ChatOptions 聊天选项

type ChatRequest

type ChatRequest struct {
	Model       string    `json:"model"`
	Messages    []Message `json:"messages"`
	Stream      bool      `json:"stream,omitempty"`
	Temperature *float64  `json:"temperature,omitempty"`
	TopP        *float64  `json:"top_p,omitempty"`
	MaxTokens   *int      `json:"max_tokens,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"`
		} `json:"message"`
		FinishReason string `json:"finish_reason"`
	} `json:"choices"`
	Usage struct {
		PromptTokens     int `json:"prompt_tokens"`
		CompletionTokens int `json:"completion_tokens"`
		TotalTokens      int `json:"total_tokens"`
	} `json:"usage"`
}

ChatResponse 聊天响应

type Client

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

Client 通义千问客户端

func NewClient

func NewClient(cfg *config.Config) *Client

NewClient 创建新的客户端

func (*Client) Chat

func (c *Client) Chat(ctx context.Context, opts ChatOptions) (*openai.ChatCompletion, error)

Chat 发送聊天请求

func (*Client) ChatStream

func (c *Client) ChatStream(ctx context.Context, opts ChatOptions, handler StreamHandler) error

ChatStream 流式聊天

func (*Client) ChatStreamToWriter

func (c *Client) ChatStreamToWriter(ctx context.Context, opts ChatOptions, writer io.Writer) error

ChatStreamToWriter 流式输出到 Writer

func (*Client) ChatWithSearch

func (c *Client) ChatWithSearch(ctx context.Context, message string, searchOpts SearchOptions) (*openai.ChatCompletion, error)

ChatWithSearch 带联网搜索的聊天

func (*Client) ChatWithSearchStream

func (c *Client) ChatWithSearchStream(ctx context.Context, message string, searchOpts SearchOptions, handler StreamHandler) error

ChatWithSearchStream 带联网搜索的流式聊天

func (*Client) ChatWithTools

func (c *Client) ChatWithTools(ctx context.Context, opts ChatOptions, toolManager *ToolManager, maxIterations int) (*openai.ChatCompletion, error)

ChatWithTools 带工具调用的聊天

func (*Client) GetClient

func (c *Client) GetClient() *openai.Client

GetClient 获取底层 OpenAI 客户端

func (*Client) SimpleChat

func (c *Client) SimpleChat(ctx context.Context, message string) (string, error)

SimpleChat 简单聊天接口

func (*Client) SimpleChatStream

func (c *Client) SimpleChatStream(ctx context.Context, message string, handler StreamHandler) error

SimpleChatStream 简单流式聊天

type HTTPClient

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

HTTPClient 基于原生HTTP的客户端实现 用于调用通义千问API的HTTP客户端

func NewHTTPClient

func NewHTTPClient(cfg *config.Config) *HTTPClient

NewHTTPClient 创建HTTP客户端 cfg: 配置对象,如果为nil则使用默认配置

func (*HTTPClient) Chat

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

Chat 发送聊天请求

func (*HTTPClient) ChatStream

func (c *HTTPClient) ChatStream(ctx context.Context, req ChatRequest, handler func(content string) error) error

ChatStream 流式聊天

func (*HTTPClient) SimpleChat

func (c *HTTPClient) SimpleChat(ctx context.Context, message string) (string, error)

SimpleChat 简单对话

func (*HTTPClient) SimpleChatStream

func (c *HTTPClient) SimpleChatStream(ctx context.Context, message string, handler func(content string) error) error

SimpleChatStream 简单流式对话

type Message

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

Message 消息结构

type SearchOptions

type SearchOptions struct {
	EnableSearch bool   // 是否启用联网搜索
	ForcedSearch bool   // 是否强制搜索
	SearchQuery  string // 搜索查询(可选)
}

SearchOptions 联网搜索选项

type StreamChunk

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

StreamChunk 流式响应块

type StreamHandler

type StreamHandler func(content string) error

StreamHandler 流式输出处理器

type Tool

type Tool struct {
	Name        string
	Description string
	Parameters  map[string]interface{}
	Function    ToolFunction
}

Tool 工具定义

func CreateCalculatorTool

func CreateCalculatorTool() *Tool

CreateCalculatorTool 创建计算器工具示例

func CreateWeatherTool

func CreateWeatherTool() *Tool

CreateWeatherTool 创建天气查询工具示例

type ToolFunction

type ToolFunction func(args string) (string, error)

ToolFunction 工具函数定义

type ToolManager

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

ToolManager 工具管理器

func NewToolManager

func NewToolManager() *ToolManager

NewToolManager 创建工具管理器

func (*ToolManager) ExecuteTool

func (tm *ToolManager) ExecuteTool(name string, args string) (string, error)

ExecuteTool 执行工具

func (*ToolManager) GetTool

func (tm *ToolManager) GetTool(name string) (*Tool, bool)

GetTool 获取工具

func (*ToolManager) GetToolParams

func (tm *ToolManager) GetToolParams() []openai.ChatCompletionToolParam

GetToolParams 获取工具参数列表

func (*ToolManager) RegisterTool

func (tm *ToolManager) RegisterTool(tool *Tool)

RegisterTool 注册工具

Jump to

Keyboard shortcuts

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