-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworker.js
More file actions
87 lines (76 loc) · 2.43 KB
/
Copy pathworker.js
File metadata and controls
87 lines (76 loc) · 2.43 KB
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
import OpenAI from 'openai';
import cheerio from 'cheerio';
async function read_website_content(url) {
console.log('reading website content');
const response = await fetch(url);
const body = await response.text();
let cheerioBody = await cheerio.load(body);
const resp = {
website_body: cheerioBody('p').text(),
url: url,
};
return JSON.stringify(resp);
}
export default {
async fetch(request, env, ctx) {
const openai = new OpenAI({
apiKey: env.OPENAI_API_KEY,
});
try {
const url = new URL(request.url);
const message = url.searchParams.get('message');
const messages = [{ role: 'user', content: message ? message : "What's in the news today?" }];
const tools = [
{
type: 'function',
function: {
name: 'read_website_content',
description: 'Read the content on a given website',
parameters: {
type: 'object',
properties: {
url: {
type: 'string',
description: 'The URL to the website to read',
},
},
required: ['url'],
},
},
},
];
const chatCompletion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo-1106',
messages: messages,
tools: tools,
tool_choice: 'auto',
});
const assistantMessage = chatCompletion.choices[0].message;
messages.push(assistantMessage);
if (assistantMessage.tool_calls) {
for (const toolCall of assistantMessage.tool_calls) {
if (toolCall.function.name === 'read_website_content') {
const url = JSON.parse(toolCall.function.arguments).url;
const websiteContent = await read_website_content(url);
messages.push({
role: 'tool',
tool_call_id: toolCall.id,
name: toolCall.function.name,
content: websiteContent,
});
}
}
const secondChatCompletion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo-1106',
messages: messages,
});
console.log(messages);
return new Response(secondChatCompletion.choices[0].message.content);
} else {
return new Response(assistantMessage.content);
}
} catch (e) {
return new Response(e.message, { status: 500 });
}
},
};