async function agent(query) {
const messages = [
{ role: "system", content: systemPrompt },
{ role: "user", content: query }
]
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages
})
const responseText = response.choices[0].message.content
messages.push({ role: "assistant", content: responseText })
const responseLines = responseText.split("\n")
console.log(responseLines)
const actionRegex = /^Action: (\w+): (.*)$/
const foundActionStr = responseLines.find(str => actionRegex.test(str))
if (foundActionStr) {
const actions = actionRegex["exec"](foundActionStr)
const [_, action, actionArg] = actions
if (!availableFunctions.hasOwnProperty(action)) {
throw new Error(`Unknown action: ${action}: ${actionArg}`)
}
const observation = await availableFunctions[action](actionArg)
message.push({ role: "assistant", content: `Observation: ${observation}` })
}
}
agent("What is the current weather in New York City?")