forked from ascorbic/daneel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.ts
65 lines (59 loc) · 1.69 KB
/
chat.ts
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
63
64
65
import type { Config, Context } from "https://edge.netlify.com/";
import { getChatStream, sanitizeMessages } from "../../lib/edge/openai.ts";
import { appConfig } from "../../config.edge.ts";
if (!appConfig.OPENAI_API_KEY || !appConfig.systemPrompt) {
throw new Error(
"OPENAI_API_KEY and systemPrompt must be set in config.edge.ts"
);
}
export default async function handler(
request: Request,
context: Context
): Promise<Response> {
const prompt =
typeof appConfig.systemPrompt === "function"
? await appConfig.systemPrompt(request, context)
: appConfig.systemPrompt;
try {
const data = await request.json();
// This only trims the size of the messages, to avoid abuse of the API.
// You should do any extra validation yourself.
const messages = sanitizeMessages(
data?.messages ?? [],
appConfig.historyLength,
appConfig.maxMessageLength
);
const stream = await getChatStream(
{
// Optional. This can also be set to a real user id, session id or leave blank.
// See https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids
user: context.ip,
...appConfig.apiConfig,
messages: [
{
role: "system",
content: prompt,
},
...messages,
],
},
appConfig.OPENAI_API_KEY ?? ""
);
return new Response(stream, {
headers: {
"Content-Type": "text/event-stream",
},
});
} catch (e) {
console.error(e);
return new Response(e.message, {
status: 500,
headers: {
"Content-Type": "text/plain",
},
});
}
}
export const config: Config = {
path: "/api/chat",
};