scrimba
AI Engineering
AI Agents
OpenAI Functions Agent - Part 9 - Automatic function calls
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 9 - Automatic function calls
AboutCommentsNotes
OpenAI Functions Agent - Part 9 - Automatic function calls
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
})

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

messages.push(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 functionArgs = JSON.parse(toolCall.function.arguments)
const functionResponse = await functionToCall(functionArgs)
console.log(functionResponse)
messages.push({
tool_call_id: toolCall.id,
role: "tool",
name: functionName,
content: functionResponse
})
}
}
}
}

await agent("What's the current weather in my current location?")

/**
The current weather in New York is sunny with a temperature of 75°F.
*/
Console
"Iteration #1"
,
[
{id:
"call_wvUcpyXfRkgAOPQnAm9UqCbW"
, type:
"function"
, function:
{name:
"getLocation"
, arguments:
"{}"
}
}
]
,
"{"ip":"83.136.182.28","network":"83.136.182.0/24","version":"IPv4","city":"Denver","region":"Colorado","region_code":"CO","country":"US","country_name":"United States","country_code":"US","country_code_iso3":"USA","country_capital":"Washington","country_tld":".us","continent_code":"NA","in_eu":false,"postal":"80202","latitude":39.74946,"longitude":-104.995217,"timezone":"America/Denver","utc_offset":"-0700","country_calling_code":"+1","currency":"USD","currency_name":"Dollar","languages":"en-US,es-US,haw,fr","country_area":9629091,"country_population":327167434,"asn":"AS141039","org":"Packethub s.a."}"
,
"Iteration #2"
,
[
{id:
"call_hrqxtREIXIfooKmBZzKCdBJI"
, type:
"function"
, function:
{name:
"getCurrentWeather"
, arguments:
"{"location":"Denver"}"
}
}
]
,
"{"location":"Denver","temperature":"75","forecast":"sunny"}"
,
"Iteration #3"
,
undefined
,
"The current weather in Denver is sunny with a temperature of 75°F."
,
"AGENT ENDING"
,
/index.html
-9:04