feat: agent支持自定义openaikey

This commit is contained in:
Hk-Gosuto 2023-08-18 13:40:04 +08:00
parent 20366fee35
commit 9a8b3f06cd

View File

@ -19,6 +19,7 @@ import { SerpAPI } from "langchain/tools";
import { Calculator } from "langchain/tools/calculator";
import { DuckDuckGo } from "@/app/api/langchain-tools/duckduckgo_search";
import { HttpGetTool } from "@/app/api/langchain-tools/http_get";
import { ACCESS_CODE_PREFIX } from "@/app/constant";
const serverConfig = getServerSideConfig();
@ -36,6 +37,7 @@ interface RequestBody {
frequency_penalty?: number;
top_p?: number;
baseUrl?: string;
apiKey?: string;
maxIterations: number;
returnIntermediateSteps: boolean;
}
@ -67,6 +69,13 @@ async function handle(req: NextRequest) {
const transformStream = new TransformStream();
const writer = transformStream.writable.getWriter();
const reqBody: RequestBody = await req.json();
const authToken = req.headers.get("Authorization") ?? "";
const token = authToken.trim().replaceAll("Bearer ", "").trim();
const isOpenAiKey = !token.startsWith(ACCESS_CODE_PREFIX);
let apiKey = serverConfig.apiKey;
if (isOpenAiKey && token) {
apiKey = token;
}
const handler = BaseCallbackHandler.fromMethods({
async handleLLMNewToken(token: string) {
@ -209,7 +218,7 @@ async function handle(req: NextRequest) {
const llm = new ChatOpenAI(
{
modelName: reqBody.model,
openAIApiKey: serverConfig.apiKey,
openAIApiKey: apiKey,
temperature: reqBody.temperature,
streaming: reqBody.stream,
topP: reqBody.top_p,