forked from XiaoMo/ChatGPT-Next-Web
feat: prod/dev env settings
This commit is contained in:
parent
2c9baa4e2c
commit
144eb68414
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,21 +1,37 @@
|
|||||||
import { OpenAIApi, Configuration } from "openai";
|
import { OpenAIApi, Configuration } from "openai";
|
||||||
import { apiKey } from "./config";
|
|
||||||
import { ChatRequest } from "./typing";
|
import { ChatRequest } from "./typing";
|
||||||
|
|
||||||
// set up openai api client
|
const isProd = process.env.NODE_ENV === "production";
|
||||||
const config = new Configuration({
|
|
||||||
|
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,
|
apiKey,
|
||||||
});
|
})
|
||||||
const openai = new OpenAIApi(config);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
|
if (!openai) {
|
||||||
|
await initService();
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const requestBody = (await req.json()) as ChatRequest;
|
const requestBody = (await req.json()) as ChatRequest;
|
||||||
const completion = await openai.createChatCompletion(
|
const completion = await openai!.createChatCompletion(
|
||||||
{
|
{
|
||||||
...requestBody,
|
...requestBody,
|
||||||
},
|
},
|
||||||
{
|
isProd
|
||||||
|
? {}
|
||||||
|
: {
|
||||||
proxy: {
|
proxy: {
|
||||||
protocol: "socks",
|
protocol: "socks",
|
||||||
host: "127.0.0.1",
|
host: "127.0.0.1",
|
||||||
|
@ -1742,7 +1742,14 @@ cosmiconfig@^7.0.1:
|
|||||||
path-type "^4.0.0"
|
path-type "^4.0.0"
|
||||||
yaml "^1.10.0"
|
yaml "^1.10.0"
|
||||||
|
|
||||||
cross-spawn@^7.0.2, cross-spawn@^7.0.3:
|
cross-env@^7.0.3:
|
||||||
|
version "7.0.3"
|
||||||
|
resolved "https://registry.npmmirror.com/cross-env/-/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf"
|
||||||
|
integrity sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==
|
||||||
|
dependencies:
|
||||||
|
cross-spawn "^7.0.1"
|
||||||
|
|
||||||
|
cross-spawn@^7.0.1, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
|
||||||
version "7.0.3"
|
version "7.0.3"
|
||||||
resolved "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
|
resolved "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
|
||||||
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
|
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
|
||||||
|
Loading…
Reference in New Issue
Block a user