Lagent#

What’s Lagent?#

Lagent is a lightweight open-source framework that allows users to efficiently build large language model(LLM)-based agents. It also provides some typical tools to augment LLM. The overview of the framework is shown below:

image

This document primarily highlights the basic usage of Lagent. For a comprehensive understanding of the toolkit, please refer to examples for more details.

Installation#

Install with pip (Recommended).

pip install lagent

Optionally, you could also build Lagent from source in case you want to modify the code:

git clone https://github.com/InternLM/lagent.git
cd lagent
pip install -e .

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

Run a ReAct agent with InternLM2.5-Chat#

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

# Initialize the HFTransformer-based Language Model (llm) and provide the model name.
llm = HFTransformer('internlm/internlm2_5-7b-chat')

# 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.
chatbot = ReAct(
    llm=llm,  # Provide the Language Model instance.
    action_executor=ActionExecutor(
        actions=[search_tool, python_interpreter]  # Specify the actions the chatbot can perform.
    ),
)
# 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$