Here is an example of a Python code that uses the LangGraph library to create a React agent and demonstrates tool calling and Retrieval-Augmented Generation (RAG) in parallel:
python
# Import necessary libraries
import langgraph
from langgraph.agents import ReactAgent
from langgraph.tools import Tool
from langgraph.rag import RAG
# Create a React agent
agent = ReactAgent()
# Define a tool for the agent to use
class MyTool(Tool):
def __init__(self):
super().__init__()
def call(self, input_text):
# Simulate a tool that returns a response
return f"Tool response: {input_text}"
# Create a RAG model
rag = RAG()
# Define a function to call the tool and RAG in parallel
def call_tool_and_rag(input_text):
# Call the tool
tool_response = agent.call_tool(MyTool(), input_text)
# Call the RAG model
rag_response = rag.generate(input_text)
# Return both responses
return tool_response, rag_response
# Test the function
input_text = "Hello, world!"
tool_response, rag_response = call_tool_and_rag(input_text)
print(f"Tool response: {tool_response}")
print(f"RAG response: {rag_response}")
This code creates a React agent and defines a tool that the agent can use. It also creates a RAG model and defines a function that calls the tool and RAG model in parallel. The function takes an input text, calls the tool and RAG model, and returns both responses.
To create an ipynb file, you can copy and paste this code into a new cell in a Jupyter Notebook. You will need to have the LangGraph library installed and imported in your notebook.
Here is the code in a format that can be copied and pasted into a Jupyter Notebook:
markdown
# LangGraph React Agent and Tool Calling with RAG
## Introduction
This notebook demonstrates how to create a React agent and use it to call a tool and a Retrieval-Augmented Generation (RAG) model in parallel.
## Import Libraries
```python
import langgraph
from langgraph.agents import ReactAgent
from langgraph.tools import Tool
from langgraph.rag import RAG
Create React Agent
python
agent = ReactAgent()
Define Tool
python
class MyTool(Tool):
def __init__(self):
super().__init__()
def call(self, input_text):
# Simulate a tool that returns a response
return f"Tool response: {input_text}"
Create RAG Model
python
rag = RAG()
Define Function to Call Tool and RAG
python
def call_tool_and_rag(input_text):
# Call the tool
tool_response = agent.call_tool(MyTool(), input_text)
# Call the RAG model
rag_response = rag.generate(input_text)
# Return both responses
return tool_response, rag_response