Files
chatbot-ui/pages/api/models.ts
T
Jungley 966021ed74 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.
2023-03-25 11:08:03 -06:00

57 lines
1.4 KiB
TypeScript

import { OpenAIModel, OpenAIModelID, OpenAIModels } from "@/types";
import { OPENAI_API_HOST } from "@/utils/app/const";
export const config = {
runtime: "edge"
};
const handler = async (req: Request): Promise<Response> => {
try {
const { key } = (await req.json()) as {
key: string;
};
const response = await fetch(`${OPENAI_API_HOST}/v1/models`, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${key ? key : process.env.OPENAI_API_KEY}`
}
});
if (response.status === 401) {
return new Response(
response.body,
{
status: 500,
headers: response.headers
}
);
} else if (response.status !== 200) {
console.error(`OpenAI API returned an error ${response.status}: ${await response.text()}`)
throw new Error("OpenAI API returned an error");
}
const json = await response.json();
const models: OpenAIModel[] = json.data
.map((model: any) => {
for (const [key, value] of Object.entries(OpenAIModelID)) {
if (value === model.id) {
return {
id: model.id,
name: OpenAIModels[value].name
};
}
}
})
.filter(Boolean);
return new Response(JSON.stringify(models), { status: 200 });
} catch (error) {
console.error(error);
return new Response("Error", { status: 500 });
}
};
export default handler;