Skip to content

This project combines two functionalities: a Code Interpreter using LLM Agent Orchestration and Tool Utilization, and a ReAct LangChain Agent example.ReAct agent using LangChain, integrated with a custom tool to calculate the length of a given text. It serves as a foundational example for building more complex, tool-augmented LLM applications.

Notifications You must be signed in to change notification settings

junfanz1/Code-Interpreter-ReAct-LangChain-Agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project combines two functionalities: a Code Interpreter using LLM Agent Orchestration and Tool Utilization, and a ReAct LangChain Agent example.

Star History

Star History Chart

Contents

ReAct LangChain Agent

This repository demonstrates the creation of a simple ReAct agent using LangChain, integrated with a custom tool to calculate the length of a given text. It serves as a foundational example for building more complex, tool-augmented LLM applications.

Project Purpose

The primary goal of this project is to provide a practical, hands-on example of building an agent using LangChain's ReAct framework. It aims to demystify the process and serve as a learning resource for developers looking to understand how to:

  • Build custom tools for LangChain agents to extend their capabilities.
  • Implement the ReAct pattern to enable agents to reason and act in a structured manner.
  • Integrate LangChain with OpenAI's powerful language models for natural language processing.
  • Manage agent interactions through an iterative loop, handling actions and observations effectively.
  • Develop a robust output parser to interpret and process various agent responses, including actions and final answers.
  • Securely manage API keys using environment variables.

This project is intended to be a stepping stone for building more complex and sophisticated agents capable of performing a wide range of tasks by interacting with tools and external data sources.

Example Input:

What is the length of the word: DOG

Example Prompt Template:

template = """
    Answer the following questions as best you can. You have access to the following tools:

    {tools}

    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, should be one of [{tool_names}]
    Action Input: the input to the action
    Observation: the result of the action
    ... (this Thought/Action/Action Input/Observation can repeat N times)
    Thought: I now know the final answer
    Final Answer: the final answer to the original input question

    Begin!

    Question: {input}
    Thought: {agent_scratchpad}
    """

Example Output (with intermediate CoT steps):

Hello ReAct LangChain!
***Prompt to LLM was:***
Human: 
    Answer the following questions as best you can. You have access to the following tools:

    get_text_length(text: str) -> int - Returns the length of a text by characters

    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, should be one of [get_text_length]
    Action Input: the input to the action
    Observation: the result of the action
    ... (this Thought/Action/Action Input/Observation can repeat N times)
    Thought: I now know the final answer
    Final Answer: the final answer to the original input question

    Begin!

    Question: What is the length of the word: DOG
    Thought: 
    
******
***LLM Response:***
I should use the get_text_length function to determine the length of the word "DOG".
    Action: get_text_length
    Action Input: "DOG"
    
******
tool='get_text_length' tool_input='DOG"\n' log='I should use the get_text_length function to determine the length of the word "DOG".\n    Action: get_text_length\n    Action Input: "DOG"\n    '
get_text_length enter with text='DOG"\n'
observation=3
***Prompt to LLM was:***
Human: 
    Answer the following questions as best you can. You have access to the following tools:

    get_text_length(text: str) -> int - Returns the length of a text by characters

    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, should be one of [get_text_length]
    Action Input: the input to the action
    Observation: the result of the action
    ... (this Thought/Action/Action Input/Observation can repeat N times)
    Thought: I now know the final answer
    Final Answer: the final answer to the original input question

    Begin!

    Question: What is the length of the word: DOG
    Thought: I should use the get_text_length function to determine the length of the word "DOG".
    Action: get_text_length
    Action Input: "DOG"
    
Observation: 3
Thought: 
    
******
***LLM Response:***
I now know the final answer
Final Answer: 3
******
return_values={'output': '3'} log='I now know the final answer\nFinal Answer: 3'
{'output': '3'}

Process finished with exit code 0

LLM Technology Stack

This project leverages the following LLM technologies:

  • LangChain: A framework for developing LLM-powered applications, providing abstractions for agents, chains, and tools.
  • OpenAI ChatOpenAI: Used as the LLM for reasoning and generating responses, leveraging its chat-based capabilities and natural language understanding.
  • ReAct Pattern: Implemented to enable the agent to interleave reasoning (thoughts) and actions, improving its ability to solve complex tasks.
  • Custom Tools: Python functions wrapped as LangChain tools, allowing the agent to interact with external functionality (e.g., get_text_length).
  • Prompt Engineering: Carefully designed prompts to guide the agent's behavior and ensure it adheres to the ReAct pattern.
  • python-dotenv: Used to manage environment variables, keeping sensitive API keys secure.
  • Regular Expressions: Used in the Custom Output Parser to reliably parse the LLM output.

