Skip to content

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

3.6 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 (
	"encoding/json"
	"fmt"
	"math"
)

const similarityWeightTolerance = 1e-9

// ResolveVectorSimilarityWeight validates the public keyword/vector weights and
// returns the vector weight used by retrieval. Keyword weight takes precedence.
func ResolveVectorSimilarityWeight(keywordsSimilarityWeight, vectorSimilarityWeight *float64) (*float64, error) {
	if keywordsSimilarityWeight != nil && (*keywordsSimilarityWeight < 0 || *keywordsSimilarityWeight > 1) {
		return nil, fmt.Errorf("keywords_similarity_weight must be between 0 and 1")
	}
	if vectorSimilarityWeight != nil && (*vectorSimilarityWeight < 0 || *vectorSimilarityWeight > 1) {
		return nil, fmt.Errorf("vector_similarity_weight must be between 0 and 1")
	}
	if keywordsSimilarityWeight != nil && vectorSimilarityWeight != nil && math.Abs(*keywordsSimilarityWeight+*vectorSimilarityWeight-1) > similarityWeightTolerance {
		return nil, fmt.Errorf("keywords_similarity_weight and vector_similarity_weight must sum to 1")
	}
	if keywordsSimilarityWeight != nil {
		resolved := 1 - *keywordsSimilarityWeight
		return &resolved, nil
	}
	return vectorSimilarityWeight, nil
}

// NormalizeSimilarityWeights validates similarity weights in a REST payload and
// synchronizes both field names. Payloads that omit both fields are unchanged.
func NormalizeSimilarityWeights(values map[string]interface{}) error {
	keywordsSimilarityWeight, err := similarityWeightFromMap(values, "keywords_similarity_weight")
	if err != nil {
		return err
	}
	vectorSimilarityWeight, err := similarityWeightFromMap(values, "vector_similarity_weight")
	if err != nil {
		return err
	}

	resolvedVectorWeight, err := ResolveVectorSimilarityWeight(keywordsSimilarityWeight, vectorSimilarityWeight)
	if err != nil {
		return err
	}
	if resolvedVectorWeight == nil {
		return nil
	}

	// Round before persisting: the float64 complement (1 - 0.7 =
	// 0.30000000000000004) would otherwise land in search_config verbatim.
	// keywords derives from the rounded vector, so the pair stays complementary.
	vectorWeight := roundSimilarityWeight(*resolvedVectorWeight)
	values["vector_similarity_weight"] = vectorWeight
	values["keywords_similarity_weight"] = roundSimilarityWeight(1 - vectorWeight)
	return nil
}

// roundSimilarityWeight snaps a weight to 6 decimal places.
func roundSimilarityWeight(weight float64) float64 {
	return math.Round(weight*1e6) / 1e6
}

func similarityWeightFromMap(values map[string]interface{}, key string) (*float64, error) {
	value, ok := values[key]
	if !ok || value == nil {
		return nil, nil
	}

	var weight float64
	switch typed := value.(type) {
	case float64:
		weight = typed
	case float32:
		weight = float64(typed)
	case int:
		weight = float64(typed)
	case int32:
		weight = float64(typed)
	case int64:
		weight = float64(typed)
	case json.Number:
		parsed, err := typed.Float64()
		if err != nil {
			return nil, fmt.Errorf("%s must be a number", key)
		}
		weight = parsed
	default:
		return nil, fmt.Errorf("%s must be a number", key)
	}
	return &weight, nil
}