-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat.js
More file actions
223 lines (208 loc) · 6.84 KB
/
chat.js
File metadata and controls
223 lines (208 loc) · 6.84 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { tools } from './tools.js'
/** @type {'text' | 'tool'} */
let outputMode = 'text' // default output mode
function systemPrompt() {
return 'You are a machine learning web application named "Hyperparam" running on a CLI terminal.'
+ '\nYou assist users with analyzing and exploring datasets, particularly in parquet format.'
+ ' The website and api are available at hyperparam.app.'
+ ' The Hyperparam CLI tool can list and explore local parquet files.'
+ '\nYou are on a terminal and can only output: text, emojis, terminal colors, and terminal formatting.'
+ ' Don\'t add additional markdown or html formatting unless requested.'
+ (process.stdout.isTTY ? ` The terminal width is ${process.stdout.columns} characters.` : '')
}
/** @type {Message} */
const systemMessage = { role: 'system', content: systemPrompt() }
const colors = {
system: '\x1b[36m', // cyan
user: '\x1b[33m', // yellow
tool: '\x1b[90m', // gray
error: '\x1b[31m', // red
normal: '\x1b[0m', // reset
}
/**
* @import { Message } from './types.d.ts'
* @param {Object} chatInput
* @returns {Promise<Message>}
*/
async function sendToServer(chatInput) {
const response = await fetch('https://hyperparam.app/api/functions/openai/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(chatInput),
})
if (!response.ok) {
throw new Error(`Request failed: ${response.status}`)
}
// Process the streaming response
/** @type {Message} */
const streamResponse = { role: 'assistant', content: '' }
const reader = response.body?.getReader()
if (!reader) throw new Error('No response body')
const decoder = new TextDecoder()
let buffer = ''
const write = writeWithColor()
while (true) {
const { done, value } = await reader.read()
if (done) break
buffer += decoder.decode(value, { stream: true })
const lines = buffer.split('\n')
// Keep the last line in the buffer
buffer = lines.pop() || ''
for (const line of lines) {
if (!line.trim()) continue
try {
const chunk = JSON.parse(line)
const { type, error } = chunk
if (type === 'response.output_text.delta') {
// text mode
if (outputMode === 'tool') {
write('\n')
}
outputMode = 'text'
streamResponse.content += chunk.delta
write(chunk.delta)
} else if (error) {
console.error(error)
throw new Error(error)
} else if (chunk.function) {
streamResponse.tool_calls ??= []
streamResponse.tool_calls.push(chunk)
} else if (!chunk.key) {
console.log('Unknown chunk', chunk)
}
} catch (err) {
console.error('Error parsing chunk', err)
}
}
}
return streamResponse
}
/**
* Send messages to the server and handle tool calls.
* Will mutate the messages array!
*
* @import { ToolCall, ToolHandler } from './types.d.ts'
* @param {Message[]} messages
* @returns {Promise<void>}
*/
async function sendMessages(messages) {
const chatInput = {
model: 'gpt-4o',
messages,
tools: tools.map(tool => tool.tool),
}
const response = await sendToServer(chatInput)
messages.push(response)
// handle tool results
if (response.tool_calls?.length) {
/** @type {{ toolCall: ToolCall, tool: ToolHandler, result: Promise<string> }[]} */
const toolResults = []
for (const toolCall of response.tool_calls) {
const tool = tools.find(tool => tool.tool.function.name === toolCall.function.name)
if (tool) {
const args = JSON.parse(toolCall.function?.arguments ?? '{}')
const result = tool.handleToolCall(args)
toolResults.push({ toolCall, tool, result })
} else {
throw new Error(`Unknown tool: ${toolCall.function.name}`)
}
}
// tool mode
if (outputMode === 'text') {
write('\n')
}
outputMode = 'tool' // switch to tool output mode
for (const toolResult of toolResults) {
const { toolCall, tool } = toolResult
try {
const content = await toolResult.result
// Construct function call message
const args = JSON.parse(toolCall.function?.arguments ?? '{}')
const entries = Object.entries(args)
let func = toolCall.function.name
if (entries.length === 0) {
func += '()'
} else {
// transform to (arg1 = 111, arg2 = 222)
const pairs = entries.map(([key, value]) => `${key} = ${value}`)
func += `(${pairs.join(', ')})`
}
write(colors.tool, `${tool.emoji} ${func}`, colors.normal, '\n')
messages.push({ role: 'tool', content, tool_call_id: toolCall.id })
} catch (error) {
write(colors.error, `\nError calling tool ${toolCall.function.name}: ${error.message}`, colors.normal)
messages.push({ role: 'tool', content: `Error calling tool ${toolCall.function.name}: ${error.message}`, tool_call_id: toolCall.id })
}
}
// send messages with tool results
await sendMessages(messages)
}
}
/**
* @param {string[]} args
*/
function write(...args) {
args.forEach(s => process.stdout.write(s))
}
/**
* Handle streaming output, but buffer if needed to handle escape codes.
* @returns {(...args: string[]) => void}
*/
function writeWithColor() {
/** @type {string | undefined} */
let buffer
/**
* @param {string} char
*/
function writeChar(char) {
if (buffer === undefined && char !== '\\' && char !== '\x1b') {
write(char)
} else {
buffer ??= ''
buffer += char
const isEscape = buffer.startsWith('\\x1b[') || buffer.startsWith('\\033[')
// if the buffer is an escape sequence, write it
if (isEscape) {
// convert to terminal escape sequence
const escaped = buffer.replace(/\\x1b\[/g, '\x1b[').replace(/\\033\[/g, '\x1b[')
write(escaped)
buffer = undefined
} else if (buffer.length > 6) {
// no match, just write it
write(buffer)
buffer = undefined
}
}
}
return (...args) => {
for (const arg of args) {
for (const char of arg) {
writeChar(char)
}
}
}
}
export function chat() {
/** @type {Message[]} */
const messages = [systemMessage]
process.stdin.setEncoding('utf-8')
write(colors.system, 'question: ', colors.normal)
process.stdin.on('data', async (/** @type {string} */ input) => {
input = input.trim()
if (input === 'exit') {
process.exit()
} else if (input) {
try {
write(colors.user, 'answer: ', colors.normal)
outputMode = 'text' // switch to text output mode
messages.push({ role: 'user', content: input.trim() })
await sendMessages(messages)
} catch (error) {
console.error(colors.error, '\n' + error)
} finally {
write('\n\n')
}
}
write(colors.system, 'question: ', colors.normal)
})
}