LazyLLM:低代碼構(gòu)建多Agent大模型應用的開源項目
LazyLLM是一款低代碼構(gòu)建多Agent大模型應用的開發(fā)工具,協(xié)助開發(fā)者用極低的成本構(gòu)建復雜的AI應用,可以持續(xù)的迭代優(yōu)化效果。提供了便捷的搭建應用的workflow,為應用開發(fā)過程中的各個環(huán)節(jié)提供了大量的標準流程和工具。
基于LazyLLM的AI應用構(gòu)建流程:原型搭建 -> 數(shù)據(jù)回流 -> 迭代優(yōu)化,可以先基于LazyLLM快速跑通應用的原型,再結(jié)合場景任務(wù)數(shù)據(jù)進行bad-case分析,然后對應用中的關(guān)鍵環(huán)節(jié)進行算法迭代和模型微調(diào),進而逐步提升整個應用的效果。
LazyLLM基本概念
Component
Component是LazyLLM中最小的執(zhí)行單元,它既可以是一個函數(shù),也可以是一個bash命令。
Module
Module是LazyLLM中的頂層組件,具備訓練、部署、推理和評測四項關(guān)鍵能力,每個模塊可以選擇實現(xiàn)其中的部分或者全部的能力,每項能力都可以由1到多個Component組成。如下表所示,內(nèi)置了一些基礎(chǔ)的Module供大家使用。
Flow
Flow是LazyLLM中定義的數(shù)據(jù)流,描述了數(shù)據(jù)如何從一個可調(diào)用對象傳遞到另一個可調(diào)用的對象,可以利用Flow直觀而高效地組織和管理數(shù)據(jù)流動?;陬A定義好的各種Flow,可以借助Module、Component、Flow甚至任一可調(diào)用的對象,輕松地構(gòu)建和管理復雜的應用程序。目前LazyLLm中實現(xiàn)的Flow有Pipeline、Parallel、Diverter、Warp、IFS、Loop等,幾乎可以覆蓋全部的應用場景。
使用指南
對話機器人
# set environment variable: LAZYLLM_OPENAI_API_KEY=xx # or you can make a config file(~/.lazyllm/config.json) and add openai_api_key=xximport lazyllmt = lazyllm.OnlineChatModule(source="openai", stream=True)w = lazyllm.WebModule(t)w.start().wait()
如果使用一個本地部署的模型,請確保自己安裝了至少一個推理框架(lightllm或vllm)
import lazyllm# Model will be download automatically if you have an internet connectiont = lazyllm.TrainableModule('internlm2-chat-7b')w = lazyllm.WebModule(t)w.start().wait()
檢索增強生成(RAG)
支持RAG常用組件:Document、Parser、Retriever、Reranker等。
import osimport lazyllmfrom lazyllm import pipeline, parallel, bind, _0, Document, Retriever, Reranker
prompt = '你將扮演一個人工智能問答助手的角色,完成一項對話任務(wù)。在這個任務(wù)中,你需要根據(jù)給定的上下文以及問題,給出你的回答。'documents = Document(dataset_path='/file/to/yourpath', embed=TrainableModule('bge-large-zh-v1.5'))with pipeline() as ppl: with parallel().sum as ppl.prl: prl.retriever1 = Retriever(documents, parser='CoarseChunk', similarity_top_k=6) prl.retriever2 = Retriever(documents, parser='SentenceDivider', similarity='chinese_bm25', similarity_top_k=6) ppl.reranker = Reranker(types='ModuleReranker', model='bge-reranker-large') | bind(ppl.input, _0) ppl.post_processer = lambda nodes: f'《{nodes[0].metadata["file_name"].split(".")[0]}》{nodes[0].get_content()}' if len(nodes) > 0 else '未找到' ppl.formatter = (lambda ctx, query: dict(context_str=ctx, query_str=query)) | bind(query=ppl.input) ppl.llm = lazyllm.TrainableModule('internlm2-chat-7b').prompt(lazyllm.ChatPrompter(prompt, extro_keys=['context_str'])) mweb = lazyllm.WebModule(ppl, port=23456).start().wait()
- 故事創(chuàng)作
import lazyllmfrom lazyllm import pipeline, warp, bindfrom lazyllm.components.formatter import JsonFormatter
toc_prompt=""" 你現(xiàn)在是一個智能助手。你的任務(wù)是理解用戶的輸入,將大綱以列表嵌套字典的列表。每個字典包含一個 `title` 和 `describe`,其中 `title` 中需要用Markdown格式標清層級,`describe` `describe` 是對該段的描述和寫作指導。
請根據(jù)以下用戶輸入生成相應的列表嵌套字典:
輸出示例:[ { "title": "# 一級標題", "describe": "請詳細描述此標題的內(nèi)容,提供背景信息和核心觀點。" }, { "title": "## 二級標題", "describe": "請詳細描述標題的內(nèi)容,提供具體的細節(jié)和例子來支持一級標題的觀點。" }, { "title": "### 三級標題", "describe": "請詳細描述標題的內(nèi)容,深入分析并提供更多的細節(jié)和數(shù)據(jù)支持。" }]用戶輸入如下:"""
completion_prompt="""你現(xiàn)在是一個智能助手。你的任務(wù)是接收一個包含 `title` 和 `describe` 的字典,并根據(jù) `describe` 中的指導展開寫作輸入示例:{ "title": "# 一級標題", "describe": "這是寫作的描述。"}
輸出:這是展開寫作寫的內(nèi)容接收如下:
"""
writer_prompt = {"system": completion_prompt, "user": '{"title": {title}, "describe": {describe}}'}
with pipeline() as ppl: ppl.outline_writer = lazyllm.OnlineChatModule(source="openai", stream=False).formatter(JsonFormatter()).prompt(toc_prompt) ppl.story_generater = warp(lazyllm.OnlineChatModule(source="openai", stream=False).prompt(writer_prompt)) ppl.synthesizer = (lambda *storys, outlines: "\n".join([f"{o['title']}\n{s}" for s, o in zip(storys, outlines)])) | bind(outlines=ppl.outline_writer)
print(ppl({'query':'請幫我寫一篇關(guān)于人工智能在醫(yī)療領(lǐng)域應用的文章。'}))
規(guī)劃:LazyLLM v0.2的目標是打造一個支持常見的AI-Agent應用場景,并在各個場景下支持靈活定制的AI應用開發(fā)框架。
LazyLLM支持10個場景的任務(wù):
LazyLLM v0.2 PRD:https://aicarrier.feishu.cn/wiki/BeFfwBFv8iXq7vkVGyrcv1kGnIh
docs:https://lazyllm.readthedocs.io/
github https://github.com/LazyAGI/LazyLLM
本文轉(zhuǎn)載自??PaperAgent??
