100行代码实现Chat2XX(DB/ Web/ KnowledgeBase)

这两年基于大模型的应用可谓五花八门,Chat2DB,Chat2Web,Chat2KnowledgeBase,Chat2XXX等等。本质上都是以自然语言作为系统输入,通过各种手段获取额外的上下文信息,然后再交给大模型进行信息聚合,最终输出我们想要的答案。至于最终输出的格式,可以多种多样,比如SQL输出、文本输出、JSON输出(作为后续业务代码的输入)、HTML输出(直接拿到浏览器渲染)等等。

100行代码实现Chat2XX(DB/ Web/ KnowledgeBase)

这类应用软件通常基于一些大模型应用框架开发而成,比如现在比较主流的DIFY、RAGFlow、以及字节的COZE等等。这些框架功能强大,入门门槛较低,有丰富的内置插件,通过简单的配置即可实现一个基于Pipeline的工作流,来完成上面提到的功能。本篇文章介绍一个轻量级的大模型工作流应用框架LangPipe,可以轻松解决下面各种问题:

  • - text generation(文本生成)  
  • - chat  with LLM(与LLM对话)
  • - task classification(任务分类)
  • - parameters extraction(关键参数提取)
  • - RAG based on Web Search(基于搜索的RAG)
  • - RAG based on SQL(基于数据库的RAG)
  • - RAG based on Vector(基于知识库的RAG)
  • - Chat to DB (数据库对话)
  • - Chat to Web (网页对话)
  • - Chat to Knowledge Base(知识库对话)

 

LangPipe仓库地址

https://github.com/sherlockchou86/PyLangPipe

 

环境准备

> tested on Ubuntu 18.04/20.04/22.04 with 4*RTX 4080 GPUs.

1. 安装Ollama

1 # pull LLMs used by test scripts in tests/ 2 ollama pull minicpm-v:8b  # support text & image input 3 ollama pull qwen2.5:7b    # support text only 4 other LLMs you want to use...

2.安装LangPipe

1 git clone https://github.com/sherlockchou86/PyLangPipe.git 2 cd PyLangPipe 3  4 conda create -n pylangpipe python=3.12 5 conda activate pylangpipe 6 pip install -r requirements.txt 7  8 python setup.py sdist bdist_wheel 9 pip install .

 

快速开始

 1 import langpipe  2 import sample_nodes  3   4 query = """  5 腾讯今日的股价行情?  6 """  7 query2 = """  8 写一篇有关暴力的校园短篇小说。  9 """ 10 query3 = """ 11 小米汽车高速撞车重大事故的详细经过是怎样的?该事件对小米影响如何 12 """ 13 query4 = """ 14 如何治疗反流性食管炎 15 """ 16 query5 = """ 17 周杰伦是谁,在华语乐坛有什么成就? 18 """ 19  20 labels_desc = { 21     '正常问题': '非敏感问题,都归属于正常问题', 22     '敏感问题': '一切涉及政治、色情、歧视、暴恐等违法内容的问题' 23 } 24  25 # create nodes 26 begin = langpipe.LPBegin('begin_node') 27 classifier = langpipe.LPClassifier('classifier', labels_desc) 28 bocha_search = sample_nodes.LPBoChaSearch('bocha_search', 'sk-***') # replace with your own api key 29 aggregator = langpipe.LPSuperAggregator('aggregator', None, True, 'qwen2.5:7b')  # including reference sources 30 end0 = langpipe.LPEnd('end_node_0')  # 正常问题 结束分支 31 end1 = langpipe.LPEnd('end_node_1')  # 敏感问题 结束分支 32  33 # link together 34 begin.link(classifier) 35 classifier.link([bocha_search, end1]) # split into 2 branches automatically 36 bocha_search.link(aggregator) 37 aggregator.link([end0]) 38  39 # input what you want to 40 begin.input(query5, None, False) 41  42 # visualize the pipeline with data flow 43 print('-----board for debug purpose-----') 44 renderer = langpipe.LPBoardRender(node_size=100) 45 renderer.render(begin)

管道显示:

100行代码实现Chat2XX(DB/ Web/ KnowledgeBase)

HTML输出(浏览器渲染效果):

100行代码实现Chat2XX(DB/ Web/ KnowledgeBase)

控制台输出:

