mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-05-24 14:40:22 +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 { Calculator } from "langchain/tools/calculator";
|
||||||
import { DuckDuckGo } from "@/app/api/langchain-tools/duckduckgo_search";
|
import { DuckDuckGo } from "@/app/api/langchain-tools/duckduckgo_search";
|
||||||
import { HttpGetTool } from "@/app/api/langchain-tools/http_get";
|
import { HttpGetTool } from "@/app/api/langchain-tools/http_get";
|
||||||
|
import { ACCESS_CODE_PREFIX } from "@/app/constant";
|
||||||
|
|
||||||
const serverConfig = getServerSideConfig();
|
const serverConfig = getServerSideConfig();
|
||||||
|
|
||||||
@ -36,6 +37,7 @@ interface RequestBody {
|
|||||||
frequency_penalty?: number;
|
frequency_penalty?: number;
|
||||||
top_p?: number;
|
top_p?: number;
|
||||||
baseUrl?: string;
|
baseUrl?: string;
|
||||||
|
apiKey?: string;
|
||||||
maxIterations: number;
|
maxIterations: number;
|
||||||
returnIntermediateSteps: boolean;
|
returnIntermediateSteps: boolean;
|
||||||
}
|
}
|
||||||
@ -67,6 +69,13 @@ async function handle(req: NextRequest) {
|
|||||||
const transformStream = new TransformStream();
|
const transformStream = new TransformStream();
|
||||||
const writer = transformStream.writable.getWriter();
|
const writer = transformStream.writable.getWriter();
|
||||||
const reqBody: RequestBody = await req.json();
|
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({
|
const handler = BaseCallbackHandler.fromMethods({
|
||||||
async handleLLMNewToken(token: string) {
|
async handleLLMNewToken(token: string) {
|
||||||
@ -209,7 +218,7 @@ async function handle(req: NextRequest) {
|
|||||||
const llm = new ChatOpenAI(
|
const llm = new ChatOpenAI(
|
||||||
{
|
{
|
||||||
modelName: reqBody.model,
|
modelName: reqBody.model,
|
||||||
openAIApiKey: serverConfig.apiKey,
|
openAIApiKey: apiKey,
|
||||||
temperature: reqBody.temperature,
|
temperature: reqBody.temperature,
|
||||||
streaming: reqBody.stream,
|
streaming: reqBody.stream,
|
||||||
topP: reqBody.top_p,
|
topP: reqBody.top_p,
|
||||||
|
Loading…
Reference in New Issue
Block a user