import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export let openAiModeration = async ({
apiKey,
input,
model,
}: {
apiKey: string,
input: string|string[],
model?: "text-moderation-latest" | "text-moderation-stable",
}) => {
if (!apiKey) {
throw new Error("You must provide an OpenAI API Key");
}
const body: { model?: string, input: string|string[] } = {
input,
};
if (model) {
body.model = model;
}
const result = await fetchJSON(
"https://api.openai.com/v1/moderations",
{
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify(body),
}
);
return result as Result;
};
interface Result {
id: string;
model: string;
results: {
flagged: boolean;
categories: {
sexual: boolean;
hate: boolean;
violence: boolean;
"self-harm": boolean;
"sexual/minors": boolean;
"hate/threatening": boolean;
"violence/graphic": boolean;
},
category_scores: {
sexual: number;
hate: number;
violence: number;
"self-harm": number;
"sexual/minors": number;
"hate/threatening": number;
"violence/graphic": number;
}
}[];
}