mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-08-08 17:21:37 +08:00
feat: prod/dev env settings
This commit is contained in:
1
app/api/chat/config.template.ts
Normal file
1
app/api/chat/config.template.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const apiKey = "your dev api key here";
|
@@ -1,27 +1,43 @@
|
||||
import { OpenAIApi, Configuration } from "openai";
|
||||
import { apiKey } from "./config";
|
||||
import { ChatRequest } from "./typing";
|
||||
|
||||
// set up openai api client
|
||||
const config = new Configuration({
|
||||
apiKey,
|
||||
});
|
||||
const openai = new OpenAIApi(config);
|
||||
const isProd = process.env.NODE_ENV === "production";
|
||||
|
||||
let openai: OpenAIApi | undefined;
|
||||
async function initService() {
|
||||
let apiKey = process.env.OPENAI_API_KEY;
|
||||
|
||||
if (!isProd) {
|
||||
apiKey = await (await import("./config")).apiKey;
|
||||
}
|
||||
|
||||
openai = new OpenAIApi(
|
||||
new Configuration({
|
||||
apiKey,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
if (!openai) {
|
||||
await initService();
|
||||
}
|
||||
|
||||
try {
|
||||
const requestBody = (await req.json()) as ChatRequest;
|
||||
const completion = await openai.createChatCompletion(
|
||||
const completion = await openai!.createChatCompletion(
|
||||
{
|
||||
...requestBody,
|
||||
},
|
||||
{
|
||||
proxy: {
|
||||
protocol: "socks",
|
||||
host: "127.0.0.1",
|
||||
port: 7890,
|
||||
},
|
||||
}
|
||||
isProd
|
||||
? {}
|
||||
: {
|
||||
proxy: {
|
||||
protocol: "socks",
|
||||
host: "127.0.0.1",
|
||||
port: 7890,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(JSON.stringify(completion.data));
|
||||
|
Reference in New Issue
Block a user