Challenges and Difficulties

During the development of this project, several challenges were encountered:

  • Output Parsing Complexity: Reliably parsing the LLM's output to extract actions, action inputs, and final answers required a custom parser (ReActOutputParser) using regular expressions to handle variations in LLM responses.
  • Agent Interaction Loop Management: Implementing the loop to manage agent actions and observations required careful state management and handling of intermediate steps.
  • LangChain Evolution: Keeping up with the rapid evolution of LangChain, including API changes and deprecations, required continuous learning and adaptation.
  • Error Handling: Implementing robust error handling to gracefully handle unexpected LLM outputs or tool execution failures was essential for stability.
  • ReAct Pattern Implementation: Correctly implementing the ReAct pattern and debugging the agent's reasoning process required a deep understanding of the pattern and careful testing.
  • Handling AgentFinish and AgentAction: Distinguishing between the Agent's decision to use a tool, and the Agent's decision to provide a final answer was a complex parsing challenge.

Future Business Impact and Further Improvements

This project, while simple, demonstrates the foundational concepts of building LLM-powered agents, which have significant potential for commercial applications. Potential impacts and improvements include:

  • Automated Customer Support: Agents can be developed to handle complex customer inquiries, providing personalized support and troubleshooting.
  • Data Analysis and Reporting: Agents can automate data analysis tasks, generating reports and extracting insights from large datasets.
  • Task Automation: Agents can automate repetitive tasks, such as scheduling, email management, and document processing, increasing efficiency.
  • Personalized Assistants: Agents can create personalized assistants that can help users with various tasks, such as managing calendars, making reservations, and providing information.
  • Code Generation and Debugging: Agents can assist developers in writing and debugging code, automating parts of the software development process.
  • Content Creation: Agents can generate content for marketing, social media, and other purposes, freeing up human resources.
  • Enhanced Output Parsing: Implement more robust output parsing to handle a wider range of LLM responses and edge cases.
  • Tool Expansion: Integrate more tools to expand the agent's capabilities and enable it to perform a wider range of tasks.
  • Web Interface Integration: Develop a web interface to provide a more interactive and user-friendly experience.
  • Improved Error Handling and Logging: Implement more comprehensive error handling and logging to improve stability and debugging.
  • Unit and Integration Testing: Add unit and integration tests to ensure the reliability and maintainability of the agent.
  • Contextual Memory: Implement a form of memory to allow the agent to retain context across multiple interactions.
  • Agent Orchestration: Expand to agent orchestration, allowing multiple agents to coordinate and solve complex problems.

Target Audience and Benefits

This project is primarily targeted at:

  • Developers: Those interested in learning how to build LLM-powered applications using LangChain and OpenAI. They can use this as a starting point for more complex projects.
  • AI/ML Enthusiasts: Individuals who want to explore the practical applications of LLMs and agents.
  • Students and Researchers: Those studying AI and natural language processing, looking for hands-on examples.
  • Businesses: Organizations looking to explore how LLMs can automate tasks and improve efficiency.

Benefits:

  • Provides a clear and concise example of building a ReAct agent.
  • Offers practical insights into using LangChain and OpenAI for agent development.
  • Demonstrates how to create custom tools and manage agent interactions.
  • Provides a foundation for building more complex LLM-powered applications.
  • Shows how to secure API keys using environment variables.

Advantages and Disadvantages

Advantages:

  • Educational Value: Serves as an excellent learning resource for understanding LangChain agents and the ReAct pattern.
  • Customization: Demonstrates how to create custom tools and tailor the agent's behavior.
  • Practical Example: Provides a working example that can be easily adapted for other use cases.
  • Robust Output Parsing: The custom parser ensures reliable handling of LLM outputs.

Disadvantages:

  • Simplicity: The agent's functionality is limited to a single tool, which may not be representative of real-world applications.
  • Lack of Memory: The agent does not retain context across multiple interactions.
  • Reliance on OpenAI API: Requires an OpenAI API key, which may incur costs for high usage.
  • Limited Error Handling: While some error handling is implemented, it could be further improved.

