mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-05-24 06:30:16 +09:00
feat: agent支持自定义openaikey
This commit is contained in:
parent
20366fee35
commit
9a8b3f06cd
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user