scrimba
Learn AI Agents
ReAct Agent - part 4 - code setup
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

ReAct Agent - part 4 - code setup
AboutCommentsNotes
ReAct Agent - part 4 - code setup
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
})

/**
* Goal - build an agent that can answer any questions that might require knowledge about my current location and the current weather at my location.
*/

/**
PLAN:
1. Design a well-written ReAct prompt
2. Build a loop for my agent to run in.
3. Parse any actions that the LLM determines are necessary
4. End condition - final Answer is given

*/

const systemPrompt = `
You cycle through Thought, Action, PAUSE, Observation. At the end of the loop you output a final Answer. Your final answer should be highly specific to the observations you have from running
the actions.
1. Thought: Describe your thoughts about the question you have been asked.
2. Action: run one of the actions available to you - then return PAUSE.
3. PAUSE
4. Observation: will be the result of running those actions.

Available actions:
- getCurrentWeather:
E.g. getCurrentWeather: Salt Lake City
Returns the current weather of the location specified.
- getLocation:
E.g. getLocation: null
Returns user's location details. No arguments needed.

Example session:
Question: Please give me some ideas for activities to do this afternoon.
Thought: I should look up the user's location so I can give location-specific activity ideas.
Action: getLocation: null
PAUSE

You will be called again with something like this:
Observation: "New York City, NY"

Then you loop again:
Thought: To get even more specific activity ideas, I should get the current weather at the user's location.
Action: getCurrentWeather: New York City
PAUSE

You'll then be called again with something like this:
Observation: { location: "New York City, NY", forecast: ["sunny"] }

You then output:
Answer: <Suggested activities based on sunny weather that are highly specific to New York City and surrounding areas.>
`

/**
* Challenge: Set up the function
* 1. Create a function called `agent` that takes a `query` as a parameter
* 2. Create a messages array that follows the pattern openai expects for
* its chat completions endpoint. The first message should be the system
* prompt we wrote above, and the second message should be the query
* from the user found in the `agent` function parameter.
* 3. Move the code below inside the function (and uncomment it)
* 4. Call the function with a string query of any kind and see what gets returned.
*/




// const response = await openai.chat.completions.create({
// model: "gpt-3.5-turbo",
// messages: [
// {
// role: "user",
// content: `Give me a list of activity ideas based on my current location of ${location} and weather of ${weather}`
// }
// ]
// })

// console.log(response.choices[0].message.content)
Console
"1. Visit Salt Lake City Public Library: Take advantage of the sunny weather to walk around this architectural masterpiece and the vibrant community gathering place. 2. Explore Antelope Island State Park: Watch wild bison and hike the trails. It's just the perfect day with 72°F to explore the natural wonders of the area. 3. Head to Sugar House Park: You can run, walk, or jog around the area. Don't forget to have a picnic under the sun. 4. Visit Temple Square: Rich with history, architecture, and stunning landscaped gardens, Temple Square is an iconic location in the city. 5. Stroll around Red Butte Garden: Enjoy the mild weather in this botanical garden, arboretum, and amphitheater. 6. Enjoy the Outdoors at Liberty Park: Perfect weather to rent a paddle boat, visit the aviary or even just have a barbeque. 7. Visit the Natural History Museum of Utah: If you feel like taking a break from the sunshine, step inside this museum to learn about Utah's natural history. 8. Hiking Big Cottonwood Canyon: Keep your water bottle and sunscreen handy and enjoy the hike under the clear skies. 9. Bike the Jordan River Parkway Trail: Enjoy a leisurely cycle along this scenic path. 10. Salt Lake City Farmer's Market: Take advantage of the beautiful weather to purchase some local produce or handmade goods and enjoy local live performances. 11. Take a Ride on the Salt Lake Trolley: Sightsee Salt Lake City's attractions under the comfortable weather. 12. Explore Bonneville Shoreline Trail: Perfect for biking, hiking or running with a beautiful view over the surroundings. 13. Visit Hogle Zoo: A sunny and warm day is perfect to explore more than 800 animals from all over the world. 14. Golfing: With the sunny weather, why not go out and play some Golf in any of the immaculate courses around the city. 15. Paddleboarding or Kayaking in Great Salt Lake: Make the most of this perfect weather by enjoying water sports in the Great Salt Lake. Make sure to follow any local guidelines or restrictions due to the ongoing pandemic situation. Enjoy your day in Salt Lake City!"
,
/index.html
-3:45