2023-11-18 11:53:35 +08:00
|
|
|
import { RETRIEVE_METHOD, type RetrievalConfig } from '@/types/app'
|
2024-01-02 23:42:00 +08:00
|
|
|
import type {
|
|
|
|
DefaultModelResponse,
|
|
|
|
Model,
|
|
|
|
} from '@/app/components/header/account-setting/model-provider-page/declarations'
|
2024-07-24 12:50:48 +08:00
|
|
|
import { RerankingModeEnum } from '@/models/datasets'
|
2023-11-18 11:53:35 +08:00
|
|
|
|
|
|
|
export const isReRankModelSelected = ({
|
|
|
|
rerankDefaultModel,
|
2024-09-08 13:14:11 +08:00
|
|
|
isRerankDefaultModelValid,
|
2023-11-18 11:53:35 +08:00
|
|
|
retrievalConfig,
|
2023-11-21 13:46:07 +08:00
|
|
|
rerankModelList,
|
2023-11-18 11:53:35 +08:00
|
|
|
indexMethod,
|
|
|
|
}: {
|
2024-01-02 23:42:00 +08:00
|
|
|
rerankDefaultModel?: DefaultModelResponse
|
2024-09-08 13:14:11 +08:00
|
|
|
isRerankDefaultModelValid: boolean
|
2023-11-18 11:53:35 +08:00
|
|
|
retrievalConfig: RetrievalConfig
|
2024-01-02 23:42:00 +08:00
|
|
|
rerankModelList: Model[]
|
2023-11-18 11:53:35 +08:00
|
|
|
indexMethod?: string
|
|
|
|
}) => {
|
2023-11-21 13:46:07 +08:00
|
|
|
const rerankModelSelected = (() => {
|
2024-01-02 23:42:00 +08:00
|
|
|
if (retrievalConfig.reranking_model?.reranking_model_name) {
|
|
|
|
const provider = rerankModelList.find(({ provider }) => provider === retrievalConfig.reranking_model?.reranking_provider_name)
|
|
|
|
|
|
|
|
return provider?.models.find(({ model }) => model === retrievalConfig.reranking_model?.reranking_model_name)
|
|
|
|
}
|
2023-11-21 13:46:07 +08:00
|
|
|
|
2024-09-08 13:14:11 +08:00
|
|
|
if (isRerankDefaultModelValid)
|
2023-11-21 13:46:07 +08:00
|
|
|
return !!rerankDefaultModel
|
|
|
|
|
|
|
|
return false
|
|
|
|
})()
|
|
|
|
|
2023-11-18 11:53:35 +08:00
|
|
|
if (
|
|
|
|
indexMethod === 'high_quality'
|
2024-07-24 12:50:48 +08:00
|
|
|
&& (retrievalConfig.search_method === RETRIEVE_METHOD.hybrid && retrievalConfig.reranking_mode !== RerankingModeEnum.WeightedScore)
|
2023-11-21 13:46:07 +08:00
|
|
|
&& !rerankModelSelected
|
2023-11-18 11:53:35 +08:00
|
|
|
)
|
|
|
|
return false
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ensureRerankModelSelected = ({
|
|
|
|
rerankDefaultModel,
|
|
|
|
indexMethod,
|
|
|
|
retrievalConfig,
|
|
|
|
}: {
|
2024-01-02 23:42:00 +08:00
|
|
|
rerankDefaultModel: DefaultModelResponse
|
2023-11-18 11:53:35 +08:00
|
|
|
retrievalConfig: RetrievalConfig
|
|
|
|
indexMethod?: string
|
|
|
|
}) => {
|
|
|
|
const rerankModel = retrievalConfig.reranking_model?.reranking_model_name ? retrievalConfig.reranking_model : undefined
|
|
|
|
if (
|
|
|
|
indexMethod === 'high_quality'
|
2023-11-21 13:46:07 +08:00
|
|
|
&& (retrievalConfig.reranking_enable || retrievalConfig.search_method === RETRIEVE_METHOD.hybrid)
|
2023-11-18 11:53:35 +08:00
|
|
|
&& !rerankModel
|
2024-08-02 18:27:06 +08:00
|
|
|
&& rerankDefaultModel
|
2023-11-18 11:53:35 +08:00
|
|
|
) {
|
|
|
|
return {
|
|
|
|
...retrievalConfig,
|
|
|
|
reranking_model: {
|
2024-01-02 23:42:00 +08:00
|
|
|
reranking_provider_name: rerankDefaultModel.provider.provider,
|
|
|
|
reranking_model_name: rerankDefaultModel.model,
|
2023-11-18 11:53:35 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retrievalConfig
|
|
|
|
}
|