18c9354095
- 注入数据库、运行时配置、统一响应、文件存储和平台 AI 能力,补充业务读写工具与客户快捷操作契约。 - 移除模块内重复的组织、客户、工单、标签、技能、旧工作流、MCP 和迁移实现,将身份权限与业务主体交由宿主管理。 - 使用 libSQL 重构向量存储,并完善图片消息、访客身份、排队调度、企业微信和支持聊天页面。 - 统一 HTTP、DTO 与 WebSocket 的 snake_case 协议,补齐模块初始化、业务动作和公共载荷等回归测试。
110 lines
3.5 KiB
Go
110 lines
3.5 KiB
Go
package ai
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"github.com/mlogclub/simple/common/strs"
|
|
openai "github.com/openai/openai-go/v3"
|
|
"github.com/openai/openai-go/v3/shared"
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/models"
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/enums"
|
|
)
|
|
|
|
type ChatCompletionResult struct {
|
|
Content string
|
|
ModelName string
|
|
PromptTokens int
|
|
CompletionTokens int
|
|
}
|
|
|
|
type llm struct{}
|
|
|
|
var LLM = &llm{}
|
|
|
|
func (s *llm) Chat(ctx context.Context, systemPrompt string, userPrompt string) (*ChatCompletionResult, error) {
|
|
config, err := resolveDefaultAIConfig(ctx, enums.AIModelTypeLLM)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s.ChatWithConfig(ctx, *config, systemPrompt, userPrompt)
|
|
}
|
|
|
|
func (s *llm) ChatWithConfig(ctx context.Context, config models.AIConfig, systemPrompt string, userPrompt string) (*ChatCompletionResult, error) {
|
|
ctx = ensurePlatformAIRequestScope(ctx)
|
|
messages := make([]openai.ChatCompletionMessageParamUnion, 0, 2)
|
|
if strs.IsNotBlank(systemPrompt) {
|
|
messages = append(messages, openai.ChatCompletionMessageParamUnion{
|
|
OfSystem: &openai.ChatCompletionSystemMessageParam{
|
|
Content: openai.ChatCompletionSystemMessageParamContentUnion{
|
|
OfString: openai.String(systemPrompt),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
messages = append(messages, openai.ChatCompletionMessageParamUnion{
|
|
OfUser: &openai.ChatCompletionUserMessageParam{
|
|
Content: openai.ChatCompletionUserMessageParamContentUnion{
|
|
OfString: openai.String(userPrompt),
|
|
},
|
|
},
|
|
})
|
|
|
|
params := openai.ChatCompletionNewParams{
|
|
Messages: messages,
|
|
Model: shared.ChatModel(config.ModelName),
|
|
}
|
|
if config.MaxOutputTokens > 0 {
|
|
params.MaxCompletionTokens = openai.Int(int64(config.MaxOutputTokens))
|
|
}
|
|
applyProviderSpecificChatParams(¶ms, config)
|
|
|
|
client := newOpenAIClient(config)
|
|
chatResp, err := client.Chat.Completions.New(ctx, params, platformRequestOptions(ctx, config, "chat.completion")...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to call llm api (model=%s provider=%s system_chars=%d user_chars=%d max_output_tokens=%d): %w",
|
|
config.ModelName, config.Provider, utf8.RuneCountInString(systemPrompt), utf8.RuneCountInString(userPrompt), config.MaxOutputTokens, err)
|
|
}
|
|
if len(chatResp.Choices) == 0 {
|
|
return nil, fmt.Errorf("no llm choices in response")
|
|
}
|
|
|
|
content := strings.TrimSpace(chatResp.Choices[0].Message.Content)
|
|
return &ChatCompletionResult{
|
|
Content: content,
|
|
ModelName: config.ModelName,
|
|
PromptTokens: int(chatResp.Usage.PromptTokens),
|
|
CompletionTokens: int(chatResp.Usage.CompletionTokens),
|
|
}, nil
|
|
}
|
|
|
|
func applyProviderSpecificChatParams(params *openai.ChatCompletionNewParams, config models.AIConfig) {
|
|
if params == nil {
|
|
return
|
|
}
|
|
if isDeepSeekV4Model(config) {
|
|
params.SetExtraFields(map[string]any{
|
|
"thinking": map[string]any{"type": "disabled"},
|
|
})
|
|
} else if isDashScopeQwenThinkingModel(config) {
|
|
params.SetExtraFields(map[string]any{
|
|
"enable_thinking": false,
|
|
})
|
|
}
|
|
}
|
|
|
|
func isDeepSeekV4Model(config models.AIConfig) bool {
|
|
baseURL := strings.ToLower(strings.TrimSpace(config.BaseURL))
|
|
modelName := strings.ToLower(strings.TrimSpace(config.ModelName))
|
|
return strings.Contains(baseURL, "api.deepseek.com") && strings.HasPrefix(modelName, "deepseek-v4-")
|
|
}
|
|
|
|
func isDashScopeQwenThinkingModel(config models.AIConfig) bool {
|
|
baseURL := strings.ToLower(strings.TrimSpace(config.BaseURL))
|
|
modelName := strings.ToLower(strings.TrimSpace(config.ModelName))
|
|
return strings.Contains(baseURL, "dashscope.aliyuncs.com") && strings.HasPrefix(modelName, "qwen3")
|
|
}
|