InternLM/agent/lagent.md

74 lines
2.4 KiB
Markdown

# Lagnet
English | [简体中文](lagent_zh-CN.md)
## 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](https://github.com/InternLM/lagent/assets/24351120/cefc4145-2ad8-4f80-b88b-97c05d1b9d3e)
This document primarily highlights the basic usage of Lagent. For a comprehensive understanding of the toolkit, please refer to [examples](https://github.com/InternLM/lagent/tree/main/examples) for more details.
## Installation
Install with pip (Recommended).
```bash
pip install lagent
```
Optionally, you could also build Lagent from source in case you want to modify the code:
```bash
git clone https://github.com/InternLM/lagent.git
cd lagent
pip install -e .
```
## Run ReAct Web Demo
```bash
# 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](https://github.com/InternLM/lagent/assets/24622904/3aebb8b4-07d1-42a2-9da3-46080c556f68)
## Run a ReAct agent with InternLM2.5-Chat
**NOTE:** If you want to run a HuggingFace model, please run `pip install -e .[all]` first.
```python
# 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$
```