Skip to content

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

6.0 KB
Raw Blame
//
//  Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

package service

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"regexp"
	"strings"

	"ragflow/internal/entity"
	modelModule "ragflow/internal/entity/models"
	"ragflow/internal/permission"
	"ragflow/internal/rag/prompts"
)

// GenerateRelatedQuestions generates related search questions for chat/searchbot endpoints.
func GenerateRelatedQuestions(ctx context.Context, question, searchID string, searchSvc *SearchService, modelFactory *ModelFactory) ([]string, error) {
	if modelFactory == nil {
		return nil, fmt.Errorf("model factory not configured")
	}
	searchConfig, tenantID, err := relatedQuestionsSearchConfig(ctx, searchID, searchSvc)
	if err != nil {
		return nil, err
	}
	if tenantID == "" {
		tenantID, err = requireCurrentTenantID(ctx)
		if err != nil {
			return nil, err
		}
	}
	modelCtx := permission.WithTenantID(ctx, tenantID)
	modelID, _ := searchConfig["chat_id"].(string)
	prompt, err := (prompts.EmbeddedPromptLoader{}).Load("related_question")
	if err != nil {
		return nil, err
	}
	messages := []modelModule.Message{
		{Role: "system", Content: prompt},
		{Role: "user", Content: "\nKeywords: " + question + "\nRelated search terms:\n    "},
	}
	var chatModel *modelModule.ChatModel
	if modelID == "" {
		chatModel, err = modelFactory.NewDefaultChatModel(modelCtx)
		if errors.Is(err, ErrNoDefaultModel) {
			return nil, fmt.Errorf("no chat model configured")
		}
	} else {
		chatModel, err = modelFactory.NewChatModel(modelCtx, modelID)
	}
	if err != nil {
		return nil, err
	}
	response, err := chatModel.ChatWithMessages(ctx, messages, relatedQuestionsConfig(searchConfig), nil)
	if err != nil {
		return nil, err
	}
	if response != nil && response.Answer != nil {
		return parseRelatedQuestions(*response.Answer), nil
	}
	return []string{}, nil
}

func relatedQuestionsSearchConfig(ctx context.Context, searchID string, searchSvc *SearchService) (map[string]interface{}, string, error) {
	if searchID == "" {
		return map[string]interface{}{}, "", nil
	}
	if searchSvc == nil {
		return nil, "", fmt.Errorf("search service not configured")
	}
	detail, err := searchSvc.GetDetail(ctx, searchID)
	if err != nil {
		return nil, "", err
	}
	tenantID, _ := detail["tenant_id"].(string)
	return relatedQuestionsSearchConfigFromDetail(detail), strings.TrimSpace(tenantID), nil
}

func relatedQuestionsSearchConfigFromDetail(detail map[string]interface{}) map[string]interface{} {
	if sc, ok := detail["search_config"].(map[string]interface{}); ok && sc != nil {
		return sc
	}
	if sc, ok := detail["search_config"].(entity.JSONMap); ok && sc != nil {
		return map[string]interface{}(sc)
	}
	return map[string]interface{}{}
}

func relatedQuestionsConfig(searchConfig map[string]interface{}) *modelModule.ChatConfig {
	var genConf map[string]interface{}
	switch v := searchConfig["llm_setting"].(type) {
	case map[string]interface{}:
		genConf = v
	case entity.JSONMap:
		genConf = map[string]interface{}(v)
	}
	if genConf == nil {
		return &modelModule.ChatConfig{Temperature: float64Ptr(0.9)}
	}
	cfg := &modelModule.ChatConfig{}
	for key, value := range genConf {
		if key == "parameter" {
			continue
		}
		switch key {
		case "stream":
			if v, ok := value.(bool); ok {
				cfg.Stream = &v
			}
		case "thinking":
			if v, ok := value.(bool); ok {
				cfg.Thinking = &v
			}
		case "max_tokens":
			if v, ok := intFromRelatedQuestionConfig(value); ok {
				cfg.MaxTokens = &v
			}
		case "temperature":
			if v, ok := floatFromRelatedQuestionConfig(value); ok {
				cfg.Temperature = &v
			}
		case "top_p":
			if v, ok := floatFromRelatedQuestionConfig(value); ok && v > 0 {
				cfg.TopP = &v
			}
		case "do_sample":
			if v, ok := value.(bool); ok {
				cfg.DoSample = &v
			}
		case "stop":
			if stops := stringSliceFromRelatedQuestionConfig(value); len(stops) > 0 {
				cfg.Stop = &stops
			}
		case "model_class":
			if v, ok := value.(string); ok {
				cfg.ModelClass = &v
			}
		case "effort":
			if v, ok := value.(string); ok {
				cfg.Effort = &v
			}
		case "verbosity":
			if v, ok := value.(string); ok {
				cfg.Verbosity = &v
			}
		}
	}
	return cfg
}

func intFromRelatedQuestionConfig(value interface{}) (int, bool) {
	switch v := value.(type) {
	case int:
		return v, true
	case int64:
		return int(v), true
	case float64:
		return int(v), true
	case json.Number:
		if n, err := v.Int64(); err == nil {
			return int(n), true
		}
	}
	return 0, false
}

func floatFromRelatedQuestionConfig(value interface{}) (float64, bool) {
	switch v := value.(type) {
	case float64:
		return v, true
	case float32:
		return float64(v), true
	case int:
		return float64(v), true
	case int64:
		return float64(v), true
	case json.Number:
		if n, err := v.Float64(); err == nil {
			return n, true
		}
	}
	return 0, false
}

func stringSliceFromRelatedQuestionConfig(value interface{}) []string {
	switch v := value.(type) {
	case []string:
		return v
	case []interface{}:
		result := make([]string, 0, len(v))
		for _, item := range v {
			if s, ok := item.(string); ok {
				result = append(result, s)
			}
		}
		return result
	}
	return nil
}

var relatedQuestionLineRe = regexp.MustCompile(`^\d+\.\s`)

func parseRelatedQuestions(text string) []string {
	var result []string
	for _, line := range strings.Split(text, "\n") {
		if relatedQuestionLineRe.MatchString(line) {
			result = append(result, relatedQuestionLineRe.ReplaceAllString(line, ""))
		}
	}
	if result == nil {
		return []string{}
	}
	return result
}

func float64Ptr(v float64) *float64 { return &v }