Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

5.5 KB
Raw Blame
'use client'
import type { FC } from 'react'
import type { AnnotationReplyConfig } from '@/models/debug'
import { Button } from '@langgenius/dify-ui/button'
import {
  Dialog,
  DialogBody,
  DialogContent,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from '@langgenius/dify-ui/dialog'
import * as React from 'react'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
import { ModelSelector } from '@/app/components/header/account-setting/model-provider-page/model-selector'
import { toast } from '@/app/notifications'
import { ANNOTATION_DEFAULT } from '@/config'
import { getErrorMessage } from '@/utils/error-message'
import { Item } from './config-param'
import ScoreSlider from './score-slider'

type Props = Readonly<{
  appId: string
  isShow: boolean
  onHide: () => void
  onSave: (
    embeddingModel: {
      embedding_provider_name: string
      embedding_model_name: string
    },
    score: number,
  ) => void | Promise<void>
  isInit?: boolean
  annotationConfig: AnnotationReplyConfig
}>
const ConfigParamModal: FC<Props> = ({
  isShow,
  onHide: doHide,
  onSave,
  isInit,
  annotationConfig: oldAnnotationConfig,
}) => {
  const { t } = useTranslation(['appAnnotation', 'appDebug', 'common', 'modelProvider'])
  const {
    modelList: embeddingsModelList,
    defaultModel: embeddingsDefaultModel,
    currentModel: isEmbeddingsDefaultModelValid,
  } = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.textEmbedding)
  const [annotationConfig, setAnnotationConfig] = useState({
    ...oldAnnotationConfig,
    score_threshold: oldAnnotationConfig.score_threshold ?? ANNOTATION_DEFAULT.score_threshold,
  })
  const [isLoading, setLoading] = useState(false)
  const [embeddingModel, setEmbeddingModel] = useState(
    oldAnnotationConfig.embedding_model
      ? {
          providerName: oldAnnotationConfig.embedding_model.embedding_provider_name,
          modelName: oldAnnotationConfig.embedding_model.embedding_model_name,
        }
      : embeddingsDefaultModel
        ? {
            providerName: embeddingsDefaultModel.provider.provider,
            modelName: embeddingsDefaultModel.model,
          }
        : undefined,
  )
  const onHide = () => {
    if (!isLoading) doHide()
  }
  const handleSave = async () => {
    if (
      !embeddingModel ||
      !embeddingModel.modelName ||
      (embeddingModel.modelName === embeddingsDefaultModel?.model && !isEmbeddingsDefaultModelValid)
    ) {
      toast.error(t(($) => $['modelProvider.embeddingModel.required'], { ns: 'modelProvider' }))
      return
    }
    setLoading(true)
    try {
      await onSave(
        {
          embedding_provider_name: embeddingModel.providerName,
          embedding_model_name: embeddingModel.modelName,
        },
        annotationConfig.score_threshold,
      )
    } catch (error) {
      toast.error(getErrorMessage(error) || t(($) => $['api.actionFailed'], { ns: 'common' }))
    } finally {
      setLoading(false)
    }
  }
  return (
    <Dialog
      open={isShow}
      onOpenChange={(open) => {
        if (!open) onHide()
      }}
    >
      <DialogContent className="w-160">
        <DialogHeader>
          <DialogTitle>
            {t(($) => $[`initSetup.${isInit ? 'title' : 'configTitle'}`], { ns: 'appAnnotation' })}
          </DialogTitle>
        </DialogHeader>

        <DialogBody className="space-y-3">
          <Item
            title={t(($) => $['feature.annotation.scoreThreshold.title'], { ns: 'appDebug' })}
            tooltip={t(($) => $['feature.annotation.scoreThreshold.description'], {
              ns: 'appDebug',
            })}
          >
            <ScoreSlider
              className="mt-1"
              value={annotationConfig.score_threshold ?? ANNOTATION_DEFAULT.score_threshold}
              onChange={(val) => {
                setAnnotationConfig({
                  ...annotationConfig,
                  score_threshold: val,
                })
              }}
            />
          </Item>

          <Item
            title={t(($) => $['modelProvider.embeddingModel.key'], { ns: 'modelProvider' })}
            tooltip={t(($) => $.embeddingModelSwitchTip, { ns: 'appAnnotation' })}
          >
            <div className="pt-1">
              <ModelSelector
                value={
                  embeddingModel && {
                    provider: embeddingModel.providerName,
                    model: embeddingModel.modelName,
                  }
                }
                models={embeddingsModelList}
                onValueChange={(val) => {
                  setEmbeddingModel({
                    providerName: val.provider,
                    modelName: val.model,
                  })
                }}
              />
            </div>
          </Item>
        </DialogBody>

        <DialogFooter>
          <Button onClick={onHide}>{t(($) => $['operation.cancel'], { ns: 'common' })}</Button>
          <Button variant="primary" onClick={handleSave} loading={isLoading}>
            <div>
              {t(($) => $[`initSetup.${isInit ? 'confirmBtn' : 'configConfirmBtn'}`], {
                ns: 'appAnnotation',
              })}
            </div>
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}
export default React.memo(ConfigParamModal)