Skip to content

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

4.2 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 (
	"fmt"
	"sort"
	"strings"

	"ragflow/internal/tokenizer"
)

// ChunksFormat normalizes retrieval chunks into the response format expected by
// the ask endpoint.  It matches the Python chunks_format() in rag/prompts/generator.py.
// Returns an empty slice for nil or empty input.
func ChunksFormat(chunks []SourcedChunk) []map[string]interface{} {
	if len(chunks) == 0 {
		return []map[string]interface{}{}
	}
	out := make([]map[string]interface{}, len(chunks))
	for i, ck := range chunks {
		out[i] = map[string]interface{}{
			"id":                ck.ID,
			"content":           ck.Content,
			"document_id":       ck.DocID,
			"document_name":     ck.DocName,
			"dataset_id":        ck.DatasetID,
			"image_id":          ck.ImageID,
			"positions":         ck.Positions,
			"url":               ck.URL,
			"similarity":        ck.Similarity,
			"vector_similarity": ck.VectorSimilarity,
			"term_similarity":   ck.TermSimilarity,
			"row_id":            ck.ID, // row_id == ID for consistency with Python
			"doc_type":          ck.DocType,
			"document_metadata": ck.DocumentMetadata,
		}
	}
	return out
}

// KbPrompt builds a knowledge-base context string from retrieved chunks for use
// in the LLM system prompt.  Truncation uses the C++ tokenizer when available;
// falls back to a character-based approximation on systems where the tokenizer
// dictionary is not installed.
//
// Corresponds to kb_prompt() in rag/prompts/generator.py.
func KbPrompt(chunks []SourcedChunk, maxTokens int) string {
	if len(chunks) == 0 || maxTokens <= 0 {
		return ""
	}
	const tokenRatio = 0.97
	limit := int(float64(maxTokens) * tokenRatio)

	var b strings.Builder
	used := 0
	for i, ck := range chunks {
		entry := formatChunkEntry(ck, i)
		tokens := tokenizer.NumTokensFromString(entry)
		if used+tokens > limit {
			break
		}
		b.WriteString(entry)
		used += tokens
	}
	return b.String()
}

// formatChunkEntry renders a single chunk as a tree-structured entry for the LLM
// prompt.  Structure matches Python kb_prompt() in rag/prompts/generator.py:
//
//	ID: <index>
//	├── Title: <doc_name>
//	├── URL: <url>
//	├── <metadata_key>: <metadata_value>
//	└── Content:
//	<chunk content>
//
// index is the chunk's position in the list the caller returns as the reference, and
// is 0-based because that list is what a marker's number indexes (see citation.go) —
// a chunk id would come back as a dead marker. Python's kb_prompt numbers the blocks
// 1-based while its own resolver reads 0-based; Go keeps both on 0.
func formatChunkEntry(ck SourcedChunk, index int) string {
	var b strings.Builder
	fmt.Fprintf(&b, "ID: %d\n", index)
	if ck.DocName != "" {
		fmt.Fprintf(&b, "├── Title: %s\n", collapseNewlines(ck.DocName))
	}
	if ck.URL != "" {
		fmt.Fprintf(&b, "├── URL: %s\n", collapseNewlines(ck.URL))
	}
	if ck.DocumentMetadata != nil {
		keys := make([]string, 0, len(ck.DocumentMetadata))
		for k := range ck.DocumentMetadata {
			keys = append(keys, k)
		}
		sort.Strings(keys)
		for _, k := range keys {
			fmt.Fprintf(&b, "├── %s: %v\n", k, collapseNewlines(fmt.Sprint(ck.DocumentMetadata[k])))
		}
	}
	b.WriteString("└── Content:\n")
	b.WriteString(ck.Content)
	b.WriteString("\n\n")
	return b.String()
}

// collapseNewlines mirrors draw_node() in Python's _kb_block(): a title, URL or
// metadata value is a single line in the rendered block, so embedded newlines
// must not break the tree the model is reading.
func collapseNewlines(s string) string {
	if !strings.Contains(s, "\n") {
		return s
	}
	for strings.Contains(s, "\n\n") {
		s = strings.ReplaceAll(s, "\n\n", "\n")
	}
	return strings.ReplaceAll(s, "\n", " ")
}