scrimba
AI Engineering
AI Agents
OpenAI Functions Agent - part 3 - Tools
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 3 - Tools
AboutCommentsNotes
OpenAI Functions Agent - part 3 - Tools
Expand for more info
index.js
run
preview
console
import OpenAI from "openai"
import { getCurrentWeather, getLocation } 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",
messages,
tools: []
})

const responseText = response.choices[0].message.content
console.log(responseText)
}
}

// console.log(await agent("What are some activity ideas that I can do this afternoon based on my location and weather?"))
Console
"Iteration #1"
,
"Thought: To give specific activity ideas, I need to get the user's location and current weather. Action: getLocation: null PAUSE"
,
"Calling function getLocation with argument null"
,
"Iteration #2"
,
"Thought: Now that I have the user's location, I can get the current weather in New York City. Action: getCurrentWeather: New York City, NY PAUSE"
,
"Calling function getCurrentWeather with argument New York City, NY"
,
"Iteration #3"
,
"Answer: Based on the current snowy weather in New York City, here are some activity ideas for this afternoon: 1. Build a snowman in Central Park. 2. Go ice skating at Rockefeller Center. 3. Have a hot chocolate tasting at a cozy café. 4. Visit a museum or art gallery. 5. Stay indoors and watch a movie marathon. Remember to dress warmly and stay safe while enjoying these activities!"
,
"Agent finished with task"
,
"Answer: Based on the current snowy weather in New York City, here are some activity ideas for this afternoon: 1. Build a snowman in Central Park. 2. Go ice skating at Rockefeller Center. 3. Have a hot chocolate tasting at a cozy café. 4. Visit a museum or art gallery. 5. Stay indoors and watch a movie marathon. Remember to dress warmly and stay safe while enjoying these activities!"
,
/index.html
-6:28