←新闻详情

InternLM 原创项目更新于 10月14日 12:28

InternLM/lagent:构建 LLM 智能体的轻量级框架

GitHub - InternLM/lagent: A lightweight framework for building LLM-based agents · GitHub

AI 导读

InternLM 开源了 lagent,一个受 PyTorch 设计理念启发的轻量级 LLM 智能体框架,用户只需用 Pythonic 方式创建层并定义层间消息传递。框架以 AgentMessage 通信、Memory 作为状态,提供同步与异步双接口,并内置 InternLMActionProcessor 适配 ToolParser 格式,采用 Apache 2.0 许可。

正文

当前显示原文,中文全文译文尚未就绪。

InternLM; lagent

Repository navigation

NotificationsYou must be signed in to change notification settings; Fork246 (246); Star2.3k (2.3k)You must be signed in to star a repository

A lightweight framework for building LLM-based agents

Repository files navigation

README; Apache-2.0 license

👋 join us on 𝕏 (Twitter), Discord and WeChat

Lagent is inspired by the design philosophy of PyTorch. We expect that the analogy of neural network layers will make the workflow clearer and more intuitive, so users only need to focus on creating layers and defining message passing between them in a Pythonic way. This is a simple tutorial to get you quickly started with building multi-agent applications.

Models as Agents

Agents use AgentMessage for communication.

Memory as State

Both input and output messages will be added to the memory of Agent in each forward pass. This is performed in __call__ rather than forward. See the following pseudo code

Inspect the memory in two ways

Clear the memory of this session(session_id=0 by default):

Custom Message Aggregation

DefaultAggregator is called under the hood to assemble and convert AgentMessage to OpenAI message format.

Implement a simple aggregator that can receive few-shots

Flexible Response Formatting

In AgentMessage, formatted is reserved to store information parsed by output_format from the model output.

Use a tool parser as follows

Consistency of Tool Calling

ActionExecutor uses the same communication data structure as Agent, but requires the content of input AgentMessage to be a dict containing:

name: tool name, e.g. 'IPythonInterpreter', 'WebBrowser.search'; parameters: keyword arguments of the tool API, e.g. {'command': 'import math;math.sqrt(2)'}, {'query': ['recent progress in AI']}

You can register custom hooks for message conversion.

For convenience, Lagent provides InternLMActionProcessor which is adapted to messages formatted by ToolParser as mentioned above.

Dual Interfaces

Lagent adopts dual interface design, where almost every component(LLMs, actions, action executors...) has the corresponding asynchronous variant by prefixing its identifier with 'Async'. It is recommended to use synchronous agents for debugging and asynchronous ones for large-scale inference to make the most of idle CPU and GPU resources.

However, make sure the internal consistency of agents, i.e. asynchronous agents should be equipped with asynchronous LLMs and asynchronous action executors that drive asynchronous tools.

Practice

Try to implement forward instead of __call__ of subclasses unless necessary; Always include the session_id argument explicitly, which is designed for isolation of memory, LLM requests and tool invocation(e.g. maintain multiple independent IPython environments) in concurrency

Single Agent

Math agents that solve problems by programming

Multiple Agents

Asynchronous blogging agents that improve writing quality by self-refinement (original AutoGen example)

A multi-agent workflow that performs information retrieval, data collection and chart plotting (original LangGraph example)

Citation

If you find this project useful in your research, please consider cite:

License

This project is released under the Apache 2.0 license.

🔼 Back to top

A lightweight framework for building LLM-based agents

来源

InternLM 原创项目