Tradeoffs

In this project, several tradeoffs were made to balance simplicity and functionality:

  • Single Tool Implementation: The project focuses on demonstrating the core concepts of ReAct agents using a single, simple tool. This simplifies the code and makes it easier to understand, but limits the agent's capabilities.
  • Basic Prompt Design: The prompt template is relatively simple to focus on the core logic of the agent. In a real-world scenario, more complex and nuanced prompts might be necessary.
  • Minimal Error Handling: While basic error handling is implemented, the project prioritizes clarity over robustness. More comprehensive error handling would add complexity.
  • No Contextual Memory: The agent does not retain context across interactions. This simplifies the project, but real-world agents would likely require some form of memory.

These tradeoffs were made to ensure the project remains accessible and understandable for beginners while still demonstrating the essential concepts of ReAct agents.

Highlight and Summary

This project provides a valuable introduction to building ReAct agents using LangChain and OpenAI. It demonstrates how to create custom tools, implement the ReAct pattern, and manage agent interactions. The custom output parser ensures reliable handling of LLM outputs. While the project is simple, it serves as a solid foundation for building more complex and sophisticated LLM-powered applications.

Future Enhancements

If this project were to be redone, several enhancements could be made:

  • Integration with More Tools: Expand the agent's capabilities by integrating more tools, such as web search, database access, and API interactions.
  • Contextual Memory Implementation: Implement a form of memory to allow the agent to retain context across multiple interactions.
  • Improved Error Handling and Logging: Add more robust error handling and logging to improve stability and debugging.
  • Web Interface Development: Create a web interface to provide a more interactive and user-friendly experience.
  • Agent Orchestration: Explore agent orchestration, allowing multiple agents to coordinate and solve complex problems.
  • Advanced Prompt Engineering: Experiment with more complex and nuanced prompts to improve the agent's reasoning and action execution.
  • Unit and Integration Testing: Add comprehensive unit and integration tests to ensure the reliability and maintainability of the agent.
  • Dockerization: Package the application in a Docker container for easier deployment and portability.
  • Configuration Management: Implement a configuration management system to allow for easier customization and deployment.

Prerequisites

Before running the project, ensure you have the following installed:

  • Python 3.9+
  • Poetry (recommended for dependency management) or pip
  • OpenAI API key

Setup

  1. Clone the Repository:

    git clone https://github.com/junfanz1/react-langchain.git
    cd react-langchain
  2. Create a Virtual Environment (Recommended):

    If you are using poetry:

    poetry install
    poetry shell

    If you are using pip:

    python3 -m venv .venv
    source .venv/bin/activate  # On macOS/Linux
    .venv\Scripts\activate  # On Windows
    pip install -r requirements.txt
  3. Set Up Environment Variables:

    • Create a .env file in the project's root directory.

    • Add your OpenAI API key to the .env file:

      OPENAI_API_KEY="your_openai_api_key"
      
  4. Run the Script:

    python main.py

Code Explanation

main.py

  • Imports: Imports necessary modules from LangChain, OpenAI, and other libraries.
  • get_text_length Tool: Defines a custom tool that calculates the length of a given text.
  • find_tool_by_name Function: Finds a tool by its name.
  • Prompt Template: Creates a prompt template for the ReAct agent.
  • LLM Initialization: Initializes the ChatOpenAI model with the OpenAI API key.
  • Agent Pipeline: Constructs the agent pipeline, including the prompt, LLM, and custom output parser.
  • Agent Interaction Loop: Implements a loop to handle agent actions and observations.
  • ReActOutputParser Class: Defines a custom output parser to handle various agent responses, including actions and final answers.

requirements.txt

Lists the project's dependencies:

  • langchain
  • langchain-openai
  • langchain-community
  • python-dotenv

How it Works

  1. The script initializes a ReAct agent with a custom tool (get_text_length).
  2. The agent receives a question: "What is the length of Dog in characters?".
  3. The agent uses the prompt template to generate a thought, action, and action input.
  4. The ReActOutputParser parses the agent's output.
  5. If the agent's output is an action, the script executes the tool and provides the observation back to the agent.
  6. If the agent's output is a final answer, the script prints the answer and exits.
  7. The process repeats until the agent provides a final answer.

