scrimba
AI Engineering
AI Agents
ReAct Agent - part 5 - Plan for parsing the response
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 5 - Plan for parsing the response
AboutCommentsNotes
ReAct Agent - part 5 - Plan for parsing the response
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.>
`

async function agent(query) {

const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: query }
]
})

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

agent("What book should I read next? I like self-help books.")
Console
"Thought: To recommend a self-help book, I need to gather more information about the user's preferences and current location. Action: getLocation: null PAUSE"
,
/index.html
-3:53