❤️ 如果你也关注 AI 的发展现状,且对 AI 应用开发感兴趣,我会每日分享大模型与 AI 领域的开源项目和应用,提供运行实例和实用教程,帮助你快速上手AI技术!
🥦 AI 在线答疑 -> 智能检索历史文章和开源项目 -> 尽在微信公众号 -> 搜一搜:蚝油菜花 🥦
💥 “凌晨3点的程序员都在哭(bushi):商汤开源的这个工具,让AI应用开发卷成填空题!”
大家好,我是蚝油菜花。你是否也经历过——
- 👉 想用GPT-4做智能客服,却在LangChain的API地狱里迷路
- 👉 调教多模型协作时,被线程管理和数据流逼到秃头
- 👉 好不容易跑通Demo,却卡在微调参数和部署环节...
今天要分享的 LazyLLM ,正是商汤大装置团队扔向开发者的一颗救星!帮助开发者以低成本快速构建多智能体的大语言模型应用:
- ✅ 5行代码组建AI团队:让ChatGPT处理文案,Claude分析数据,SDXL生成图表
- ✅ 动态Token剪枝黑科技:长文本推理速度提升300%
- ✅ 企业级一键部署:从本地测试到云端上线只需1条命令
🚀 快速阅读
LazyLLM 是一款低代码开发工具,专注于多智能体大语言模型应用的构建。
- 核心功能:支持低代码开发、多智能体协同、模型微调与推理、一键部署和跨平台兼容。
- 技术原理:采用数据流驱动开发方式,模块化设计支持灵活组合,动态优化模型性能。
LazyLLM 是什么
LazyLLM 是一个低代码开发工具,旨在帮助开发者以低成本快速构建多智能体的大语言模型应用。它通过简化的开发流程,降低 AI 应用的开发门槛,尤其适合初学者和专业开发者。无论是聊天机器人、检索增强生成(RAG)还是多模态应用,都可以通过少量代码实现复杂功能。
LazyLLM 提供了一键部署和跨平台支持的能力,兼容 Windows、Linux 和 macOS 等多种操作系统环境。同时,它还支持在线和离线模型的推理,能够结合图像、音频等多模态数据,拓展应用场景。
LazyLLM 的主要功能
- 低代码开发:通过极简的开发流程,快速构建复杂的 AI 应用,降低开发难度。
- 多智能体支持:支持多个模型协同工作,满足不同场景下的需求。
- 模型微调与推理:支持在线和离线模型微调,兼容多种推理框架。
- 一键部署:通过轻量级网关机制,支持本地或云端的一键部署。
- 跨平台支持:兼容多种操作系统和环境,便于灵活选择开发和部署环境。
- 多模态扩展:支持结合图像、音频等数据,构建更丰富的应用场景。
- 灵活配置:提供丰富的配置选项,支持定制化开发和性能优化。
LazyLLM 的技术原理
- 数据流驱动开发:通过 Pipeline、Parallel、Diverter 和 Loop 等数据流控制方式,灵活组织复杂的数据处理流程。
- 组件化与模块化设计:基于组件和模块构建应用,支持训练、部署、推理和评估等核心能力。
- 模型微调与参数优化:自动选择最佳微调框架和参数分割策略,支持网格搜索优化配置。
- 动态 Token 剪枝:引入动态剪枝技术,提高长文本推理效率。
如何运行 LazyLLM
1. 安装 LazyLLM
可以通过以下两种方式快速安装:
# 使用 pip 安装基础依赖
pip3 install lazyllm
# 安装完整依赖
pip3 install lazyllm
lazyllm install full
2. 三行代码部署聊天机器人
以下是一个简单的聊天机器人示例:
import lazyllm
chat = lazyllm.OnlineChatModule()
lazyllm.WebModule(chat).start().wait()
如果使用本地模型,可以指定模型名称:
chat = lazyllm.TrainableModule('internlm2-chat-7b')
lazyllm.WebModule(chat, port=23466).start().wait()
如果你使用pip
安装了lazyllm
并确保Python环境的bin目录在你的$PATH
中,你还可以通过执行一行命令lazyllm run chatbot
快速启动聊天机器人!如果你想使用本地模型,只需要使用--model
参数指定模型名称。例如,你可以使用lazyllm run chatbot --model=internlm2-chat-7b
启动基于本地模型的聊天机器人。
3. 构建具有多模态和意图识别的高级智能体
代码示例:
from lazyllm import TrainableModule, WebModule, deploy, pipeline
from lazyllm.tools import IntentClassifier
painter_prompt = 'Now you are a master of drawing prompts, capable of converting any Chinese content entered by the user into English drawing prompts. In this task, you need to convert any input content into English drawing prompts, and you can enrich and expand the prompt content.'
musician_prompt = 'Now you are a master of music composition prompts, capable of converting any Chinese content entered by the user into English music composition prompts. In this task, you need to convert any input content into English music composition prompts, and you can enrich and expand the prompt content.'
base = TrainableModule('internlm2-chat-7b')
with IntentClassifier(base) as ic:
ic.case['Chat', base]
ic.case['Speech Recognition', TrainableModule('SenseVoiceSmall')]
ic.case['Image QA', TrainableModule('Mini-InternVL-Chat-2B-V1-5').deploy_method(deploy.LMDeploy)]
ic.case['Drawing', pipeline(base.share().prompt(painter_prompt), TrainableModule('stable-diffusion-3-medium'))]
ic.case['Generate Music', pipeline(base.share().prompt(musician_prompt), TrainableModule('musicgen-small'))]
ic.case['Text to Speech', TrainableModule('ChatTTS')]
WebModule(ic, history=[base], audio=True, port=8847).start().wait()
4. 构建 RAG 应用
导入库和提示词:
import os
import lazyllm
from lazyllm import pipeline, parallel, bind, SentenceSplitter, Document, Retriever, Reranker
prompt = 'You will play the role of an AI Q&A assistant and complete a dialogue task. In this task, you need to provide your answer based on the given context and question.'
以下是一个在线部署的 RAG 示例:
documents = Document(dataset_path="your data path", embed=lazyllm.OnlineEmbeddingModule(), manager=False)
documents.create_node_group(name="sentences", transform=SentenceSplitter, chunk_size=1024, chunk_overlap=100)
with pipeline() as ppl:
with parallel().sum as ppl.prl:
prl.retriever1 = Retriever(documents, group_name="sentences", similarity="cosine", topk=3)
prl.retriever2 = Retriever(documents, "CoarseChunk", "bm25_chinese", 0.003, topk=3)
ppl.reranker = Reranker("ModuleReranker", model="bge-reranker-large", topk=1) | bind(query=ppl.input)
ppl.formatter = (lambda nodes, query: dict(context_str="".join([node.get_content() for node in nodes]), query=query)) | bind(query=ppl.input)
ppl.llm = lazyllm.OnlineChatModule(stream=False).prompt(lazyllm.ChatPrompter(prompt, extro_keys=["context_str"]))
lazyllm.WebModule(ppl, port=23466).start().wait()
以下是一个本地部署的示例:
documents = Document(dataset_path='/file/to/yourpath', embed=lazyllm.TrainableModule('bge-large-zh-v1.5'))
documents.create_node_group(name="sentences", transform=SentenceSplitter, chunk_size=1024, chunk_overlap=100)
with pipeline() as ppl:
with parallel().sum as ppl.prl:
prl.retriever1 = Retriever(documents, group_name="sentences", similarity="cosine", topk=3)
prl.retriever2 = Retriever(documents, "CoarseChunk", "bm25_chinese", 0.003, topk=3)
ppl.reranker = Reranker("ModuleReranker", model="bge-reranker-large", topk=1) | bind(query=ppl.input)
ppl.formatter = (lambda nodes, query: dict(context_str="".join([node.get_content() for node in nodes]), query=query)) | bind(query=ppl.input)
ppl.llm = lazyllm.TrainableModule("internlm2-chat-7b").prompt(lazyllm.ChatPrompter(prompt, extro_keys=["context_str"]))
lazyllm.WebModule(ppl, port=23456).start().wait()
资源
- GitHub 仓库:https://github.com/LazyAGI/LazyLLM
❤️ 如果你也关注 AI 的发展现状,且对 AI 应用开发感兴趣,我会每日分享大模型与 AI 领域的开源项目和应用,提供运行实例和实用教程,帮助你快速上手AI技术!
🥦 AI 在线答疑 -> 智能检索历史文章和开源项目 -> 尽在微信公众号 -> 搜一搜:蚝油菜花 🥦