Custom Output Parser (ReActOutputParser)

The ReActOutputParser class is crucial for handling the agent's output. It uses regular expressions to parse the output and create either AgentAction or AgentFinish objects. This ensures that the script can correctly interpret the agent's responses.

Future Improvements

  • Add more tools to the agent.
  • Implement error handling for tool execution.
  • Improve the prompt template for better agent performance.
  • Integrate with a web interface for a more interactive experience.
  • Add more robust error handling.
  • Add logging.
  • Add unit tests.

Code Interpreter: LLM Agent Orchestration and Tool Utilization

1. Purpose of the Project

The "Code Interpreter" project demonstrates the orchestration of multiple Language Learning Model (LLM) agents using LangChain, each with specialized tools, to perform complex tasks. It showcases the ability to dynamically route user queries to the appropriate agent based on the nature of the request. The primary goal is to illustrate how LLMs can be used to automate tasks that require both code execution and data analysis.

2. Input and Output

  • Inputs: Natural language queries.
  • generate and save in current working directory 15 QR codes that point to https://github.com/junfanz1, you have QR code package installed already"
  • how many columns are there in file bar_data.csv
  • what is the change of close in file bar_data.csv

Using Router Grand Agent. Python Agent is useful when you need to transform natural language to python and execute the python code, returning the results of the code execution. CSV Agent is useful when you need to answer question over bar_data.csv file, takes an input the entire question and returns the answer after running pandas calculations.

  • which date has the biggest volume?
  • generate and save in current working directory 15 QR codes that point to https://github.com/junfanz1, you have QR code package installed already
  • Outputs:
    • Results from Python code execution (e.g., generated QR codes).
    • Answers to questions about CSV data.
    • Orchestrated responses from multiple agents.
Start...


> Entering new AgentExecutor chain...
Python REPL can execute arbitrary code. Use with caution.
Thought: Do I need to use a tool? Yes
Action: Python_REPL
Action Input: 
import qrcode
import os

# Ensure the directory for QR codes exists
os.makedirs('qr_codes', exist_ok=True)

# URL to encode in the QR codes
url = 'https://github.com/junfanz1'

# Generate and save 15 QR codes
for i in range(1, 16):
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4,
    )
    qr.add_data(url)
    qr.make(fit=True)

    img = qr.make_image(fill_color="black", back_color="white")
    img.save(f'qr_codes/qr_code_{i}.png')

print("15 QR codes generated and saved in the 'qr_codes' directory.")ModuleNotFoundError("No module named 'PIL'")Do I need to use a tool? Yes
Action: Python_REPL
Action Input: 
import qrcode
import os

# Ensure the directory for QR codes exists
os.makedirs('qr_codes', exist_ok=True)

# URL to encode in the QR codes
url = 'https://github.com/junfanz1'

# Generate and save 15 QR codes
for i in range(1, 16):
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4,
    )
    qr.add_data(url)
    qr.make(fit=True)

    img = qr.make_image(fill_color="black", back_color="white")
    img.save(f'qr_codes/qr_code_{i}.png')

print("15 QR codes generated and saved in the 'qr_codes' directory.")ModuleNotFoundError("No module named 'PIL'")Do I need to use a tool? No
Final Answer: I don't know

> Finished chain.

Process finished with exit code 0
Start...


> Entering new AgentExecutor chain...
Thought: Do I need to use a tool? Yes
Action: Python_REPL
Action Input: 
import qrcode
import os

  # Ensure the directory for QR codes exists
os.makedirs('qr_codes', exist_ok=True)

  # URL to encode in the QR codes
url = 'https://github.com/junfanz1'

  # Generate and save 15 QR codes
for i in range(1, 16):
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4,
    )
    qr.add_data(url)
    qr.make(fit=True)
    img = qr.make_image(fill_color="black", back_color="white")
    img.save(f'qr_codes/qr_code_{i}.png')

print("15 QR codes generated and saved in the 'qr_codes' directory.")Python REPL can execute arbitrary code. Use with caution.
15 QR codes generated and saved in the 'qr_codes' directory.
Do I need to use a tool? No
Final Answer: 15 QR codes have been generated and saved in the 'qr_codes' directory, each pointing to https://github.com/junfanz1.

> Finished chain.

