Find answers from the community

Updated 3 months ago

Extracting thoughts actions and observations from react agent for frontend use

Hi all, I have a newbie question regarding the ReAct agent: how can I extract the thoughts/actions/action input/observations such that I could pass them to a frontend?
Plain Text
Thought: The current language of the user is English. I need to use a tool to help me answer the question.
Action: sqrt
Action Input: {'x': 123}
Observation: 11.090536506409418
> Running step 3ccbf375-d226-4a50-b7d9-1d7330595763. Step input: None
Thought: I can answer without using any more tools. I'll use the user's language to answer.
Answer: The square root of 123 is approximately 11.0905.
The square root of 123 is approximately 11.0905.

In other words, how can I access "Thought: The current language of the user is English. I need to use a tool to help me answer the question." and store it in a variable? Any help/pointers highly appreciated!
T
L
3 comments
Here is some code that I used:
Plain Text
from llama_index.core.agent import ReActAgent
from llama_index.llms.openai import OpenAI
from llama_index.core.tools import FunctionTool
import math

def sqrt(x: float) -> float:
    """Calculate the square root of a number"""
    return math.sqrt(x)

sqrt_tool = FunctionTool.from_defaults(fn=sqrt)
llm = OpenAI(model="gpt-4o")
agent = ReActAgent.from_tools([sqrt_tool], llm=llm, verbose=True)

response = agent.chat("What is sqrt(123)?")
print(response)

The variable response only contains the final output of the LLM.
@Logan M Awesome! Thank you so much! 😍
Add a reply
Sign up and join the conversation on Discord