Skip to content

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

5.3 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.
 */

import { FormLayout } from '@/constants/form';
import { useTranslate } from '@/hooks/common-hooks';
import { cn } from '@/lib/utils';
import { useFormContext } from 'react-hook-form';
import { z } from 'zod';
import { SliderInputFormField } from '../slider-input-form-field';
import { SingleFormSlider } from '../ui/dual-range-slider';
import {
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from '../ui/form';
import { NumberInput } from '../ui/input';

interface SimilaritySliderFormFieldProps {
  similarityName?: string;
  similarityWeightName?: string;
  isTooltipShown?: boolean;
  numberInputClassName?: string;
}

export const initialSimilarityThresholdValue = {
  similarity_threshold: 0.2,
};
export const initialKeywordsSimilarityWeightValue = {
  keywords_similarity_weight: 0.7,
};

export const similarityThresholdSchema = { similarity_threshold: z.number() };

export const keywordsSimilarityWeightSchema = {
  keywords_similarity_weight: z.number().min(0).max(1),
};

export function SimilaritySliderFormField({
  similarityName = 'similarity_threshold',
  similarityWeightName = 'keywords_similarity_weight',
  isTooltipShown,
  numberInputClassName,
}: SimilaritySliderFormFieldProps) {
  const { t } = useTranslate('knowledgeDetails');
  const form = useFormContext();
  const normalizeWeight = (weight: number) =>
    Number(Math.min(1, Math.max(0, weight)).toFixed(2));

  return (
    <>
      <SliderInputFormField
        name={similarityName}
        label={t('similarityThreshold')}
        max={1}
        step={0.01}
        layout={FormLayout.Vertical}
        tooltip={isTooltipShown && t('similarityThresholdTip')}
        numberInputClassName={numberInputClassName}
        percentage
      ></SliderInputFormField>
      <FormField
        control={form.control}
        name={similarityWeightName}
        defaultValue={0}
        render={({ field }) => {
          const handleSliderChange = (value: number) =>
            field.onChange(normalizeWeight(value));

          return (
            <FormItem
            // className={cn({ 'flex items-center gap-1 space-y-0': isHorizontal })}
            >
              <FormLabel
                tooltip={isTooltipShown && t('keywordSimilarityWeightTip')}
              >
                {t('keywordSimilarityWeight')}
              </FormLabel>
              <div className={cn('flex items-end gap-4 justify-between')}>
                <FormControl>
                  <div className="flex flex-col flex-1 gap-2">
                    <div className="flex justify-between items-center">
                      <div className="flex items-center gap-1">
                        <label className="italic text-xs text-text-secondary">
                          full-text
                        </label>
                        <span className="bg-bg-card rounded-md p-1 w-10 text-center text-xs">
                          {normalizeWeight(field.value).toFixed(2)}
                        </span>
                      </div>
                      <div className="flex  items-center gap-1">
                        <label className="italic text-xs text-text-secondary">
                          vector
                        </label>
                        <span className="bg-bg-card rounded-md p-1 w-10 text-center text-xs">
                          {normalizeWeight(1 - field.value).toFixed(2)}
                        </span>
                      </div>
                    </div>
                    <SingleFormSlider
                      {...field}
                      value={normalizeWeight(field.value)}
                      onChange={handleSliderChange}
                      max={1}
                      step={0.01}
                      min={0}
                    ></SingleFormSlider>
                  </div>
                </FormControl>
                <FormControl>
                  <NumberInput
                    className={cn(
                      'h-6 w-10 p-0 text-center bg-bg-input border-border-button border text-text-secondary',
                      '[appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none',
                      numberInputClassName,
                    )}
                    max={1}
                    min={0}
                    step={0.01}
                    {...field}
                    value={normalizeWeight(field.value)}
                    onChange={(value) =>
                      field.onChange(normalizeWeight(Number(value)))
                    }
                  ></NumberInput>
                </FormControl>
              </div>
              <FormMessage />
            </FormItem>
          );
        }}
      />
    </>
  );
}