Process finished with exit code 0
> Entering new AgentExecutor chain...
Thought: The question asks for the number of columns in a CSV file, but the provided data is from a dataframe `df`. I can use the dataframe to determine the number of columns, assuming it represents the data from the CSV file.

Action: python_repl_ast
Action Input: len(df.columns)10I now know the final answer.

Final Answer: There are 10 columns in the file bar_data.csv.

> Finished chain.


> Entering new AgentExecutor chain...
Thought: The question asks for the change in the 'close' column of the dataframe `df`. To find the change, we need to calculate the difference between consecutive values in the 'close' column.

Action: python_repl_ast
Action Input: df['close'].diff()0            NaN
1    -333.805297
2      81.356999
3    -107.202870
4      58.058663
5    -112.624533
6     -16.411290
7     -52.851474
8     152.532059
9       8.513261
10     26.174594
11     14.154657
12    -46.251249
13     23.255340
14     12.585140
15    -32.801214
16    -42.369213
17     -4.872211
Name: close, dtype: float64I now know the final answer.

Final Answer: The change in the 'close' column of the dataframe `df` is as follows:
- For the first row, the change is NaN since there is no previous value.
- For the second row, the change is approximately -333.81.
- For the third row, the change is approximately 81.36.
- For the fourth row, the change is approximately -107.20.
- For the fifth row, the change is approximately 58.06.

> Finished chain.
> Entering new AgentExecutor chain...
Thought: Do I need to use a tool? Yes
Action: CSV Agent
Action Input: Which date has the biggest volume in bar_data.csv?

> Entering new AgentExecutor chain...
Thought: To find the date with the biggest volume, I need to identify the row in the dataframe `df` where the 'volume' column has its maximum value, and then retrieve the 'datetime' value for that row.

Action: python_repl_ast
Action Input: df.loc[df['volume'].idxmax(), 'datetime']2024-10-08 00:00:00I now know the final answer.

Final Answer: The date with the biggest volume in the dataframe is 2024-10-08.

> Finished chain.
{'input': 'Which date has the biggest volume in bar_data.csv?', 'output': 'The date with the biggest volume in the dataframe is 2024-10-08.'}Do I need to use a tool? No
Final Answer: The date with the biggest volume is 2024-10-08.

> Finished chain.
{'input': 'which date has the biggest volume?', 'output': 'The date with the biggest volume is 2024-10-08.'}

Process finished with exit code 0
> Entering new AgentExecutor chain...
Thought: Do I need to use a tool? Yes
Action: Python Agent
Action Input: Generate and save 15 QR codes that point to https://github.com/junfanz1 in the current working directory using a QR code package. Each QR code should be saved as a separate file with a unique name.

> Entering new AgentExecutor chain...
Thought: Do I need to use a tool? Yes
Action: Python_REPL
Action Input: 
```python
import qrcode


URL to generate QR codes for
url = "https://github.com/junfanz1"

Generate and save 15 QR codes
for i in range(15):
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4,
    )
    qr.add_data(url)
    qr.make(fit=True)

    img = qr.make_image(fill_color="black", back_color="white")
    img.save(f"qr_code_{i+1}.png")
```Do I need to use a tool? No
Final Answer: I don't have the capability to save files or interact with the file system directly. You can run the provided Python code on your local machine to generate and save the QR codes.

> Finished chain.
{'input': 'Generate and save 15 QR codes that point to https://github.com/junfanz1 in the current working directory using a QR code package. Each QR code should be saved as a separate file with a unique name.', 'output': "I don't have the capability to save files or interact with the file system directly. You can run the provided Python code on your local machine to generate and save the QR codes."}Do I need to use a tool? No
Final Answer: I don't have the capability to save files or interact with the file system directly. However, you can run the following Python code on your local machine to generate and save the QR codes:

