chore: add openai api endpoint config field (#11)

This commit is contained in:
boojack
2023-03-30 15:05:13 +08:00
committed by GitHub
parent 201388ec40
commit 04ab812070
3 changed files with 7 additions and 2 deletions

View File

@ -1 +1,2 @@
OPENAI_API_KEY=<YOUR_API_KEY>
OPENAI_API_ENDPOINT=<YOUR_API_ENDPOINT>

View File

@ -1,4 +1,4 @@
import { openAIApiKey } from "@/utils";
import { openAIApiEndpoint, openAIApiKey } from "@/utils";
import { createParser, ParsedEvent, ReconnectInterval } from "eventsource-parser";
import { NextRequest } from "next/server";
@ -8,7 +8,7 @@ export const config = {
const handler = async (req: NextRequest) => {
const reqBody = await req.json();
const rawRes = await fetch(`https://api.openai.com/v1/chat/completions`, {
const rawRes = await fetch(`${openAIApiEndpoint}/v1/chat/completions`, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${openAIApiKey}`,

View File

@ -1,7 +1,11 @@
import { encode } from "@nem035/gpt-3-encoder";
// openAIApiKey is the API key for OpenAI API.
export const openAIApiKey = process.env.OPENAI_API_KEY;
// openAIApiEndpoint is the API endpoint for OpenAI API. Defaults to https://api.openai.com.
export const openAIApiEndpoint = process.env.OPENAI_API_ENDPOINT || "https://api.openai.com";
export const countTextTokens = (text: string) => {
return encode(text).length;
};