import process from "node:process";
export const chat = async (
prompt: string | object = "Hello world",
options = {},
) => {
const { Configuration, OpenAIApi } = await import(
"https://esm.sh/openai@3.3.0"
);
const configuration = new Configuration({
apiKey: process.env.OPENAI,
});
const openai = new OpenAIApi(configuration);
const messages = typeof prompt === "string"
? [{ role: "user", content: prompt }]
: prompt;
const { data } = await openai.createChatCompletion({
model: "gpt-3.5-turbo-0613",
messages,
...options,
});
const message = data.choices[0].message;
return message.function_call ? message.function_call : message.content;
};