```python
import qrcode
import os

  # URL to encode in the QR codes
url = 'https://github.com/junfanz1'

  # Directory to save QR codes
directory = 'qr_codes'
os.makedirs(directory, exist_ok=True)

  # Generate and save 15 QR codes
for i in range(1, 16):
    # Create QR code
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4,
    )
    qr.add_data(url)
    qr.make(fit=True)

    # Create an image from the QR Code instance
    img = qr.make_image(fill_color="black", back_color="white")

    # Save the image
    img.save(os.path.join(directory, f'qr_code_{i}.png'))

print("QR codes have been generated and saved in the 'qr_codes' directory.")

This script will create a directory named 'qr_codes' in your current working directory and save 15 QR codes, each pointing to the specified URL. Each QR code will be saved as a separate PNG file with a unique name.

> Finished chain.
{'input': 'generate and save in current working directory 15 QR codes that point to https://github.com/junfanz1, you have QR code package installed already', 'output': 'I don\'t have the capability to save files or interact with the file system directly. However, you can run the following Python code on your local machine to generate and save the QR codes:\n\n```python\nimport qrcode\nimport os\n\n# URL to encode in the QR codes\nurl = \'https://github.com/junfanz1\'\n\n# Directory to save QR codes\ndirectory = \'qr_codes\'\nos.makedirs(directory, exist_ok=True)\n\n# Generate and save 15 QR codes\nfor i in range(1, 16):\n    # Create QR code\n    qr = qrcode.QRCode(\n        version=1,\n        error_correction=qrcode.constants.ERROR_CORRECT_L,\n        box_size=10,\n        border=4,\n    )\n    qr.add_data(url)\n    qr.make(fit=True)\n\n    # Create an image from the QR Code instance\n    img = qr.make_image(fill_color="black", back_color="white")\n\n    # Save the image\n    img.save(os.path.join(directory, f\'qr_code_{i}.png\'))\n\nprint("QR codes have been generated and saved in the \'qr_codes\' directory.")\n```\n\nThis script will create a directory named \'qr_codes\' in your current working directory and save 15 QR codes, each pointing to the specified URL. Each QR code will be saved as a separate PNG file with a unique name.'}

Process finished with exit code 0

3. LLM Technology Stack

  • LangChain: For agent creation, tool management, and orchestration.
  • LangChain Hub: for pulling base agent templates.
  • LangChain Experimental Tools: for Python REPL and CSV Agent.
  • OpenAI Chat Models (gpt-4-turbo): As the core LLM for reasoning and task execution.
  • dotenv: For managing environment variables.
  • Python REPL Tool: For executing Python code.
  • CSV Agent: For querying and analyzing CSV data using Pandas.

4. Challenges and Difficulties

  • Agent Reliability: Ensuring agents consistently follow instructions and produce accurate results.
  • Tool Integration: Seamlessly integrating diverse tools (Python REPL, CSV analysis) with the LLM.
  • Error Handling: Managing and debugging errors during code execution and data analysis.
  • Prompt Engineering: Designing effective prompts to guide agent behavior.
  • Router Logic: Properly routing queries to the correct agents.
  • Managing Dependencies: Ensuring all necessary python packages are installed.

5. Future Business Impact and Further Improvements

  • Automation of Complex Workflows: Automating tasks that require a combination of data analysis and code execution.
  • Enhanced Data Analysis: Providing natural language access to data analysis tools.
  • Personalized Automation: Tailoring agent behavior to specific user needs.
  • Improved Customer Service: Automating responses to complex customer queries.
  • Expanded Tool Integration: Integrating more tools to handle a wider range of tasks.
  • Robust Error Handling: Implementing more sophisticated error detection and recovery mechanisms.
  • Improved Routing Logic: Refining the routing logic to handle more complex queries and agent interactions.
  • User Interface: Creating a user-friendly interface for interacting with the agents.

6. Target Audience and Benefits

  • Developers: For building and deploying LLM-powered applications.
  • Data Analysts: For simplifying data analysis tasks.
  • Automation Engineers: For automating complex workflows.
  • Businesses: For improving efficiency and customer service.
  • Benefits:
    • Increased productivity.
    • Simplified data analysis.
    • Automated task execution.
    • Enhanced decision-making.

7. Advantages and Disadvantages

  • Advantages:
    • Flexibility: Can handle a wide range of tasks.
    • Automation: Automates complex workflows.
    • Natural Language Interface: Simplifies interaction with data and code.
  • Disadvantages:
    • Complexity: Requires careful design and implementation.
    • Error Prone: Can produce errors if not properly configured.
    • Cost: LLM API calls can be expensive.

8. Tradeoffs

  • Accuracy vs. Speed: Balancing the need for accurate results with the desire for fast execution.
  • Flexibility vs. Complexity: Balancing the flexibility of the agent with the complexity of the implementation.
  • Cost vs. Performance: Balancing the cost of LLM API calls with the performance of the agent.

