Quickstart

Using Lagent, you can easily build agents with just a few lines of code.

Run a ReWOO agent with GPT-3.5

Below is an example of running ReWOO with GPT-3.5

# Import necessary modules and classes from the "lagent" library.
from lagent.agents import ReWOO
from lagent.actions import ActionExecutor, GoogleSearch
from lagent.llms import GPTAPI

# Initialize the Language Model (llm) and provide your API key.
llm = GPTAPI(model_type='gpt-3.5-turbo', key=['Your OPENAI_API_KEY'])

# Initialize the Google Search tool and provide your API key.
search_tool = GoogleSearch(api_key='Your SERPER_API_KEY')

# Create a chatbot by configuring the ReWOO agent.
chatbot = ReWOO(
    llm=llm,  # Provide the Language Model instance.
    action_executor=ActionExecutor(
        actions=[search_tool]  # Specify the actions the chatbot can perform.
    ),
)

# Ask the chatbot a question and store the response.
response = chatbot.chat('What profession does Nicholas Ray and Elia Kazan have in common')

# Print the chatbot's response.
print(response.response)  # Output the response generated by the chatbot.
>>> Film director.

Run a ReAct agent with InternLM

NOTE: If you want to run a HuggingFace model, please run pip install -e .[all] first.

# Import necessary modules and classes from the "lagent" library.
from lagent.agents import ReAct
from lagent.actions import ActionExecutor, GoogleSearch, PythonInterpreter
from lagent.llms import HFTransformer

from lagent.llms.meta_template import INTERNLM2_META as META

# Initialize the HFTransformer-based Language Model (llm) and
# provide the model name.
llm = HFTransformer(path='internlm/internlm2-chat-7b', meta_template=META)

# Initialize the Google Search tool and provide your API key.
search_tool = GoogleSearch(api_key='Your SERPER_API_KEY')

# Initialize the Python Interpreter tool.
python_interpreter = PythonInterpreter()

# Create a chatbot by configuring the ReAct agent.
# Specify the actions the chatbot can perform.
chatbot = ReAct(
    llm=llm,  # Provide the Language Model instance.
    action_executor=ActionExecutor(
        actions=[search_tool, python_interpreter]),
)
# Ask the chatbot a mathematical question in LaTeX format.
response = chatbot.chat('若$z=-1+\sqrt{3}i$,则$\frac{z}{{z\overline{z}-1}}=\left(\ \ \right)$')

# Print the chatbot's response.
print(response.response)  # Output the response generated by the chatbot.
>>> $-\\frac{1}{3}+\\frac{{\\sqrt{3}}}{3}i$

Run ReAct Web Demo

# You need to install streamlit first
# pip install streamlit
streamlit run examples/react_web_demo.py

Then you can chat through the UI shown as below image