feat: Allow customization of OpenAI host with environment variable (#152)

This commit modifies the OpenAI host configuration to support customization through an environment variable. This change is particularly useful in scenarios where access to the official OpenAI host is restricted or unavailable, allowing users to configure an alternative host for their specific needs.
This commit is contained in:
Jungley 2023-03-26 01:08:03 +08:00 committed by GitHub
parent dd44a06213
commit 966021ed74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 4 deletions

View File

@ -1,5 +1,3 @@
const path = require("path");
module.exports = {
i18n: {
defaultLocale: "en",

View File

@ -1,4 +1,5 @@
import { OpenAIModel, OpenAIModelID, OpenAIModels } from "@/types";
import { OPENAI_API_HOST } from "@/utils/app/const";
export const config = {
runtime: "edge"
@ -10,7 +11,7 @@ const handler = async (req: Request): Promise<Response> => {
key: string;
};
const response = await fetch("https://api.openai.com/v1/models", {
const response = await fetch(`${OPENAI_API_HOST}/v1/models`, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${key ? key : process.env.OPENAI_API_KEY}`

6
types/env.d.ts vendored Normal file
View File

@ -0,0 +1,6 @@
namespace NodeJS {
interface ProcessEnv {
OPENAI_API_KEY: string;
OPENAI_API_HOST?: string;
}
}

View File

@ -1 +1,4 @@
export const DEFAULT_SYSTEM_PROMPT = "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown.";
export const OPENAI_API_HOST = process.env.OPENAI_API_HOST || 'https://api.openai.com'

View File

@ -1,8 +1,9 @@
import { Message, OpenAIModel } from "@/types";
import { createParser, ParsedEvent, ReconnectInterval } from "eventsource-parser";
import { OPENAI_API_HOST } from "../app/const";
export const OpenAIStream = async (model: OpenAIModel, systemPrompt: string, key: string, messages: Message[]) => {
const res = await fetch("https://api.openai.com/v1/chat/completions", {
const res = await fetch(`${OPENAI_API_HOST}/v1/chat/completions`, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${key ? key : process.env.OPENAI_API_KEY}`