diff --git a/agent/README.md b/agent/README.md index e69de29..8f54ec7 100644 --- a/agent/README.md +++ b/agent/README.md @@ -0,0 +1,13 @@ +# InternLM-Chat Agent + +English | [简体中文](README_zh-CN.md) + +## Introduction + +On August 22, 2023, the Shanghai Artificial Intelligence Laboratory open-sourced InternLM-Chat-7B v1.1, the first open-dialogue model with code interpretation capabilities. It supports external tools such as the Python interpreter and can fetch real-time information through search engines. + +InternLM2-Chat, open-sourced on January 17, 2024, further enhances its capabilities in code interpretation and general tool invocation. With improved and more generalized instruction understanding, tool selection, and result reflection, the new model can more reliably support the construction of complex intelligent agents. It facilitates effective multi-round invocation of tools and accomplishes more intricate tasks. The model exhibits decent computational and reasoning abilities even without external tools, surpassing ChatGPT in mathematical performance. When combined with a code interpreter, InternLM2-Chat-20B achieves a level comparable to GPT-4 on GSM8K and MATH. Leveraging strong foundational capabilities in mathematics and tools, InternLM2-Chat provides practical data analysis capabilities. + +## Experience + +We offer examples using [Lagent](lagent.md) to build intelligent agents based on InternLM2-Chat, calling code interpreters or searching tools. Additionally, we provide a sample using [PAL to evaluate GSM8K math problems](pal_inference.md) with InternLM-Chat-7B. diff --git a/agent/README_zh-CN.md b/agent/README_zh-CN.md new file mode 100644 index 0000000..c9d53c2 --- /dev/null +++ b/agent/README_zh-CN.md @@ -0,0 +1,13 @@ +# InternLM-Chat 智能体 + +[English](README.md) | 简体中文 + +## 简介 + +在 2023 年 8 月 22 日,上海人工智能实验室开源了 InternLM-Chat-7B v1.1,是首个具有代码解释能力的开源对话模型,支持 Python 解释器等外部工具,并且可以通过搜索引擎获取实时信息。 + +2024 年 1 月 17 日开源的 InternLM2-Chat 进一步提高了在代码解释和通用工具调用方面的能力。基于更强和更具有泛化性的指令理解、工具筛选与结果反思等能力,新版模型可以更可靠地支持复杂智能体的搭建,支持对工具进行有效的多轮调用,完成较复杂的任务。模型在不使用外部工具的条件下已具备不错的计算能力和推理能力,数理表现超过 ChatGPT;在配合代码解释器(code-interpreter)的条件下,InternLM2-Chat-20B 在 GSM8K 和 MATH 上可以达到和 GPT-4 相仿的水平。基于在数理和工具方面强大的基础能力,InternLM2-Chat 提供了实用的数据分析能力。 + +## 体验 + +我们提供了使用 [Lagent](lagent_zh_cn.md) 来基于 InternLM2-Chat 构建智能体调用代码解释器或者搜索等工具的例子。同时,我们也提供了采用 [PAL 评测 GSM8K 数学题](pal_inference_zh-CN.md) InternLM-Chat-7B 的样例。 diff --git a/agent/lagent.md b/agent/lagent.md new file mode 100644 index 0000000..f623c82 --- /dev/null +++ b/agent/lagent.md @@ -0,0 +1,73 @@ +# 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 our framework is shown below: + +![image](https://github.com/InternLM/lagent/assets/24351120/cefc4145-2ad8-4f80-b88b-97c05d1b9d3e) + +This article primarily highlights the basic usage of Lagent. For a comprehensive understanding of the toolkit, we invite you to 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 InternLM-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/internlm-chat-7b-v1_1') + +# 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$ +``` diff --git a/agent/lagent_zh-CN.md b/agent/lagent_zh-CN.md new file mode 100644 index 0000000..0365969 --- /dev/null +++ b/agent/lagent_zh-CN.md @@ -0,0 +1,73 @@ +# Lagnet + +[English](lagent.md) | 简体中文 + +## 简介 + +[Lagent](https://github.com/InternLM/lagent) 是一个轻量级、开源的基于大语言模型的智能体(agent)框架,支持用户快速地将一个大语言模型转变为多种类型的智能体,并提供了一些典型工具为大语言模型赋能。它的整个框架图如下: + +![image](https://github.com/InternLM/lagent/assets/24351120/cefc4145-2ad8-4f80-b88b-97c05d1b9d3e) + +本文主要介绍 Lagent 的基本用法。更全面的介绍请参考 Lagent 中提供的 [例子](https://github.com/InternLM/lagent/tree/main/examples)。 + +## 安装 + +通过 pip 进行安装 (推荐)。 + +```bash +pip install lagent +``` + +同时,如果你想修改这部分的代码,也可以通过以下命令从源码编译 Lagent: + +```bash +git clone https://github.com/InternLM/lagent.git +cd lagent +pip install -e . +``` + +## 运行一个 ReAct 智能体的网页样例 + +```bash +# 需要确保已经安装 streamlit 包 +# pip install streamlit +streamlit run examples/react_web_demo.py +``` + +然后你就可以在网页端和智能体进行对话了,效果如下图所示 + +![image](https://github.com/InternLM/lagent/assets/24622904/3aebb8b4-07d1-42a2-9da3-46080c556f68) + +## 用 InternLM-Chat 构建一个 ReAct 智能体 + +**注意:**如果你想要启动一个 HuggingFace 的模型,请先运行 pip install -e .[all]。 + +```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/internlm-chat-7b-v1_1') + +# 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$ +``` diff --git a/agent/pal_inference.md b/agent/pal_inference.md new file mode 100644 index 0000000..851f0d8 --- /dev/null +++ b/agent/pal_inference.md @@ -0,0 +1,62 @@ +# Inference on GSM8K with PAL in InternLM-Chat + +English | [简体中文](pal_inference_zh-CN.md) + +Utilize [PAL](https://github.com/reasoning-machines/pal) paradigm inference on the [GSM8K](https://huggingface.co/datasets/gsm8k) dataset, enabling the model to write code and execute it through the Python interpreter to solve mathematical problems. The usage is as follows: + +```bash +python pal_inference.py \ + \ + \ + [--dataset ] \ + [--max_length ] \ + [--top_p ] \ + [--eoh ] \ + [--eoa ] \ + [--eos ] \ + [--temperature ] \ + [--time_out