feat: optimize WebUI stream output and sanitize user-facing answers
This commit is contained in:
@@ -81,9 +81,10 @@ type InputFile struct {
|
||||
}
|
||||
|
||||
type OpenAICompatibleClient struct {
|
||||
client openai.Client
|
||||
model string
|
||||
log *logger.Logger
|
||||
client openai.Client
|
||||
model string
|
||||
disableThinkingParam bool
|
||||
log *logger.Logger
|
||||
}
|
||||
|
||||
func NewOpenAICompatibleClient(cfg config.LLMConfig, log *logger.Logger) *OpenAICompatibleClient {
|
||||
@@ -95,9 +96,10 @@ func NewOpenAICompatibleClient(cfg config.LLMConfig, log *logger.Logger) *OpenAI
|
||||
opts = append(opts, option.WithBaseURL(cfg.BaseURL))
|
||||
}
|
||||
return &OpenAICompatibleClient{
|
||||
client: openai.NewClient(opts...),
|
||||
model: cfg.Model,
|
||||
log: log,
|
||||
client: openai.NewClient(opts...),
|
||||
model: cfg.Model,
|
||||
disableThinkingParam: shouldDisableThinkingParam(cfg.BaseURL),
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,7 +140,7 @@ func (c *OpenAICompatibleClient) GenerateWithTools(ctx context.Context, messages
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := c.client.Chat.Completions.New(ctx, params)
|
||||
resp, err := c.client.Chat.Completions.New(ctx, params, c.chatCompletionRequestOptions()...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("llm tool-call request failed: %w", err)
|
||||
}
|
||||
@@ -180,7 +182,7 @@ func (c *OpenAICompatibleClient) generateWithMessagesInternal(ctx context.Contex
|
||||
Messages: sdkMessages,
|
||||
}
|
||||
|
||||
resp, err := c.client.Chat.Completions.New(ctx, params)
|
||||
resp, err := c.client.Chat.Completions.New(ctx, params, c.chatCompletionRequestOptions()...)
|
||||
if err != nil {
|
||||
if c.log != nil {
|
||||
c.log.Errorf("llm request failed err=%v", err)
|
||||
@@ -392,3 +394,18 @@ func appendIfMissing(items []string, value string) []string {
|
||||
}
|
||||
return append(items, value)
|
||||
}
|
||||
|
||||
func (c *OpenAICompatibleClient) chatCompletionRequestOptions() []option.RequestOption {
|
||||
if !c.disableThinkingParam {
|
||||
return nil
|
||||
}
|
||||
return []option.RequestOption{option.WithJSONSet("enable_thinking", false)}
|
||||
}
|
||||
|
||||
func shouldDisableThinkingParam(baseURL string) bool {
|
||||
baseURL = strings.ToLower(strings.TrimSpace(baseURL))
|
||||
if baseURL == "" {
|
||||
return false
|
||||
}
|
||||
return strings.Contains(baseURL, "dashscope.aliyuncs.com")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user