ReAct
ReAct using TavilyΒΆ
In this example, we configure a User Assistant agent with a search tool skill to answer a question using information retrieval. The agent will utilize the search tool to gather information before responding to the user. The workflow involves setting up the agent with specific skills and linking it to the user for interaction.
OverviewΒΆ
The workflow includes:
- User Assistant Agent: Uses a search tool to gather real-time information for answering user queries.
- User Proxy Agent: Acts as a bridge to initiate the interaction.
- Agent Flow: The User Proxy sends a query to the Assistant, which uses the search tool to retrieve information.
To set up the search_tool skill in Tavily, follow these steps before configuring the agents.
Create the Search Tool SkillΒΆ
Go to the Skills tab and click on Add Skill to create a new skill:
- Name:
search_tool
- Description: "Search tool using Tavily AI"
- Skill Content:
```python import os from typing import Annotated from tavily import TavilyClient tavily = TavilyClient(api_key=os.environ["TAVILY_API_KEY"]) def search_tool(query: Annotated[str, "The search query"]) -> Annotated[str, "The search results"]: """Search tool using Tavily AI.""" return tavily.get_search_context(query=query, search_depth="advanced") ```
Environment Variables: Add the necessary environment variable for the Tavily API key.
- Key:
TAVILY_API_KEY
- Value: Enter your Tavily API key. You can get one on the Tavily website.
- Key:
- Name:
Set up the User ProxyΒΆ
- Drag and Drop a new User Proxy agent onto the canvas.
- Configure the User Proxy agent:
- No additional models or skills are required for the User Proxy.
Set up the Assistant AgentΒΆ
- Drag and Drop an Assistant agent onto the canvas.
- Configure the Assistant agent:
- System Message: "Only use the tools you have been provided with. Reply TERMINATE at the end when the task is done."
- Assign the Model: Link a model of your choice to the User Proxy agent. In this example, we use the
claude-3-5-sonnet-20240620
model. - Add Skills:
- Go to the Skills tab and select
search_tool
. - Set the Executor as User to allow manual control over the search process.
- Go to the Skills tab and select
Establish ConnectionsΒΆ
- Link the User Proxy agent to the Assistant agent by dragging a line between them.
- For the message, we use this custom method:
def callable_message(sender, recipient, context): """Complete the message function""" ReAct_prompt = """ Answer the following questions as best you can. You have access to tools provided. Use the following format: Question: the input question you must answer Thought: you should always think about what to do Action: the action to take Action Input: the input to the action Observation: the result of the action ... (this process can repeat multiple times) Thought: I now know the final answer Final Answer: the final answer to the original input question Begin! Question: {input} """ return ReAct_prompt.format(input=context["question"])
- Add to the message context:
- Key:
question
- Value:
What is the result of super bowl 2024?
- Key:
Step 4: Execute and MonitorΒΆ
- Run the flow by pressing the play button.
- In the logs section, observe how the Assistant uses the search tool to retrieve relevant information and respond to the query.
Warning
When running the flow for the first time, you might get an error saying Please install anthropic to use anthropic
. Even though the library is installed (ag2[anthropic]
), you might need to restart the kernel to resolve the issue.
Note
Once the flow starts, you might be prompt before running the search tool. You can just press Enter on the prompt to continue the flow.
Files used in this example:
- Flow: ReAct.waldiez
- Skill: search_tool.waldiezSkill