From 20366fee35adf546b16362fa8ec3cb22ef6e92c0 Mon Sep 17 00:00:00 2001 From: Hk-Gosuto Date: Fri, 18 Aug 2023 12:01:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20agent=E6=94=AF=E6=8C=81baseUrl=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- app/api/langchain/tool/agent/route.ts | 33 +++++++++++++++++++-------- app/client/platforms/openai.ts | 1 + 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1a6abcadb..8b2128279 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ 尝试使用 `chat-conversational-react-description` 等类型的 `agent` 使用插件时效果并不理想,不再考虑支持其它版本的模型。 - [x] `SERPAPI_API_KEY` 目前为必填,后续会支持使用 DuckDuckGo 替换搜索插件 -- [ ] Agent 不支持自定义接口地址 +- [x] Agent 不支持自定义接口地址 - [x] ~~部分场景下插件会调用失败~~ 问题出现在使用 [Calculator](https://js.langchain.com/docs/api/tools_calculator/classes/Calculator) 进行计算时的参数错误,暂时无法干预。 diff --git a/app/api/langchain/tool/agent/route.ts b/app/api/langchain/tool/agent/route.ts index 9b428f02f..d33501db7 100644 --- a/app/api/langchain/tool/agent/route.ts +++ b/app/api/langchain/tool/agent/route.ts @@ -35,6 +35,7 @@ interface RequestBody { presence_penalty?: number; frequency_penalty?: number; top_p?: number; + baseUrl?: string; maxIterations: number; returnIntermediateSteps: boolean; } @@ -194,15 +195,29 @@ async function handle(req: NextRequest) { outputKey: "output", chatHistory: new ChatMessageHistory(pastMessages), }); - const llm = new ChatOpenAI({ - modelName: reqBody.model, - openAIApiKey: serverConfig.apiKey, - temperature: reqBody.temperature, - streaming: reqBody.stream, - topP: reqBody.top_p, - presencePenalty: reqBody.presence_penalty, - frequencyPenalty: reqBody.frequency_penalty, - }); + // support base url + let baseUrl = "https://api.openai.com/v1"; + if (serverConfig.baseUrl) baseUrl = serverConfig.baseUrl; + if ( + reqBody.baseUrl?.startsWith("http://") || + reqBody.baseUrl?.startsWith("https://") + ) + baseUrl = reqBody.baseUrl; + if (!baseUrl.endsWith("/v1")) + baseUrl = baseUrl.endsWith("/") ? `${baseUrl}v1` : `${baseUrl}/v1`; + console.log("[baseUrl]", baseUrl); + const llm = new ChatOpenAI( + { + modelName: reqBody.model, + openAIApiKey: serverConfig.apiKey, + temperature: reqBody.temperature, + streaming: reqBody.stream, + topP: reqBody.top_p, + presencePenalty: reqBody.presence_penalty, + frequencyPenalty: reqBody.frequency_penalty, + }, + { basePath: baseUrl }, + ); const executor = await initializeAgentExecutorWithOptions(tools, llm, { agentType: "openai-functions", returnIntermediateSteps: reqBody.returnIntermediateSteps, diff --git a/app/client/platforms/openai.ts b/app/client/platforms/openai.ts index 08caeaff0..9721dcd07 100644 --- a/app/client/platforms/openai.ts +++ b/app/client/platforms/openai.ts @@ -217,6 +217,7 @@ export class ChatGPTApi implements LLMApi { presence_penalty: modelConfig.presence_penalty, frequency_penalty: modelConfig.frequency_penalty, top_p: modelConfig.top_p, + baseUrl: useAccessStore.getState().openaiUrl, maxIterations: options.agentConfig.maxIterations, returnIntermediateSteps: options.agentConfig.returnIntermediateSteps, };