feat: 昔涟工具扩展 — OpenAI Function Calling 集成 (网络搜索/网页抓取/IoT设备查询)

This commit is contained in:
2026-05-16 23:12:39 +08:00
parent 7f2961e63e
commit 1f5c2508d6
9 changed files with 1081 additions and 26 deletions
+29
View File
@@ -13,6 +13,19 @@ type Adapter struct {
provider LLMProvider
}
// OpenAITool 暴露给调用方使用的工具定义(与 openai.go 的 openAITool 等价)
type OpenAITool struct {
Type string `json:"type"`
Function OpenAIToolFunc `json:"function"`
}
// OpenAIToolFunc 工具函数定义
type OpenAIToolFunc struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]interface{} `json:"parameters"`
}
// LLMProvider LLM提供商接口
type LLMProvider interface {
// Chat 同步对话
@@ -21,6 +34,12 @@ type LLMProvider interface {
// ChatStream 流式对话,返回一个channel逐token推送
ChatStream(ctx context.Context, messages []model.LLMMessage) (<-chan StreamChunk, error)
// ChatWithTools 同步对话(支持工具调用),tools 为 nil 时等价于 Chat
ChatWithTools(ctx context.Context, messages []model.LLMMessage, tools []OpenAITool) (*model.LLMResponse, error)
// ChatStreamWithTools 流式对话(支持工具调用),tools 为 nil 时等价于 ChatStream
ChatStreamWithTools(ctx context.Context, messages []model.LLMMessage, tools []OpenAITool) (<-chan StreamChunk, error)
// ModelName 返回当前使用的模型名称
ModelName() string
}
@@ -43,11 +62,21 @@ func (a *Adapter) Chat(ctx context.Context, messages []model.LLMMessage) (*model
return a.provider.Chat(ctx, messages)
}
// ChatWithTools 同步对话(支持工具调用)
func (a *Adapter) ChatWithTools(ctx context.Context, messages []model.LLMMessage, tools []OpenAITool) (*model.LLMResponse, error) {
return a.provider.ChatWithTools(ctx, messages, tools)
}
// ChatStream 流式对话
func (a *Adapter) ChatStream(ctx context.Context, messages []model.LLMMessage) (<-chan StreamChunk, error) {
return a.provider.ChatStream(ctx, messages)
}
// ChatStreamWithTools 流式对话(支持工具调用)
func (a *Adapter) ChatStreamWithTools(ctx context.Context, messages []model.LLMMessage, tools []OpenAITool) (<-chan StreamChunk, error) {
return a.provider.ChatStreamWithTools(ctx, messages, tools)
}
// ModelName 返回模型名称
func (a *Adapter) ModelName() string {
return a.provider.ModelName()