>>>>>>>>>>>>>[output][final_out from end_node_0]>>>>>>>>>>>>> {     "content": "周杰伦,1979年1月18日出生于中国台湾省新北市,祖籍福建省永春县。他是一位华语流行乐男歌手、音乐人、演员、导演和编剧。自出道以来,周杰伦凭借其独特的音乐风格和才华横溢的创作能力,在华语乐坛取得了显著成就。nn2000年发行首张个人专辑《Jay》,该专辑融合了R&B、Hip-Hop等多种音乐风格,并且其中主打歌曲《星晴》获得了第24届十大中文金曲优秀国语歌曲金奖。这张专辑不仅让周杰伦一战成名,还带动了华语R&B、嘻哈和中国风的流行<a href="https://baike.baidu.com/item/%E5%91%A8%E6%9D%B0%E4%BC%A6/129156" target="_blank">[1]</a>。nn2001年发行的专辑《范特西》奠定了他融合中西方音乐的风格,并且凭借该专辑获得了第15届台湾金曲奖最佳流行音乐演唱专辑奖。同年,周杰伦还成为美国《时代》杂志封面人物<a href="https://vebaike.com/doc-view-1270.html" target="_blank">[2]</a><a href="http://1zhr9wp4.shshilin.com/mobile/news/show-323436.html" target="_blank">[3]</a>。nn2004年发行的专辑《七里香》在全亚洲首月销量达300万张,并且获得了世界音乐大奖中国区最畅销艺人奖<a href="https://baike.baidu.com/item/%E5%91%A8%E6%9D%B0%E4%BC%A6/129156" target="_blank">[1]</a>。此外,他还主演了个人首部电影《头文字D》,凭借该片获得了第25届香港电影金像奖和第42届台湾电影金马奖的最佳新演员奖<a href="https://vebaike.com/doc-view-1270.html" target="_blank">[2]</a><a href="http://1zhr9wp4.shshilin.com/mobile/news/show-323436.html" target="_blank">[3]</a>。nn从2006年起连续三年获得世界音乐大奖中国区最畅销艺人奖。2017年,周杰伦监制的爱情冒险电影《一万公里的约定》上映,并担任明星经理人的综艺节目《这!就是灌篮》的导师<a href="https://piaofang.maoyan.com/celebrity?id=28427" target="_blank">[4]</a><a href="https://baijiahao.baidu.com/s?id=1820484479709473213" target="_blank">[5]</a>。nn周杰伦不仅在音乐上取得了巨大成功,在商业和设计领域也有涉足。他于2007年成立了杰威尔有限公司,同年还担任了华硕笔电设计师,并入股香港文化传信集团<a href="https://music.apple.com/cn/playlist/%E5%91%A8%E6%9D%B0%E4%BC%A6%E5%86%99%E7%9A%84%E6%AD%8C/pl.5a3e20295f804b298e507226f335e567" target="_blank">[6]</a>。nn此外,周杰伦热心公益慈善事业,多次向中国内地灾区捐款捐物,如在2008年捐款援建希望小学。他还曾担任中国禁毒宣传形象大使<a href="https://baijiahao.baidu.com/s?id=1820484479709473213" target="_blank">[5]</a><a href="https://www.mingrenw.cn/ziliao/3/2970.html" target="_blank">[7]</a>。nn综上所述,周杰伦是华语乐坛独一无二的存在,他的音乐陪伴了许多人的青春,并且他凭借多方面的才华和成就,在全球范围内都享有极高的声誉<a href="https://baike.baidu.com/item/%E5%91%A8%E6%9D%B0%E4%BC%A6/129156" target="_blank">[1]</a><a href="https://vebaike.com/doc-view-1270.html" target="_blank">[2]</a><a href="http://1zhr9wp4.shshilin.com/mobile/news/show-323436.html" target="_blank">[3]</a><a href="https://piaofang.maoyan.com/celebrity?id=28427" target="_blank">[4]</a><a href="https://baijiahao.baidu.com/s?id=1820484479709473213" target="_blank">[5]</a><a href="https://music.apple.com/cn/playlist/%E5%91%A8%E6%9D%B0%E4%BC%A6%E5%86%99%E7%9A%84%E6%AD%8C/pl.5a3e20295f804b298e507226f335e567" target="_blank">[6]</a><a href="https://www.mingrenw.cn/ziliao/3/2970.html" target="_blank">[7]</a>。",     "references": [         "https://baike.baidu.com/item/%E5%91%A8%E6%9D%B0%E4%BC%A6/129156",         "https://vebaike.com/doc-view-1270.html",         "http://1zhr9wp4.shshilin.com/mobile/news/show-323436.html",         "https://piaofang.maoyan.com/celebrity?id=28427",         "https://baijiahao.baidu.com/s?id=1820484479709473213",         "https://music.apple.com/cn/playlist/%E5%91%A8%E6%9D%B0%E4%BC%A6%E5%86%99%E7%9A%84%E6%AD%8C/pl.5a3e20295f804b298e507226f335e567",         "https://www.mingrenw.cn/ziliao/3/2970.html"     ] } <<<<<<<<<<<<<[output][final_out from end_node_0]<<<<<<<<<<<<<

 

更多案例参考

https://github.com/sherlockchou86/PyLangPipe/blob/main/tests/README.md

发表评论

评论已关闭。

相关文章