Public
Script
October 23, 2023
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
/**
* Calls the OpenAI moderation model. Useful for determining if OpenAI will flag something you did.
* https://platform.openai.com/docs/api-reference/moderations
*/
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;
}
}[];
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023