Skip to main content
In this guide, we’ll extend a LangChain-based agent with Python REPL functionality. By combining the Tavily Search Results tool for information lookup with a Python REPL tool for on-the-fly calculations, your agent can handle both data retrieval and computation seamlessly.
Make sure you have Python 3.8+ installed and the necessary packages (langchain, langchain-experimental, langchain-openai, langchain-community) available in your environment.

Prerequisites

  • Tavily API key stored in the TAVILY_API_KEY environment variable
  • Access to OpenAI’s Chat API via langchain-openai
Without a valid TAVILY_API_KEY, the search tool will fail to retrieve tournament data. Use export TAVILY_API_KEY=your_api_key to configure it.

1. Import Modules and Initialize Tools

2. Build the Chat Prompt Template

3. Initialize the Agent Executor

4. Example Queries

Below are common examples demonstrating search and computation capabilities.
  1. Tournament Schedule
  2. Hosting Countries
  3. Date Calculation

Under the Hood: Python REPL Execution

This code prints:
And the agent returns:
There are 31 days left before the first match of the ICC Men’s T20 World Cup 2024 starts on June 1, 2024.
By combining the Tavily search tool for tournament data and the Python REPL tool for on-the-fly math, the agent can answer complex queries accurately and interactively.
Stay tuned for more demos and advanced capabilities in upcoming articles!

Watch Video

Practice Lab