import { fetch } from "https://esm.town/v/std/fetch";
import process from "node:process";
export let getChatgpt = async ({ prompts, token }) => {
let text = "";
const postdata = {
model: "text-davinci-003",
prompt: `${prompts}`,
temperature: 0.7,
n: 1,
logprobs: null,
max_tokens: 1024,
};
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${token || process.env.openaiKey}`,
};
const getCompelitoins = async (data) => {
const response = await fetch("https://api.openai.com/v1/completions", {
method: "POST",
headers: {
...headers,
},
body: JSON.stringify(data),
});
const result = await response.json();
return result;
};
const data = await getCompelitoins(postdata);
if (data?.error?.type === "server_error") {
return data.error.message;
}
return data.choices;
};