{"name":"lagent: A Lightweight Framework for Building LLM-Based Agents","description":"lagent is a lightweight, open-source framework developed by InternLM, designed for efficiently building large language model (LLM)-based agents. It provides a PyTorch-inspired design philosophy, making it intuitive for developers to create and manage multi-agent applications. This framework simplifies the process of agent communication, memory management, and tool integration.","github":"https://github.com/InternLM/lagent","url":"https://osrepos.com/repo/internlm-lagent","source":"osrepos.com","sourceDescription":"This repository profile is provided by osrepos.com, an open source repository discovery platform.","repositoryProfile":"https://osrepos.com/repo/internlm-lagent","generatedFor":"open source discovery and AI-assisted research","markdown":"https://osrepos.com/repo/internlm-lagent.md","json":"https://osrepos.com/repo/internlm-lagent.json","topics":["agent","gpt","llm","transformers","Python","AI","Machine Learning","Framework"],"keywords":["agent","gpt","llm","transformers","Python","AI","Machine Learning","Framework"],"stars":null,"summary":"lagent is a lightweight, open-source framework developed by InternLM, designed for efficiently building large language model (LLM)-based agents. It provides a PyTorch-inspired design philosophy, making it intuitive for developers to create and manage multi-agent applications. This framework simplifies the process of agent communication, memory management, and tool integration.","content":"## Introduction\n\nlagent, developed by InternLM, is a lightweight and open-source framework designed for building sophisticated agents powered by Large Language Models (LLMs). Inspired by the design philosophy of PyTorch, lagent offers an intuitive and clear workflow, allowing developers to focus on creating agent layers and defining message passing between them in a Pythonic way. It simplifies complex tasks like agent communication, memory management, and tool integration, making it an excellent choice for developing various LLM-based applications.\n\n## Installation\n\nTo get started with lagent, you can install it directly from the source. Follow these simple steps:\n\nbash\ngit clone https://github.com/InternLM/lagent.git\ncd lagent\npip install -e .\n\n\n## Examples\n\nlagent provides a flexible architecture for building and managing LLM agents. Here are some core functionalities demonstrated through examples:\n\n### Models as Agents\n\nAgents in lagent communicate using `AgentMessage`. You can easily initialize an agent with an LLM and a system prompt.\n\npython\nfrom lagent.agents import Agent\nfrom lagent.schema import AgentMessage\nfrom lagent.llms import VllmModel, INTERNLM2_META\n\nllm = VllmModel(\n    path='Qwen/Qwen2-7B-Instruct',\n    meta_template=INTERNLM2_META,\n    tp=1,\n    top_k=1,\n    temperature=1.0,\n    stop_words=['<|im_end|>'],\n    max_new_tokens=1024,\n)\nsystem_prompt = 'Your answer can only be chosen from \"?\", \"?\", \"?\".'\nagent = Agent(llm, system_prompt)\n\nuser_msg = AgentMessage(sender='user', content='Today\\'s weather conditions')\nbot_msg = agent(user_msg)\nprint(bot_msg)\n\n\n### Memory as State\n\nBoth input and output messages are automatically added to an agent's memory. This allows agents to maintain context throughout a conversation. You can inspect and clear an agent's memory.\n\npython\nmemory = agent.memory.get_memory()\nprint(memory)\nagent.reset() # Clear the memory for the current session\n\n\n### Flexible Response Formatting and Tool Calling\n\nlagent supports flexible response formatting and robust tool integration. You can define custom parsers to extract structured information from LLM outputs and use `ActionExecutor` to invoke external tools.\n\npython\nfrom lagent.prompts.parsers import ToolParser\nfrom lagent.actions import ActionExecutor, IPythonInteractive\nfrom lagent.hooks import InternLMActionProcessor\n\nsystem_prompt = \"Analyze step by step and write Python code to solve the following problem.\"\nparser = ToolParser(tool_type='code interpreter', begin='python\\n', end='\\n\\n')\nllm.gen_params['stop_words'].append('\\n\\n')\nagent = Agent(llm, system_prompt, output_format=parser)\n\nuser_msg = AgentMessage(\n    sender='user',\n    content='Marie is thinking of a multiple of 63, while Jay is thinking of a '\n    'factor of 63. They happen to be thinking of the same number. There are '\n    'two possibilities for the number that each of them is thinking of, one '\n    'positive and one negative. Find the product of these two numbers.')\nbot_msg = agent(user_msg)\nprint(bot_msg.model_dump_json(indent=4))\n\n# Execute the generated code\nexecutor = ActionExecutor(actions=[IPythonInteractive()], hooks=[InternLMActionProcessor()])\nexecutor_msg = executor(bot_msg)\nprint(executor_msg)\n\n\n### Dual Interfaces for Concurrency\n\nLagent offers both synchronous and asynchronous interfaces for almost every component, allowing for efficient debugging and large-scale inference.\n\npython\nfrom lagent.llms import VllmModel, AsyncVllmModel\nfrom lagent.actions import ActionExecutor, AsyncActionExecutor\nfrom lagent.agents import Agent, AsyncAgent\n\n\n## Why Use It\n\nlagent stands out as a powerful framework for LLM agent development due to several key advantages:\n\n*   **PyTorch-Inspired Design:** Its familiar design philosophy makes it easy for machine learning developers to quickly grasp and utilize the framework.\n*   **Lightweight and Flexible:** The framework is designed to be lightweight, offering high flexibility for customizing agent behavior, message aggregation, and response parsing.\n*   **Robust Tool Integration:** Seamlessly integrate external tools like code interpreters and web browsers, enabling agents to perform complex tasks.\n*   **Asynchronous Support:** Dual synchronous and asynchronous interfaces allow for optimized performance in various scenarios, from debugging to large-scale deployments.\n*   **Active Development:** Backed by InternLM, lagent benefits from ongoing development and community support.\n\n## Links\n\n*   **GitHub Repository:** <a href=\"https://github.com/InternLM/lagent\" target=\"_blank\">https://github.com/InternLM/lagent</a>\n*   **Documentation:** <a href=\"https://lagent.readthedocs.io/en/latest/\" target=\"_blank\">https://lagent.readthedocs.io/en/latest/</a>\n*   **PyPI:** <a href=\"https://pypi.org/project/lagent\" target=\"_blank\">https://pypi.org/project/lagent</a>\n*   **License:** <a href=\"https://github.com/InternLM/lagent/tree/main/LICENSE\" target=\"_blank\">Apache-2.0 License</a>","metrics":{"detailViews":7,"githubClicks":6},"dates":{"published":null,"modified":"2026-01-18T16:01:09.000Z"}}