feat: optimize WebUI stream output and sanitize user-facing answers

This commit is contained in:
2026-03-13 13:14:37 +08:00
parent 8dc5354fa4
commit 33c357a1de
8 changed files with 228 additions and 45 deletions

View File

@@ -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")
}

View File

@@ -0,0 +1,24 @@
package llm
import "testing"
func TestShouldDisableThinkingParam(t *testing.T) {
if !shouldDisableThinkingParam("https://dashscope.aliyuncs.com/compatible-mode/v1") {
t.Fatal("expected DashScope base URL to require enable_thinking=false")
}
if shouldDisableThinkingParam("https://api.openai.com/v1") {
t.Fatal("expected standard OpenAI base URL not to require enable_thinking=false")
}
}
func TestChatCompletionRequestOptions(t *testing.T) {
client := &OpenAICompatibleClient{disableThinkingParam: true}
if got := len(client.chatCompletionRequestOptions()); got != 1 {
t.Fatalf("expected 1 request option when disableThinkingParam=true, got %d", got)
}
client.disableThinkingParam = false
if got := len(client.chatCompletionRequestOptions()); got != 0 {
t.Fatalf("expected 0 request options when disableThinkingParam=false, got %d", got)
}
}