From d052d0de5328886a929f55265c49cab66abd7637 Mon Sep 17 00:00:00 2001 From: Archer <545436317@qq.com> Date: Wed, 12 Mar 2025 16:51:00 +0800 Subject: [PATCH] fix: model test channel id;fix: quote reader (#4123) * fix: model test channel id * fix: quote reader --- .../ChatBox/components/ResponseTags.tsx | 45 +++++++++---- .../account/model/Channel/ModelTest.tsx | 13 +++- .../account/model/Channel/index.tsx | 12 +++- .../ChatQuoteList/CollectionQuoteReader.tsx | 3 + .../chat/ChatQuoteList/QuoteReader.tsx | 63 +++++++++++++++---- .../chat/ChatQuoteList/index.tsx | 4 +- .../app/src/pages/api/core/ai/model/test.ts | 47 ++++++++------ .../api/core/chat/quote/getCollectionQuote.ts | 5 +- projects/app/src/pages/chat/share.tsx | 19 +++--- projects/app/src/web/core/ai/config.ts | 3 +- .../web/core/chat/context/chatItemContext.tsx | 2 + 11 files changed, 151 insertions(+), 65 deletions(-) diff --git a/projects/app/src/components/core/chat/ChatContainer/ChatBox/components/ResponseTags.tsx b/projects/app/src/components/core/chat/ChatContainer/ChatBox/components/ResponseTags.tsx index 6e0dc276e..cab6e1b90 100644 --- a/projects/app/src/components/core/chat/ChatContainer/ChatBox/components/ResponseTags.tsx +++ b/projects/app/src/components/core/chat/ChatContainer/ChatBox/components/ResponseTags.tsx @@ -44,6 +44,7 @@ const ResponseTags = ({ const chatType = useContextSelector(ChatBoxContext, (v) => v.chatType); const appId = useContextSelector(ChatBoxContext, (v) => v.appId); const chatId = useContextSelector(ChatBoxContext, (v) => v.chatId); + const outLinkAuthData = useContextSelector(ChatBoxContext, (v) => v.outLinkAuthData); const setQuoteData = useContextSelector(ChatItemContext, (v) => v.setQuoteData); @@ -65,6 +66,7 @@ const ResponseTags = ({ ? quoteListRef.current.scrollHeight > (isPc ? 50 : 55) : true; + const isShowReadRawSource = useContextSelector(ChatItemContext, (v) => v.isShowReadRawSource); const sourceList = useMemo(() => { return Object.values( quoteList.reduce((acc: Record, cur) => { @@ -157,18 +159,34 @@ const ResponseTags = ({ onClick={(e) => { e.stopPropagation(); - setQuoteData({ - rawSearch: quoteList, - metadata: { - appId, - chatId, - chatItemDataId: dataId, - collectionId: item.collectionId, - sourceId: item.sourceId || '', - sourceName: item.sourceName, - datasetId: item.datasetId - } - }); + if (isShowReadRawSource) { + setQuoteData({ + rawSearch: quoteList, + metadata: { + appId, + chatId, + chatItemDataId: dataId, + collectionId: item.collectionId, + sourceId: item.sourceId || '', + sourceName: item.sourceName, + datasetId: item.datasetId, + outLinkAuthData + } + }); + } else { + setQuoteData({ + rawSearch: quoteList, + metadata: { + appId, + chatId, + chatItemDataId: dataId, + collectionIdList: [item.collectionId], + sourceId: item.sourceId || '', + sourceName: item.sourceName, + outLinkAuthData + } + }); + } }} height={6} > @@ -230,7 +248,8 @@ const ResponseTags = ({ appId, chatId, chatItemDataId: dataId, - collectionIdList: [...new Set(quoteList.map((item) => item.collectionId))] + collectionIdList: [...new Set(quoteList.map((item) => item.collectionId))], + outLinkAuthData } }); }} diff --git a/projects/app/src/pageComponents/account/model/Channel/ModelTest.tsx b/projects/app/src/pageComponents/account/model/Channel/ModelTest.tsx index 54895df06..74e3cf7ed 100644 --- a/projects/app/src/pageComponents/account/model/Channel/ModelTest.tsx +++ b/projects/app/src/pageComponents/account/model/Channel/ModelTest.tsx @@ -34,7 +34,15 @@ type ModelTestItem = { duration?: number; }; -const ModelTest = ({ models, onClose }: { models: string[]; onClose: () => void }) => { +const ModelTest = ({ + channelId, + models, + onClose +}: { + channelId: number; + models: string[]; + onClose: () => void; +}) => { const { t } = useTranslation(); const { toast } = useToast(); const [testModelList, setTestModelList] = useState([]); @@ -57,6 +65,7 @@ const ModelTest = ({ models, onClose }: { models: string[]; onClose: () => void colorSchema: 'red' } }); + const { loading: loadingModels } = useRequest2(getSystemModelList, { manual: false, refreshDeps: [models], @@ -95,7 +104,7 @@ const ModelTest = ({ models, onClose }: { models: string[]; onClose: () => void ); const start = Date.now(); try { - await getTestModel(model); + await getTestModel({ model, channelId }); const duration = Date.now() - start; setTestModelList((prev) => prev.map((item) => diff --git a/projects/app/src/pageComponents/account/model/Channel/index.tsx b/projects/app/src/pageComponents/account/model/Channel/index.tsx index 795cfd521..accd1ba32 100644 --- a/projects/app/src/pageComponents/account/model/Channel/index.tsx +++ b/projects/app/src/pageComponents/account/model/Channel/index.tsx @@ -74,7 +74,7 @@ const ChannelTable = ({ Tab }: { Tab: React.ReactNode }) => { } }); - const [testModels, setTestModels] = useState(); + const [modelTestData, setTestModelData] = useState<{ channelId: number; models: string[] }>(); const isLoading = loadingChannelList || @@ -165,7 +165,11 @@ const ChannelTable = ({ Tab }: { Tab: React.ReactNode }) => { { icon: 'core/chat/sendLight', label: t('account_model:model_test'), - onClick: () => setTestModels(item.models) + onClick: () => + setTestModelData({ + channelId: item.id, + models: item.models + }) }, ...(item.status === ChannelStatusEnum.ChannelStatusEnabled ? [ @@ -222,7 +226,9 @@ const ChannelTable = ({ Tab }: { Tab: React.ReactNode }) => { onSuccess={refreshChannelList} /> )} - {!!testModels && setTestModels(undefined)} />} + {!!modelTestData && ( + setTestModelData(undefined)} /> + )} ); }; diff --git a/projects/app/src/pageComponents/chat/ChatQuoteList/CollectionQuoteReader.tsx b/projects/app/src/pageComponents/chat/ChatQuoteList/CollectionQuoteReader.tsx index 415dd0497..32a290a88 100644 --- a/projects/app/src/pageComponents/chat/ChatQuoteList/CollectionQuoteReader.tsx +++ b/projects/app/src/pageComponents/chat/ChatQuoteList/CollectionQuoteReader.tsx @@ -35,9 +35,11 @@ const CollectionReader = ({ const { t } = useTranslation(); const router = useRouter(); const { userInfo } = useUserStore(); + const { collectionId, datasetId, chatItemDataId, sourceId, sourceName } = metadata; const [quoteIndex, setQuoteIndex] = useState(0); + // Get dataset permission const { data: permissionData, loading: isPermissionLoading } = useRequest2( async () => await getDatasetDataPermission(datasetId), { @@ -56,6 +58,7 @@ const CollectionReader = ({ const currentQuoteItem = filterResults[quoteIndex]; + // Get quote list const { dataList: datasetDataList, setDataList: setDatasetDataList, diff --git a/projects/app/src/pageComponents/chat/ChatQuoteList/QuoteReader.tsx b/projects/app/src/pageComponents/chat/ChatQuoteList/QuoteReader.tsx index f8979865e..be0e25261 100644 --- a/projects/app/src/pageComponents/chat/ChatQuoteList/QuoteReader.tsx +++ b/projects/app/src/pageComponents/chat/ChatQuoteList/QuoteReader.tsx @@ -22,10 +22,14 @@ const QuoteReader = ({ }) => { const { t } = useTranslation(); + const filterRawSearch = useMemo(() => { + return rawSearch.filter((item) => metadata.collectionIdList.includes(item.collectionId)); + }, [rawSearch, metadata.collectionIdList]); + const { data: quoteList, loading } = useRequest2( async () => await getQuoteDataList({ - datasetDataIdList: rawSearch.map((item) => item.id), + datasetDataIdList: filterRawSearch.map((item) => item.id), collectionIdList: metadata.collectionIdList, chatItemDataId: metadata.chatItemDataId, appId: metadata.appId, @@ -33,12 +37,13 @@ const QuoteReader = ({ ...metadata.outLinkAuthData }), { + refreshDeps: [metadata, filterRawSearch], manual: false } ); const formatedDataList = useMemo(() => { - return rawSearch + return filterRawSearch .map((searchItem) => { const dataItem = quoteList?.find((item) => item._id === searchItem.id); @@ -57,7 +62,7 @@ const QuoteReader = ({ .sort((a, b) => { return (b.score.primaryScore?.value || 0) - (a.score.primaryScore?.value || 0); }); - }, [quoteList, rawSearch]); + }, [quoteList, filterRawSearch]); return ( @@ -71,16 +76,48 @@ const QuoteReader = ({ > - - - {t('common:core.chat.Quote Amount', { amount: rawSearch.length })} - + {metadata.sourceId ? ( + <> + + + {metadata.sourceName || t('common:common.UnKnow Source')} + + + ) : ( + <> + + + {t('common:core.chat.Quote Amount', { amount: filterRawSearch.length })} + + + )} {t('common:core.chat.quote.Quote Tip')} diff --git a/projects/app/src/pageComponents/chat/ChatQuoteList/index.tsx b/projects/app/src/pageComponents/chat/ChatQuoteList/index.tsx index 21fb0491c..cc580f213 100644 --- a/projects/app/src/pageComponents/chat/ChatQuoteList/index.tsx +++ b/projects/app/src/pageComponents/chat/ChatQuoteList/index.tsx @@ -1,9 +1,9 @@ import React from 'react'; import { SearchDataResponseItemType } from '@fastgpt/global/core/dataset/type'; -import { useContextSelector } from 'use-context-selector'; import { ChatItemContext, GetQuoteProps } from '@/web/core/chat/context/chatItemContext'; import CollectionQuoteReader from './CollectionQuoteReader'; import QuoteReader from './QuoteReader'; +import { useContextSelector } from 'use-context-selector'; const ChatQuoteList = ({ rawSearch = [], @@ -18,7 +18,7 @@ const ChatQuoteList = ({ return ( <> - {'collectionId' in metadata && isShowReadRawSource && ( + {'collectionId' in metadata && ( )} {'collectionIdList' in metadata && ( diff --git a/projects/app/src/pages/api/core/ai/model/test.ts b/projects/app/src/pages/api/core/ai/model/test.ts index 49abd2ff9..68196612e 100644 --- a/projects/app/src/pages/api/core/ai/model/test.ts +++ b/projects/app/src/pages/api/core/ai/model/test.ts @@ -1,7 +1,7 @@ import type { ApiRequestProps, ApiResponseType } from '@fastgpt/service/type/next'; import { NextAPI } from '@/service/middleware/entry'; import { authSystemAdmin } from '@fastgpt/service/support/permission/user/auth'; -import { findModelFromAlldata, getReRankModel } from '@fastgpt/service/core/ai/model'; +import { findModelFromAlldata } from '@fastgpt/service/core/ai/model'; import { EmbeddingModelItemType, LLMModelItemType, @@ -9,7 +9,7 @@ import { STTModelType, TTSModelType } from '@fastgpt/global/core/ai/model.d'; -import { getAIApi } from '@fastgpt/service/core/ai/config'; +import { createChatCompletion, getAIApi } from '@fastgpt/service/core/ai/config'; import { addLog } from '@fastgpt/service/common/system/log'; import { getVectorsByText } from '@fastgpt/service/core/ai/embedding'; import { reRankRecall } from '@fastgpt/service/core/ai/rerank'; @@ -18,7 +18,7 @@ import { isProduction } from '@fastgpt/global/common/system/constants'; import * as fs from 'fs'; import { llmCompletionsBodyFormat } from '@fastgpt/service/core/ai/utils'; -export type testQuery = { model: string; channelId?: string }; +export type testQuery = { model: string; channelId?: number }; export type testBody = {}; @@ -37,7 +37,7 @@ async function handler( const headers: Record = channelId ? { - 'Aiproxy-Channel': channelId + 'Aiproxy-Channel': String(channelId) } : {}; @@ -75,26 +75,33 @@ const testLLMModel = async (model: LLMModelItemType, headers: Record { desc={props.appIntro || data?.app?.intro} icon={props.appAvatar || data?.app?.avatar} /> - + {(!quoteData || isPc) && ( - + {RenderHistoryList} @@ -302,7 +301,7 @@ const OutLink = (props: Props) => { )} {quoteData && ( - + GET('/core/ai/model/list'); export const getSystemModelDetail = (model: string) => @@ -21,7 +22,7 @@ export const getModelConfigJson = () => GET('/core/ai/model/getConfigJso export const putUpdateWithJson = (data: updateWithJsonBody) => PUT('/core/ai/model/updateWithJson', data); -export const getTestModel = (model: String) => GET('/core/ai/model/test', { model }); +export const getTestModel = (data: testQuery) => GET('/core/ai/model/test', data); export const putUpdateDefaultModels = (data: updateDefaultBody) => PUT('/core/ai/model/updateDefault', data); diff --git a/projects/app/src/web/core/chat/context/chatItemContext.tsx b/projects/app/src/web/core/chat/context/chatItemContext.tsx index c13fc0ae2..3193d7eeb 100644 --- a/projects/app/src/web/core/chat/context/chatItemContext.tsx +++ b/projects/app/src/web/core/chat/context/chatItemContext.tsx @@ -48,6 +48,8 @@ export type GetCollectionQuoteDataProps = GetQuoteDataBasicProps & { }; export type GetAllQuoteDataProps = GetQuoteDataBasicProps & { collectionIdList: string[]; + sourceId?: string; + sourceName?: string; }; export type GetQuoteProps = GetAllQuoteDataProps | GetCollectionQuoteDataProps;