mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-01 23:56:39 +08:00
add basePath for api env inject
This commit is contained in:
parent
c93a46a02f
commit
e6241bf405
@ -6,6 +6,10 @@ async function createStream(req: NextRequest) {
|
|||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
|
|
||||||
let apiKey = process.env.OPENAI_API_KEY;
|
let apiKey = process.env.OPENAI_API_KEY;
|
||||||
|
let apiBasePath = process.env.OPENAI_API_BASE_PATH;
|
||||||
|
if (apiBasePath) {
|
||||||
|
apiBasePath = apiBasePath.replace(/\/+$/, "");
|
||||||
|
}
|
||||||
|
|
||||||
const userApiKey = req.headers.get("token");
|
const userApiKey = req.headers.get("token");
|
||||||
if (userApiKey) {
|
if (userApiKey) {
|
||||||
@ -13,7 +17,7 @@ async function createStream(req: NextRequest) {
|
|||||||
console.log("[Stream] using user api key");
|
console.log("[Stream] using user api key");
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await fetch("https://api.openai.com/v1/chat/completions", {
|
const res = await fetch(`${apiBasePath ? apiBasePath : 'https://api.openai.com/v1'}/chat/completions`, {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${apiKey}`,
|
Authorization: `Bearer ${apiKey}`,
|
||||||
|
@ -1,20 +1,23 @@
|
|||||||
import { OpenAIApi, Configuration } from "openai";
|
import { OpenAIApi, Configuration, ConfigurationParameters } from "openai";
|
||||||
import { ChatRequest } from "./typing";
|
import { ChatRequest } from "./typing";
|
||||||
|
|
||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
try {
|
try {
|
||||||
let apiKey = process.env.OPENAI_API_KEY;
|
let apiKey = process.env.OPENAI_API_KEY;
|
||||||
|
let apiBasePath = process.env.OPENAI_API_BASE_PATH;
|
||||||
|
|
||||||
const userApiKey = req.headers.get("token");
|
const userApiKey = req.headers.get("token");
|
||||||
if (userApiKey) {
|
if (userApiKey) {
|
||||||
apiKey = userApiKey;
|
apiKey = userApiKey;
|
||||||
}
|
}
|
||||||
|
let configuration: ConfigurationParameters = {
|
||||||
|
apiKey,
|
||||||
|
};
|
||||||
|
if (apiBasePath) {
|
||||||
|
configuration.basePath = apiBasePath.replace(/\/+$/, "");
|
||||||
|
}
|
||||||
|
|
||||||
const openai = new OpenAIApi(
|
const openai = new OpenAIApi(new Configuration(configuration));
|
||||||
new Configuration({
|
|
||||||
apiKey,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
const requestBody = (await req.json()) as ChatRequest;
|
const requestBody = (await req.json()) as ChatRequest;
|
||||||
const completion = await openai!.createChatCompletion({
|
const completion = await openai!.createChatCompletion({
|
||||||
|
Loading…
Reference in New Issue
Block a user