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
import process from "node:process";
import { AIAgent } from "https://esm.town/v/zzz/AIAgent";
import { verifyJWT } from "https://esm.town/v/zzz/verifyJWT";
import { upstashRateLimiter } from "https://esm.town/v/zzz/upstashRateLimiter";
// Create a summary from a given text using GPT 4
export const OpenAISummary = async (text: string, config: {
apiKey?: string;
jwt?: string;
modelName?: string;
} = {}) => {
const { apiKey, jwt, modelName = "gpt-4" } = config;
if (!apiKey && !jwt) {
const { success, reset } = await upstashRateLimiter(
"anon",
"@zzz.OpenAISummary",
2,
"60 s",
);
if (!success) {
return `Rate Limit Exceeded. Try again at ${reset}`;
}
}
else if (!apiKey) {
try {
await verifyJWT(jwt);
}
catch (err) {
return `Invalid JWT`;
}
}
const agent = await AIAgent(
apiKey || process.env.OPENAI_API_KEY_GPT4,
);
const response = await agent.summarize(text, modelName);
return response;
};