diff --git a/.env.template b/.env.template index 6b6021392..7be2f86f3 100644 --- a/.env.template +++ b/.env.template @@ -55,7 +55,7 @@ NEXT_PUBLIC_ENABLE_NODEJS_PLUGIN=1 # (optional) # Default: Empty # If you want to enable RAG, set this value to 1. -NEXT_PUBLIC_ENABLE_RAG= +ENABLE_RAG= # (optional) # Default: Empty diff --git a/app/api/config/route.ts b/app/api/config/route.ts index db84fba17..aed95a1b4 100644 --- a/app/api/config/route.ts +++ b/app/api/config/route.ts @@ -13,6 +13,7 @@ const DANGER_CONFIG = { hideBalanceQuery: serverConfig.hideBalanceQuery, disableFastLink: serverConfig.disableFastLink, customModels: serverConfig.customModels, + isEnableRAG: serverConfig.isEnableRAG, }; declare global { diff --git a/app/api/file/upload/route.ts b/app/api/file/upload/route.ts index e9a0b0ae1..c372065e7 100644 --- a/app/api/file/upload/route.ts +++ b/app/api/file/upload/route.ts @@ -21,14 +21,9 @@ async function handle(req: NextRequest) { try { const formData = await req.formData(); const file = formData.get("file") as File; + const fileData = await file.arrayBuffer(); const originalFileName = file?.name; - let fileData: ArrayBuffer | undefined; - for (const [key, value] of formData.entries()) { - if (value instanceof File) { - fileData = await value.arrayBuffer(); - } - } if (!fileData) throw new Error("Get file buffer error"); const buffer = Buffer.from(fileData); const fileType = path.extname(originalFileName).slice(1); diff --git a/app/api/langchain-tools/nodejs_tools.ts b/app/api/langchain-tools/nodejs_tools.ts index 4683b39fb..8bdb1892b 100644 --- a/app/api/langchain-tools/nodejs_tools.ts +++ b/app/api/langchain-tools/nodejs_tools.ts @@ -60,7 +60,7 @@ export class NodeJSTool { wolframAlphaTool, pdfBrowserTool, ]; - if (!!process.env.NEXT_PUBLIC_ENABLE_RAG) { + if (!!process.env.ENABLE_RAG) { tools.push(new RAGSearch(this.sessionId, this.model, this.ragEmbeddings)); } return tools; diff --git a/app/api/langchain-tools/rag_search.ts b/app/api/langchain-tools/rag_search.ts index 16755ea39..c3db3c4c3 100644 --- a/app/api/langchain-tools/rag_search.ts +++ b/app/api/langchain-tools/rag_search.ts @@ -37,6 +37,8 @@ export class RAGSearch extends Tool { /** @ignore */ async _call(inputs: string, runManager?: CallbackManagerForToolRun) { const serverConfig = getServerSideConfig(); + if (!serverConfig.isEnableRAG) + throw new Error("env ENABLE_RAG not configured"); // const pinecone = new Pinecone(); // const pineconeIndex = pinecone.Index(serverConfig.pineconeIndex!); // const vectorStore = await PineconeStore.fromExistingIndex(this.embeddings, { diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 7ddb929ad..01e9eb62a 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -117,7 +117,6 @@ import { SpeechApi, WebTranscriptionApi, } from "../utils/speech"; -import { getServerSideConfig } from "../config/server"; import { FileInfo } from "../client/platforms/utils"; const ttsPlayer = createTTSPlayer(); @@ -507,14 +506,18 @@ export function ChatActions(props: { ); const [showModelSelector, setShowModelSelector] = useState(false); const [showUploadImage, setShowUploadImage] = useState(false); - const [showUploadFile, setShowUploadFile] = useState(false); + const accessStore = useAccessStore(); + const isEnableRAG = useMemo( + () => accessStore.enableRAG(), + // eslint-disable-next-line react-hooks/exhaustive-deps + [], + ); useEffect(() => { const show = isVisionModel(currentModel); setShowUploadImage(show); - const isEnableRAG = !!process.env.NEXT_PUBLIC_ENABLE_RAG; setShowUploadFile(isEnableRAG && !show && isSupportRAGModel(currentModel)); if (!show) { props.setAttachImages([]); diff --git a/app/config/server.ts b/app/config/server.ts index 7a8228b9f..36ff0d3cf 100644 --- a/app/config/server.ts +++ b/app/config/server.ts @@ -112,7 +112,7 @@ export const getServerSideConfig = () => { !process.env.R2_ACCOUNT_ID && !process.env.S3_ENDPOINT, - isEnableRAG: !!process.env.NEXT_PUBLIC_ENABLE_RAG, + isEnableRAG: !!process.env.ENABLE_RAG, ragEmbeddingModel: process.env.RAG_EMBEDDING_MODEL ?? "text-embedding-3-large", ragChunkSize: process.env.RAG_CHUNK_SIZE ?? "2000", diff --git a/app/store/access.ts b/app/store/access.ts index 75442a6a9..c10e4e6c9 100644 --- a/app/store/access.ts +++ b/app/store/access.ts @@ -56,8 +56,10 @@ export const useAccessStore = createPersistStore( return get().needCode; }, - isEnableRAG() { - return ensure(get(), ["isEnableRAG"]); + enableRAG() { + this.fetch(); + + return get().isEnableRAG; }, isValidOpenAI() { diff --git a/app/store/chat.ts b/app/store/chat.ts index b46ef2819..9293aecf4 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -374,9 +374,9 @@ export const useChatStore = createPersistStore( session.messages.push(savedUserMessage); session.messages.push(botMessage); }); + const isEnableRAG = attachFiles && attachFiles?.length > 0; var api: ClientApi; api = new ClientApi(ModelProvider.GPT); - const isEnableRAG = !!process.env.NEXT_PUBLIC_ENABLE_RAG; if ( config.pluginConfig.enable && session.mask.usePlugins && diff --git a/app/utils.ts b/app/utils.ts index 2d4155501..e6042402b 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -3,6 +3,7 @@ import { showToast } from "./components/ui-lib"; import Locale from "./locales"; import { RequestMessage } from "./client/api"; import { DEFAULT_MODELS } from "./constant"; +import { useAccessStore } from "./store"; export function trimTopic(topic: string) { // Fix an issue where double quotes still show in the Indonesian language diff --git a/docs/rag-cn.md b/docs/rag-cn.md index 2183f2495..6f56ea3ee 100644 --- a/docs/rag-cn.md +++ b/docs/rag-cn.md @@ -41,7 +41,7 @@ ## 环境变量 -### `NEXT_PUBLIC_ENABLE_RAG` +### `ENABLE_RAG` 如果你想启用 RAG 功能,将此环境变量设置为 1 即可。