scrimba
AI Engineering
AI Agents
OpenAI Functions Agent - Part 7 - Pushing to messages
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

OpenAI Functions Agent - Part 7 - Pushing to messages
AboutCommentsNotes
OpenAI Functions Agent - Part 7 - Pushing to messages
Expand for more info
index.js
run
preview
console
import OpenAI from "openai"
import { getCurrentWeather, getLocation, tools } from "./tools"

export const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
dangerouslyAllowBrowser: true
})

const availableFunctions = {
getCurrentWeather,
getLocation
}

async function agent(query) {
const messages = [
{ role: "system", content: "You are a helpful AI agent. Give highly specific answers based on the information you're provided. Prefer to gather information with the tools provided to you rather than giving basic, generic answers." },
{ role: "user", content: query }
]

const MAX_ITERATIONS = 5

// for (let i = 0; i < MAX_ITERATIONS; i++) {
// console.log(`Iteration #${i + 1}`)
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo-1106",
messages,
tools
})

console.log(response.choices[0])
const { finish_reason: finishReason, message } = response.choices[0]
const { tool_calls: toolCalls } = message

if (finishReason === "stop") {
console.log(message.content)
console.log("AGENT ENDING")
return
} else if (finishReason === "tool_calls") {
for (const toolCall of toolCalls) {
const functionName = toolCall.function.name
const functionToCall = availableFunctions[functionName]
const functionResponse = await functionToCall()
console.log(functionResponse)

}
}

// }
}

await agent("What's the current weather in Tokyo and New York City and Oslo?")

/**
{
"index": 0,
"message": {
"role": "assistant",
"content": "As an AI, I don't have feelings, but I'm here to assist you with any questions or tasks you have. How can I help you today?"
},
"finish_reason": "stop"
}


{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_SDhXnJbvxSWwy1m1R1J43EmQ",
"type": "function",
"function": {
"name": "getLocation",
"arguments": "{}"
}
}
]
},
"finish_reason": "tool_calls"
}
*/
Console
{index:
0
, message:
{role:
"assistant"
, content:
null
, tool_calls:
[
{id:
"call_1NUFb20d2KZ5xEpSCs75u4Lp"
, type:
"function"
, function:
{name:
"getCurrentWeather"
, arguments:
"{"city": "Tokyo"}"
}
}
,
{id:
"call_adFx23FXai6jSm9JDheIppA2"
, type:
"function"
, function:
{name:
"getCurrentWeather"
, arguments:
"{"city": "New York City"}"
}
}
,
{id:
"call_tbKHfgdw47WjuZFzULsiDNrb"
, type:
"function"
, function:
{name:
"getCurrentWeather"
, arguments:
"{"city": "Oslo"}"
}
}
]
}
, finish_reason:
"tool_calls"
}
,
"{"temperature":"75","unit":"F","forecast":"sunny"}"
,
"{"temperature":"75","unit":"F","forecast":"sunny"}"
,
"{"temperature":"75","unit":"F","forecast":"sunny"}"
,
/index.html
-4:22