From 966021ed74cb0788f7ac15b93dd3ba6bd0b0b3a5 Mon Sep 17 00:00:00 2001 From: Jungley Date: Sun, 26 Mar 2023 01:08:03 +0800 Subject: [PATCH] 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. --- next-i18next.config.js | 2 -- pages/api/models.ts | 3 ++- types/env.d.ts | 6 ++++++ utils/app/const.ts | 3 +++ utils/server/index.ts | 3 ++- 5 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 types/env.d.ts diff --git a/next-i18next.config.js b/next-i18next.config.js index 224b276..1c44b8a 100644 --- a/next-i18next.config.js +++ b/next-i18next.config.js @@ -1,5 +1,3 @@ -const path = require("path"); - module.exports = { i18n: { defaultLocale: "en", diff --git a/pages/api/models.ts b/pages/api/models.ts index 839f6aa..7f441ad 100644 --- a/pages/api/models.ts +++ b/pages/api/models.ts @@ -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 => { 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}` diff --git a/types/env.d.ts b/types/env.d.ts new file mode 100644 index 0000000..b816009 --- /dev/null +++ b/types/env.d.ts @@ -0,0 +1,6 @@ +namespace NodeJS { + interface ProcessEnv { + OPENAI_API_KEY: string; + OPENAI_API_HOST?: string; + } +} diff --git a/utils/app/const.ts b/utils/app/const.ts index badac76..0b40c68 100644 --- a/utils/app/const.ts +++ b/utils/app/const.ts @@ -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' \ No newline at end of file diff --git a/utils/server/index.ts b/utils/server/index.ts index 9c44d5a..b25cf4a 100644 --- a/utils/server/index.ts +++ b/utils/server/index.ts @@ -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}`