-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.js
170 lines (117 loc) · 5.57 KB
/
route.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import openai from '@/service/openai'
import response_format from '@/assets/response_format.json'
import get_weather from '@/assets/get_weather.json'
import get_events from '@/assets/get_events.json'
import { mockApiCall } from '@/lib/mockapi'
export async function POST(request) {
let { message, context } = await request.json()
console.log('message', message)
let system_prompt = `You are a helpful assistant.\n` +
`Today is ${new Date()}.`
let messages = [
{ role: "system", content: system_prompt }
]
if(context.length > 0) {
messages = [...messages, ...context]
}
messages.push({ role: "user", content: message })
let tools = [get_weather, get_events]
return new Response(new ReadableStream({
async pull(controller) {
let is_completed = false
let tool_calls = []
let tool_content = ''
console.log('stream start', (new Date()).toLocaleTimeString())
do {
let message_items = messages
// Process the tools invoked
if (Object.keys(tool_calls).length > 0) {
let tool_message = { role: 'assistant', content: tool_content || null, tool_calls: [] }
let tool_outputs = []
for(let i = 0; i < tool_calls.length; i++) {
let tool_id = tool_calls[i].id
try {
tool_message.tool_calls.push({
id: tool_id,
type: 'function',
function: tool_calls[i].function
})
const tool_name = tool_calls[i].function.name
const tool_args = JSON.parse(tool_calls[i].function.arguments)
let tool_output = { status: 'error', name: tool_name, message: 'tool not found' }
try {
const response = await mockApiCall(tool_name, tool_args)
tool_output = { ...tool_args, ...response.data }
} catch(e) {
console.log(e.message)
}
tool_outputs.push({
tool_call_id: tool_id,
role: 'tool',
name: tool_name,
content: JSON.stringify(tool_output)
})
} catch(e) {
console.log('error-tool', e.message)
}
}
message_items.push(tool_message)
tool_outputs.forEach((tool_output) => {
message_items.push(tool_output)
})
console.log(tool_message)
console.log(tool_outputs)
}
tool_calls = []
tool_content = ''
try {
const stream = await openai.chat.completions.create({
model: "gpt-4o-mini-2024-07-18",
messages: message_items,
tools: tools,
response_format: response_format,
stream: true,
temperature: 0.9,//0.2
})
for await (const chunk of stream) {
if(chunk.choices.length > 0) {
if(chunk.choices[0].delta.content) {
tool_content += chunk.choices[0].delta.content
controller.enqueue(chunk.choices[0].delta.content)
}
if(chunk.choices[0].delta.tool_calls) {
chunk.choices[0].delta.tool_calls.forEach((tool) => {
if(tool_calls[tool.index]) {
tool_calls[tool.index].function.arguments += tool.function.arguments
} else {
tool_calls[tool.index] = {
id: tool.id,
function: tool.function
}
}
})
}
if(chunk.choices[0].finish_reason) {
console.log('finished_reason', chunk.choices[0].finish_reason)
}
}
}
// Exit loop if no tools invoked.
if(tool_calls.length === 0) {
is_completed = true
}
} catch(e) {
console.log(e.message)
is_completed = true
}
} while(!is_completed)
console.log('stream close', (new Date()).toLocaleTimeString())
controller.close()
}
}), {
status: 200,
headers: {
'Content-Type': 'text/event-stream'
}
})
}