Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Fix(go): use llm setting for AI summary and return raw error msg (#18615)

Lynn141bcff471fb
diff --git a/internal/handler/mindmap.go b/internal/handler/mindmap.go
index 120950c030..05b20bf297 100644
--- a/internal/handler/mindmap.go
+++ b/internal/handler/mindmap.go
@@ -72,10 +72,10 @@ func runMindMap(ctx context.Context, config mindMapRunConfig) (mindMapNode, erro
// composite-name parsing which fails for bare IDs. If the configured
// model can't be resolved, fall back to the tenant's default chat model
// (mirrors Python's gen_mindmap get_tenant_default_model_by_type).
- ch, streamErr := config.LLM.ChatStream(streamCtx, modelTenantID, modelID, messages, &modelModule.ChatConfig{})
+ ch, _, streamErr := config.LLM.ChatStream(streamCtx, modelTenantID, modelID, messages, &modelModule.ChatConfig{})
if streamErr != nil && config.TenantSvc != nil {
if defaultModel, err := config.TenantSvc.GetDefaultModelName(streamCtx, modelTenantID, entity.ModelTypeChat); err == nil && defaultModel != "" && defaultModel != modelID {
- ch, streamErr = config.LLM.ChatStream(streamCtx, modelTenantID, defaultModel, messages, &modelModule.ChatConfig{})
+ ch, _, streamErr = config.LLM.ChatStream(streamCtx, modelTenantID, defaultModel, messages, &modelModule.ChatConfig{})
}
}
if streamErr != nil {
diff --git a/internal/service/ask_service.go b/internal/service/ask_service.go
index 942b82d4ce..46234df9ee 100644
--- a/internal/service/ask_service.go
+++ b/internal/service/ask_service.go
@@ -35,6 +35,10 @@ const (
DefaultAskVectorSimilarityWeight = 0.3
DefaultAskTokenBudget = 4096
DefaultAskStreamMinTokens = 16
+ // DefaultAskTemperature and DefaultAskTopP mirror Python's
+ // LLM_SETTING_DEFAULTS in api/db/services/llm_service.py.
+ DefaultAskTemperature = 0.1
+ DefaultAskTopP = 0.3
)
// AskDeltaKind classifies a streaming event emitted by AskService.
@@ -68,6 +72,8 @@ type AskStreamOptions struct {
Keyword *bool
SimilarityThreshold *float64
VectorSimilarityWeight *float64
+ Temperature *float64
+ TopP *float64
}
// Retriever abstracts chunk retrieval for AskService.
@@ -75,9 +81,26 @@ type Retriever interface {
RetrievalTest(ctx context.Context, req *RetrievalTestRequest, userID string) (*RetrievalTestResponse, error)
}
-// StreamingLLM abstracts streaming chat for AskService.
+// StreamingLLM abstracts streaming chat for AskService. The first channel
+// carries answer deltas; the second delivers the driver error when the stream
+// terminates abnormally (nil when it completes cleanly). The trailing error is
+// non-nil only for synchronous setup failures.
type StreamingLLM interface {
- ChatStream(ctx context.Context, messages []modelModule.Message, config *modelModule.ChatConfig) (<-chan string, error)
+ ChatStream(ctx context.Context, messages []modelModule.Message, config *modelModule.ChatConfig) (<-chan string, <-chan error, error)
+}
+
+// llmErrorMessage converts a raw LLM driver error into a user-facing message,
+// mirroring Python's completion error handling which appends an API-Key hint
+// when the provider rejects the credentials.
+func llmErrorMessage(err error) string {
+ if err == nil {
+ return "LLM call failed"
+ }
+ msg := err.Error()
+ if strings.Contains(msg, "invalid key") || strings.Contains(msg, "invalid api") {
+ return msg + " Please set LLM API-Key in 'User Setting -> Model Providers -> API-Key'"
+ }
+ return msg
}
// AskService performs retrieval-augmented Q&A with streaming output.
@@ -191,12 +214,18 @@ func (s *AskService) run(ctx context.Context, llm StreamingLLM, userID, question
// first, then collapses into a hidden think block, and the provider may
// emit only a fragment as the visible answer once the reasoning has
// consumed the output budget.
- genConf := &modelModule.ChatConfig{Temperature: ptrFloat64(0.1), Thinking: ptrBool(false)}
+ genConf := &modelModule.ChatConfig{Temperature: ptrFloat64(DefaultAskTemperature), Thinking: ptrBool(false)}
+ if opts.Temperature != nil {
+ genConf.Temperature = opts.Temperature
+ }
+ if opts.TopP != nil {
+ genConf.TopP = opts.TopP
+ }
- ch, err := llm.ChatStream(ctx, messages, genConf)
+ ch, llmErrs, err := llm.ChatStream(ctx, messages, genConf)
if err != nil {
common.Warn("AskService LLM stream failed", zap.Error(err))
- s.sendOrCancel(out, AskDelta{Kind: AskDeltaError, Value: "LLM call failed"}, ctx)
+ s.sendOrCancel(out, AskDelta{Kind: AskDeltaError, Value: llmErrorMessage(err)}, ctx)
return
}
@@ -212,6 +241,20 @@ func (s *AskService) run(ctx context.Context, llm StreamingLLM, userID, question
}
}
+ // Surface the real driver error (e.g. a provider 400/401) instead of a
+ // generic message when the stream terminated abnormally.
+ var llmErr error
+ select {
+ case llmErr = <-llmErrs:
+ case <-ctx.Done():
+ return
+ }
+ if llmErr != nil {
+ common.Warn("AskService LLM stream failed", zap.Error(llmErr))
+ s.sendOrCancel(out, AskDelta{Kind: AskDeltaError, Value: llmErrorMessage(llmErr)}, ctx)
+ return
+ }
+
// Phase 4: Finalize — citation insertion + reference formatting.
visible := ExtractVisibleAnswer(fullAnswer)
if strings.TrimSpace(visible) == "" {
diff --git a/internal/service/ask_service_test.go b/internal/service/ask_service_test.go
index 6b6deb5dfe..8caa783454 100644
--- a/internal/service/ask_service_test.go
+++ b/internal/service/ask_service_test.go
@@ -44,16 +44,44 @@ type fakeStreamLLM struct {
err error
}
-func (f *fakeStreamLLM) ChatStream(ctx context.Context, messages []modelModule.Message, config *modelModule.ChatConfig) (<-chan string, error) {
+func (f *fakeStreamLLM) ChatStream(ctx context.Context, messages []modelModule.Message, config *modelModule.ChatConfig) (<-chan string, <-chan error, error) {
if f.err != nil {
- return nil, f.err
+ return nil, nil, f.err
}
ch := make(chan string, len(f.chunks)+1)
for _, c := range f.chunks {
ch <- c
}
close(ch)
- return ch, nil
+ errCh := make(chan error, 1)
+ close(errCh)
+ return ch, errCh, nil
+}
+
+// capturingStreamLLM records the ChatConfig actually handed to the LLM.
+type capturingStreamLLM struct {
+ fakeStreamLLM
+ gotConfig *modelModule.ChatConfig
+}
+
+func (c *capturingStreamLLM) ChatStream(ctx context.Context, messages []modelModule.Message, config *modelModule.ChatConfig) (<-chan string, <-chan error, error) {
+ c.gotConfig = config
+ return c.fakeStreamLLM.ChatStream(ctx, messages, config)
+}
+
+// asyncErrStreamLLM returns answer deltas, then reports an async driver error.
+type asyncErrStreamLLM struct {
+ fakeStreamLLM
+}
+
+func (a *asyncErrStreamLLM) ChatStream(ctx context.Context, messages []modelModule.Message, config *modelModule.ChatConfig) (<-chan string, <-chan error, error) {
+ ch := make(chan string, 1)
+ ch <- "partial"
+ close(ch)
+ errCh := make(chan error, 1)
+ errCh <- fmt.Errorf("API request failed with status 400: invalid temperature")
+ close(errCh)
+ return ch, errCh, nil
}
// ---- AskService tests ----
@@ -165,6 +193,97 @@ func TestAskService_LLMError(t *testing.T) {
}
}
+func TestAskService_TemperatureFromLLMSetting(t *testing.T) {
+ ret := &fakeRetriever{result: &RetrievalTestResponse{
+ Chunks: []map[string]interface{}{
+ {"id": "c1", "content_with_weight": "chunk", "docnm_kwd": "Doc", "kb_id": "kb1", "doc_id": "d1"},
+ },
+ DocAggs: []map[string]interface{}{},
+ }}
+ llm := &capturingStreamLLM{fakeStreamLLM: fakeStreamLLM{chunks: []string{"answer"}}}
+ svc := NewAskService(ret, nil, 0, 0)
+
+ opts := askOptionsFromSearchConfig("s1", map[string]interface{}{
+ "llm_setting": map[string]interface{}{
+ "temperature": 0.6,
+ "temperature_enabled": true,
+ },
+ })
+ deltas := collect(svc.StreamWithOptions(context.Background(), llm, "user1", "test", []string{"kb1"}, opts))
+
+ var hasFinal bool
+ for _, d := range deltas {
+ if d.Kind == AskDeltaFinal {
+ hasFinal = true
+ }
+ }
+ if !hasFinal {
+ t.Fatalf("expected final delta, got %+v", deltas)
+ }
+ if llm.gotConfig == nil || llm.gotConfig.Temperature == nil || *llm.gotConfig.Temperature != 0.6 {
+ t.Errorf("expected configured temperature 0.6, got %+v", llm.gotConfig)
+ }
+}
+
+func TestAskService_TemperatureDisabledUsesDefault(t *testing.T) {
+ ret := &fakeRetriever{result: &RetrievalTestResponse{
+ Chunks: []map[string]interface{}{
+ {"id": "c1", "content_with_weight": "chunk", "docnm_kwd": "Doc", "kb_id": "kb1", "doc_id": "d1"},
+ },
+ DocAggs: []map[string]interface{}{},
+ }}
+ llm := &capturingStreamLLM{fakeStreamLLM: fakeStreamLLM{chunks: []string{"answer"}}}
+ svc := NewAskService(ret, nil, 0, 0)
+
+ opts := askOptionsFromSearchConfig("s1", map[string]interface{}{
+ "llm_setting": map[string]interface{}{
+ "temperature": 0.6,
+ "temperature_enabled": false,
+ },
+ })
+ deltas := collect(svc.StreamWithOptions(context.Background(), llm, "user1", "test", []string{"kb1"}, opts))
+
+ var hasFinal bool
+ for _, d := range deltas {
+ if d.Kind == AskDeltaFinal {
+ hasFinal = true
+ }
+ }
+ if !hasFinal {
+ t.Fatalf("expected final delta, got %+v", deltas)
+ }
+ if llm.gotConfig == nil || llm.gotConfig.Temperature == nil || *llm.gotConfig.Temperature != 0.1 {
+ t.Errorf("expected default temperature 0.1 when disabled, got %+v", llm.gotConfig)
+ }
+}
+
+func TestAskService_TemperatureMissingUsesLLMSettingDefaults(t *testing.T) {
+ ret := &fakeRetriever{result: &RetrievalTestResponse{
+ Chunks: []map[string]interface{}{
+ {"id": "c1", "content_with_weight": "chunk", "docnm_kwd": "Doc", "kb_id": "kb1", "doc_id": "d1"},
+ },
+ DocAggs: []map[string]interface{}{},
+ }}
+ llm := &capturingStreamLLM{fakeStreamLLM: fakeStreamLLM{chunks: []string{"answer"}}}
+ svc := NewAskService(ret, nil, 0, 0)
+
+ // Flag enabled but value absent (and a completely absent flag) must
+ // substitute the Python LLM_SETTING_DEFAULTS, matching resolve_llm_setting.
+ opts := askOptionsFromSearchConfig("s1", map[string]interface{}{
+ "llm_setting": map[string]interface{}{
+ "temperature_enabled": true,
+ },
+ })
+ collect(svc.StreamWithOptions(context.Background(), llm, "user1", "test", []string{"kb1"}, opts))
+
+ if llm.gotConfig == nil || llm.gotConfig.Temperature == nil || *llm.gotConfig.Temperature != DefaultAskTemperature {
+ t.Errorf("expected LLM_SETTING_DEFAULTS temperature %v, got %+v", DefaultAskTemperature, llm.gotConfig)
+ }
+ if llm.gotConfig.TopP == nil || *llm.gotConfig.TopP != DefaultAskTopP {
+ t.Errorf("expected LLM_SETTING_DEFAULTS top_p %v, got %+v", DefaultAskTopP, llm.gotConfig)
+ }
+}
+
func TestExtractChunkVectors_Empty(t *testing.T) {
if got := ExtractChunkVectors(nil); got != nil {
t.Errorf("expected nil for nil input, got %v", got)
@@ -247,3 +366,46 @@ func TestAskService_ContextCancel(t *testing.T) {
// Should get no deltas (or very few) since context is cancelled.
_ = deltas
}
+
+func TestAskService_AsyncLLMErrorSurfaces(t *testing.T) {
+ ret := &fakeRetriever{result: &RetrievalTestResponse{
+ Chunks: []map[string]interface{}{
+ {"id": "c1", "content_with_weight": "chunk", "docnm_kwd": "Doc", "kb_id": "kb1", "doc_id": "d1"},
+ },
+ DocAggs: []map[string]interface{}{},
+ }}
+ llm := &asyncErrStreamLLM{fakeStreamLLM: fakeStreamLLM{chunks: []string{"partial"}}}
+ svc := NewAskService(ret, nil, 0, 0)
+ deltas := collect(svc.Stream(context.Background(), llm, "user1", "test", []string{"kb1"}))
+
+ var errDelta *AskDelta
+ for i := range deltas {
+ if deltas[i].Kind == AskDeltaError {
+ errDelta = &deltas[i]
+ break
+ }
+ }
+ if errDelta == nil {
+ t.Fatalf("expected error delta, got %+v", deltas)
+ }
+ if !strings.Contains(errDelta.Value, "invalid temperature") {
+ t.Errorf("expected real driver error surfaced, got %q", errDelta.Value)
+ }
+}
+
+func TestAskService_LLMErrorValueIsRealError(t *testing.T) {
+ ret := &fakeRetriever{result: &RetrievalTestResponse{
+ Chunks: []map[string]interface{}{
+ {"id": "c1", "content_with_weight": "chunk"},
+ },
+ }}
+ llm := &fakeStreamLLM{err: fmt.Errorf("provider name missing in model name: gpt-4o")}
+ svc := NewAskService(ret, nil, 0, 0)
+ deltas := collect(svc.Stream(context.Background(), llm, "user1", "test", []string{"kb1"}))
+ if len(deltas) < 1 || deltas[0].Kind != AskDeltaError {
+ t.Fatalf("expected error delta, got %+v", deltas)
+ }
+ if !strings.Contains(deltas[0].Value, "provider name missing in model name") {
+ t.Errorf("expected real sync error surfaced, got %q", deltas[0].Value)
+ }
+}
diff --git a/internal/service/model_chat.go b/internal/service/model_chat.go
index 3258f1a196..d3651f3e6b 100644
--- a/internal/service/model_chat.go
+++ b/internal/service/model_chat.go
@@ -42,18 +42,21 @@ func (m *ModelProviderService) Chat(ctx context.Context, tenantID, modelID strin
return chatModel.ModelDriver.ChatWithMessages(ctx, *chatModel.ModelName, messages, chatModel.APIConfig, config, nil)
}
-func (m *ModelProviderService) ChatStream(ctx context.Context, tenantID, modelID string, messages []modelModule.Message, config *modelModule.ChatConfig) (<-chan string, error) {
+func (m *ModelProviderService) ChatStream(ctx context.Context, tenantID, modelID string, messages []modelModule.Message, config *modelModule.ChatConfig) (<-chan string, <-chan error, error) {
chatModel, err := m.GetChatModel(ctx, tenantID, modelID)
if err != nil {
- return nil, err
+ return nil, nil, err
}
- return chatStreamWithContext(ctx, chatModel, messages, config), nil
+ ch, errCh := chatStreamWithContext(ctx, chatModel, messages, config)
+ return ch, errCh, nil
}
-func chatStreamWithContext(ctx context.Context, chatModel *modelModule.ChatModel, messages []modelModule.Message, config *modelModule.ChatConfig) <-chan string {
+func chatStreamWithContext(ctx context.Context, chatModel *modelModule.ChatModel, messages []modelModule.Message, config *modelModule.ChatConfig) (<-chan string, <-chan error) {
ch := make(chan string, 256)
+ errCh := make(chan error, 1)
go func() {
defer close(ch)
+ defer close(errCh)
if err := chatModel.ModelDriver.ChatStreamlyWithSender(ctx, *chatModel.ModelName, messages, chatModel.APIConfig, config, nil,
func(delta *string, _ *string) error {
if delta == nil {
@@ -73,9 +76,10 @@ func chatStreamWithContext(ctx context.Context, chatModel *modelModule.ChatModel
return
}
common.Warn("ChatStreamlyWithSender returned error", zap.Error(err))
+ errCh <- err
}
}()
- return ch
+ return ch, errCh
}
// TenantStreamAdapter adapts tenant/model-aware chat streaming to AskService.
@@ -85,9 +89,9 @@ type TenantStreamAdapter struct {
ModelID string
}
-func (a *TenantStreamAdapter) ChatStream(ctx context.Context, messages []modelModule.Message, config *modelModule.ChatConfig) (<-chan string, error) {
+func (a *TenantStreamAdapter) ChatStream(ctx context.Context, messages []modelModule.Message, config *modelModule.ChatConfig) (<-chan string, <-chan error, error) {
if a.LLM == nil {
- return nil, fmt.Errorf("streaming LLM not configured")
+ return nil, nil, fmt.Errorf("streaming LLM not configured")
}
return a.LLM.ChatStream(ctx, a.TenantID, a.ModelID, messages, config)
}
diff --git a/internal/service/model_chat_test.go b/internal/service/model_chat_test.go
index 890821fc52..fe9326e4ae 100644
--- a/internal/service/model_chat_test.go
+++ b/internal/service/model_chat_test.go
@@ -53,7 +53,7 @@ func TestChatStreamWithContextStripsDoneSentinel(t *testing.T) {
APIConfig: &modelModule.APIConfig{},
}
- ch := chatStreamWithContext(context.Background(), chatModel, nil, &modelModule.ChatConfig{})
+ ch, _ := chatStreamWithContext(context.Background(), chatModel, nil, &modelModule.ChatConfig{})
var sb strings.Builder
for delta := range ch {
sb.WriteString(delta)
diff --git a/internal/service/search.go b/internal/service/search.go
index df4ed5d51f..6f450495e3 100644
--- a/internal/service/search.go
+++ b/internal/service/search.go
@@ -453,9 +453,28 @@ func askOptionsFromSearchConfig(searchID string, searchConfig map[string]interfa
if value, ok := floatFromSearchConfig(searchConfig["vector_similarity_weight"]); ok {
opts.VectorSimilarityWeight = &value
}
+ if llmSetting, ok := searchConfigMapValue(searchConfig["llm_setting"]); ok {
+ opts.Temperature = generationFloat(llmSetting, "temperature", DefaultAskTemperature)
+ opts.TopP = generationFloat(llmSetting, "top_p", DefaultAskTopP)
+ }
return opts
}
+// generationFloat resolves a chat-generation parameter from the llm_setting
+// saved in search_config, matching Python's resolve_llm_setting: the
+// user-configured value is kept only when the {key}_enabled flag is enabled
+// (absent flags count as enabled) and the value is present; otherwise the
+// parameter's LLM_SETTING_DEFAULTS fallback is substituted.
+func generationFloat(llmSetting map[string]interface{}, key string, fallback float64) *float64 {
+ if enabled, ok := llmSetting[key+"_enabled"].(bool); ok && !enabled {
+ return &fallback
+ }
+ if v, ok := floatFromSearchConfig(llmSetting[key]); ok {
+ return &v
+ }
+ return &fallback
+}
+
func searchConfigMapFromValue(value interface{}) map[string]interface{} {
if result, ok := searchConfigMapValue(value); ok {
return result