Public
HTTP
willthereader-openaidefiner.web.val.run
September 1, 2024
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { OpenAI } from "https://esm.town/v/std/openai";
const openai = new OpenAI();
export default async function(req: Request): Promise<Response> {
log.info("Request received:", req.method, req.url);
try {
if (req.method === "OPTIONS") {
return handleCorsPreflightRequest();
}
const selection = await parseRequest(req);
log.info("Parsed selection:", selection);
const messages = prepareMessages(selection);
log.info("Prepared messages for OpenAI");
const openai = new OpenAI();
const stream = await openai.chat.completions.create({
messages,
model: config.MODEL_NAME,
max_tokens: config.MAX_TOKENS,
temperature: config.TEMPERATURE,
stream: true,
});
// log.info("OpenAI stream created");
return streamResponse(stream);
} catch (error) {
return handleError(error);
}
}
export async function parseRequest(req: Request): Promise<string> {
const body = await req.text();
log.debug("Request body:", body);
const parsedBody = JSON.parse(body);
const selection = parsedBody.selection;
if (!selection || selection.trim() === "") {
throw new Error("Empty or invalid 'selection' in request body");
}
return selection;
}
export function prepareMessages(selection: string): Array<Object> {
return [
{
role: "system",
content:
`${config.SYSTEM_MESSAGE} When encountering HTML img tags with class "smilie", interpret them as emojis and include their meaning in your response.`,
},
{
role: "user",
content: `Define in detail: "${selection}".`,
},
];
}
export function streamResponse(stream: ReadableStream): Response {
const encoder = new TextEncoder();
const readableStream = new ReadableStream({
async start(controller) {
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || "";
if (content) {
// log.debug("Received chunk from OpenAI:", content);
const encodedChunk = encoder.encode(JSON.stringify({ chunk: content }) + "\n");
controller.enqueue(encodedChunk);
}
}
controller.close();
},
});
return new Response(readableStream, {
headers: {
"Content-Type": "application/json; charset=utf-8",
"Access-Control-Allow-Origin": "*",
"Transfer-Encoding": "chunked",
},
});
}
export function handleError(error: Error): Response {
log.error("Error occurred:", error);
const errorMessage = "An error occurred while processing your request. Please try again.";
const errorResponse = JSON.stringify({ error: errorMessage });
return new Response(errorResponse, {
status: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
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!
September 1, 2024