import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export let openAiTextCompletion = async (params: {
apiKey: string,
org?: string,
prompt: string,
model?: string,
suffix?: string,
max_tokens?: number,
temperature?: number,
top_p?: number,
n?: number,
stream?: false,
logprobs?: number,
echo?: boolean,
stop?: string | string[],
presence_penalty?: number,
frequency_penalty?: number,
best_of?: number,
logit_bias?: { [key: string]: number },
user?: string,
}) => {
if (!params.apiKey) {
throw new Error(
"Please provide 'apiKey' param. See: https://beta.openai.com/account/api-keys "
);
}
const { apiKey, org, ...args } = params;
args.stream = false;
const response = await fetchJSON("https://api.openai.com/v1/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${params.apiKey}`,
...(params.org ? { "OpenAI-Organization": params.org } : {}),
},
body: JSON.stringify(args),
});
return response as Response;
};
interface Response {
id: string;
object: "text-completion";
created: number;
model: string;
choices: {
text: string;
index: number;
logprobs: any;
finish_reason: string;
}[];
usage: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
}
}