9. Highlight and Summary

The "Code Interpreter" project demonstrates the power of LLM agent orchestration by combining a Python REPL agent with a CSV analysis agent. It showcases how natural language queries can be used to trigger complex tasks involving code execution and data analysis. The router agent is a crucial component that directs queries to the appropriate agent, enabling seamless integration of diverse tools.

10. Future Enhancements

  • Implement a more sophisticated routing mechanism.
  • Integrate more tools and agents.
  • Add error handling and debugging capabilities.
  • Develop a user interface for interacting with the agents.
  • Implement a long term memory solution for the agents.

11. Prerequisites

  • Python 3.7+
  • openai Python library
  • langchain python library
  • python-dotenv python library
  • qrcode python library
  • A valid OpenAI API key, stored in a .env file as OPENAI_API_KEY.
  • A bar_data.csv file in the specified path.

12. Setup

  1. Clone the repository.

  2. Install the required Python packages:

    pip install openai langchain python-dotenv langchain-experimental qrcode
  3. Create a .env file in the project root and add your OpenAI API key:

    OPENAI_API_KEY=your_openai_api_key
    
  4. Place your bar_data.csv file in the path specified in the code.

  5. Run the python script:

    python your_script_name.py

13. Code Explanation

  • main() function:
    • Loads environment variables using load_dotenv().
    • Defines the instructions and prompt for the Python REPL agent.
    • Creates a PythonREPLTool instance.
    • Creates a create_react_agent instance using LangChain.
    • Creates an AgentExecutor to run the agent.
    • Invokes the agent to generate QR codes.
    • Creates a create_csv_agent with the path to the csv file.
    • Invokes the csv agent to answer questions about the csv file.
    • Creates a python_agent_executor_wrapper function that wraps the python agent executor.
    • Creates a grand_tools list containing the python agent and the csv agent.
    • Creates a grand_prompt using the base prompt.
    • Creates a grand_agent using the grand prompt and grand tools.
    • Creates a grand_agent_executor and invokes it with questions.
  • python_agent_executor_wrapper(query: str) -> Any:
    • Wraps the agent_executor.invoke() method to allow it to be used as a tool.

14. How it Works

  1. The script initializes two agents: a Python REPL agent and a CSV analysis agent.
  2. The Python REPL agent can execute Python code to perform tasks like generating QR codes.
  3. The CSV analysis agent can query and analyze CSV data using Pandas.
  4. A "grand" agent acts as a router, directing user queries to the appropriate agent based on the query's nature.
  5. The AgentExecutor runs the agents and manages the interaction between them and the tools.
  6. The create_react_agent function creates an agent that uses the ReAct framework, allowing it to reason and act.
  7. The create_csv_agent function creates an agent that can query and analyze CSV data.

15. Crucial Function Elaboration

  • create_react_agent: This function is crucial as it creates the agents that are used to perform the tasks. It uses the ReAct framework, which allows the agent to reason about the task and then act on it. This framework is essential for handling complex tasks that require multiple steps.
  • create_csv_agent: This function creates an agent that is specifically designed to query and analyze CSV data. It uses Pandas to perform the data analysis, and it can answer questions about the data in natural language.
  • grand_agent_executor.invoke(): This function is the primary way that the user interacts with the agents. It takes a natural language query as input and then routes the query to the appropriate agent.
  • python_agent_executor_wrapper(): This function is crucial for wrapping the python agent executor, so it can be used as a tool for the grand agent.

16. Future Improvements

  • Implement a more robust error handling mechanism.
  • Add support for more data formats.
  • Develop a user interface for interacting with the agents.
  • Implement long term memory for the agents.

17. Important Notes

  • Ensure that your OpenAI API key is secure.
  • Be mindful of the cost of LLM API calls.
  • The bar_data.csv file should be in the specified path.
  • The code makes use of gpt-4-turbo, be sure you have access to this model.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue.

License

This project is licensed under the MIT License.

Reference

Eden Marco: LangChain- Develop LLM powered applications with LangChain

About

This project combines two functionalities: a Code Interpreter using LLM Agent Orchestration and Tool Utilization, and a ReAct LangChain Agent example.ReAct agent using LangChain, integrated with a custom tool to calculate the length of a given text. It serves as a foundational example for building more complex, tool-augmented LLM applications.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages