-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
93 lines (67 loc) · 2.62 KB
/
index.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
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
import { OpenAI } from 'openai'
import { NestorClient } from 'nestor-client'
const nestorClient = new NestorClient({ logger: null })
setTimeout(async () => {
// init
const openai = new OpenAI()
// prompt
//const user_input = 'Given the tools you have access to, what kind of tasks can you do for me? Come up with creative problems you can solve by combining the tools available to you. If you have no tools provided say you can\'t do much.'
const user_input = 'Create a playlist of the songs played at The National concert in Chicago on 24 September 2024'
// build messages
const messages: any[] = [
{ role: 'system', content: 'You are a helpful assistant. You answer questions using tools that are made available to you. If the tools provided do no allow you to answer the question say it without making up things.' },
{ role: 'user', content: user_input }
]
// log
console.log(`PROMPT: ${user_input}\n\nANSWER:`)
// now iterate
while (true) {
// build payload
let payload: OpenAI.ChatCompletionCreateParams = {
model: 'gpt-4o',
messages: messages,
}
// add tools
const tools = await nestorClient.list()
if (tools.length > 0) {
payload.tools = tools
payload.tool_choice = 'auto'
}
// initialize
const completion = await openai.chat.completions.create(payload)
// get completion
const completionResponse = completion.choices[0].message
//console.log(completionResponse)
// check tool calls
if (completionResponse.tool_calls) {
const results: any[] = []
for (const toolCall of completionResponse.tool_calls) {
// get attrs
const id = toolCall.id
let name = toolCall.function.name
const parameters = JSON.parse(toolCall.function.arguments)
// sometime the llm prefixes the name with 'function' or 'functions'
name = name.replace(/^functions?/, '')
// call the tool
console.log(` - Calling tool ${name}...`)
const response = await nestorClient.call(name, parameters)
//console.log('-> Got', JSON.stringify(response))
// build message
results.push({
role: 'tool',
name: name,
content: JSON.stringify(response),
tool_call_id: id
})
}
// now add messages to the queue
messages.push({ role: 'assistant', tool_calls: completionResponse.tool_calls })
messages.push(...results)
} else {
// we should be done!
const completion_text = completion.choices[0].message.content
console.log(completion_text)
process.exit(0)
}
}
}, 1000)