当前位置: 首页 > news >正文

GPT实战系列-LangChain如何构建基通义千问的多工具链

GPT实战系列-LangChain如何构建基通义千问的多工具链

LangChain

GPT实战系列-LangChain如何构建基通义千问的多工具链

GPT实战系列-构建多参数的自定义LangChain工具

GPT实战系列-通过Basetool构建自定义LangChain工具方法

GPT实战系列-一种构建LangChain自定义Tool工具的简单方法

GPT实战系列-搭建LangChain流程简单应用

GPT实战系列-简单聊聊LangChain搭建本地知识库准备

GPT实战系列-LangChain + ChatGLM3构建天气查询助手

GPT实战系列-大模型为我所用之借用ChatGLM3构建查询助手

GPT实战系列-简单聊聊LangChain

大模型查询工具助手之股票免费查询接口

随着OpenAI的GPT-4这样的大型语言模型(LLMs)已经风靡全球,现在让它们自动执行各种任务,如回答问题、翻译语言、分析文本等。LLMs是在交互上真正体验到像“人工智能”。

如何管理这些模块呢?

LangChain在这方面发挥重要作用。LangChain使构建由LLMs驱动的应用程序变得简单,使用LangChain,可以在统一的界面中轻松与不同类型的LLMs进行交互,管理模型版本,管理对话版本,并将LLMs连接在一起。

准备

本例子中,采用通义千问作为LLM

# 引入需要的模块
from langchain.chains import LLMChain, SimpleSequentialChainfrom langchain import PromptTemplateimport os

设置 千问的相关环境,和模型接口函数:

from langchain_community.llms import Tongyi
os.environ["DASHSCOPE_API_KEY"] = "your key"
llm = Tongyi()

构建第一个Prompt链

LangChain可以连接到自己定义的工具,也可以连接到内嵌的tool提供商。此处,先用Prompt构建简单的链路。

# 第一步 prompt工具链
template = "Can you provide a brief summary of the movie {movie_title}? Please keep it concise."first_prompt = PromptTemplate(input_variables=["movie_title"],template=template)chain_one = LLMChain(llm=llm, prompt=first_prompt)

构建第二个Prompt链

# 第二步 prompt工具链second_prompt = PromptTemplate(input_variables=["actor"],template="Can you list three movies featuring {actor}?")chain_two = LLMChain(llm=llm, prompt=second_prompt)

可以看到 两个int 参数:

multiply
multiply(a: int, b: int) -> int - Multiply two numbers.
{'a': {'title': 'A', 'type': 'integer'}, 'b': {'title': 'B', 'type': 'integer'}}

SimpleSequentialChain把链串起来

通过SimpleSequentialChain 把 各个链串起来,形成信息处理流。

# 结合第一和第二链
overall_chain = SimpleSequentialChain(chains=[chain_one, chain_two], verbose=True)final_answer = overall_chain.run("Inception")print(final_answer)

最后打印,实现功能。效果取决于定义的Prompt和模型的能力,得到类似的输出::

> Entering new SimpleSequentialChain chain...
Inception is a 2010 science fiction thriller film directed by Christopher Nolan. The movie follows Dom Cobb (Leonardo DiCaprio), an expert thief who specializes in infiltrating people's dreams to steal their ideas. Cobb is offered a chance to have his criminal history erased if he completes an impossible task: implanting an idea into the subconscious mind of a wealthy businessman, Robert Fischer (Cillian Murphy). To do this, Cobb assembles a team including a chemist, an architect, and a forger, and they delve into multiple layers of dream-sharing, facing challenges like time dilation and the risk of being trapped in their own subconscious. As the dreamscapes become more complex, Cobb's haunted past threatens to derail the mission and jeopardize the lives of his team members. The film explores themes of reality, dreams, and the power of the human mind.
1. The Matrix (1999) - This science fiction action film, directed by the Wachowskis, also blurs the lines between reality and the virtual world. It follows Neo (Keanu Reeves), a computer programmer who discovers that his reality is actually a simulated world created by intelligent machines. With the help of a group of rebels, including Morpheus (Laurence Fishburne) and Trinity (Carrie-Anne Moss), Neo learns to manipulate this simulated reality and fights against the machine-controlled dystopia.2. Interstellar (2014) - Another Christopher Nolan-directed film, Interstellar explores the boundaries of space, time, and human endeavor. Matthew McConaughey plays Cooper, a former pilot and engineer who leads an expedition through a wormhole in search of a new habitable planet for humanity. The movie delves into complex scientific concepts like relativity and the fifth dimension while examining the emotional impact of leaving loved ones behind.3. Paprika (2006) - This Japanese animated psychological science fiction film, directed by Satoshi Kon, revolves around a device that allows therapists to enter and explore their patients' dreams. Dr. Atsuko Chiba, using her alter ego Paprika, must navigate a chaotic dreamscape when the device falls into the wrong hands, causing dream and reality to merge dangerously. The film explores similar themes of dreams and their impact on the human psyche as Inception.> Finished chain.
1. The Matrix (1999) - This science fiction action film, directed by the Wachowskis, also blurs the lines between reality and the virtual world. It follows Neo (Keanu Reeves), a computer programmer who discovers that his reality is actually a simulated world created by intelligent machines. With the help of a group of rebels, including Morpheus (Laurence Fishburne) and Trinity (Carrie-Anne Moss), Neo learns to manipulate this simulated reality and fights against the machine-controlled dystopia.2. Interstellar (2014) - Another Christopher Nolan-directed film, Interstellar explores the boundaries of space, time, and human endeavor. Matthew McConaughey plays Cooper, a former pilot and engineer who leads an expedition through a wormhole in search of a new habitable planet for humanity. The movie delves into complex scientific concepts like relativity and the fifth dimension while examining the emotional impact of leaving loved ones behind.3. Paprika (2006) - This Japanese animated psychological science fiction film, directed by Satoshi Kon, revolves around a device that allows therapists to enter and explore their patients' dreams. Dr. Atsuko Chiba, using her alter ego Paprika, must navigate a chaotic dreamscape when the device falls into the wrong hands, causing dream and reality to merge dangerously. The film explores similar themes of dreams and their impact on the human psyche as Inception.

LangChain是一个Python框架,可以使用LLMs构建应用程序。它与各种模块连接,使与LLM和提示管理,一切变得简单。

觉得有用 收藏 收藏 收藏

点个赞 点个赞 点个赞

End

GPT专栏文章:

GPT实战系列-实战Qwen通义千问在Cuda 12+24G部署方案_通义千问 ptuning-CSDN博客

GPT实战系列-ChatGLM3本地部署CUDA11+1080Ti+显卡24G实战方案

GPT实战系列-Baichuan2本地化部署实战方案

GPT实战系列-让CodeGeeX2帮你写代码和注释_codegeex 中文-CSDN博客

GPT实战系列-ChatGLM3管理工具的API接口_chatglm3 api文档-CSDN博客

GPT实战系列-大话LLM大模型训练-CSDN博客

GPT实战系列-LangChain + ChatGLM3构建天气查询助手

GPT实战系列-大模型为我所用之借用ChatGLM3构建查询助手

GPT实战系列-P-Tuning本地化训练ChatGLM2等LLM模型,到底做了什么?(二)

GPT实战系列-P-Tuning本地化训练ChatGLM2等LLM模型,到底做了什么?(一)

GPT实战系列-ChatGLM2模型的微调训练参数解读

GPT实战系列-如何用自己数据微调ChatGLM2模型训练

GPT实战系列-ChatGLM2部署Ubuntu+Cuda11+显存24G实战方案

GPT实战系列-Baichuan2等大模型的计算精度与量化

GPT实战系列-GPT训练的Pretraining,SFT,Reward Modeling,RLHF

GPT实战系列-探究GPT等大模型的文本生成-CSDN博客

相关文章:

  • 数据库--SQL语言-1
  • 深入了解二叉搜索树:原理、实现与应用
  • C语言-写一个简单的Web服务器(一)
  • uniapp+node.js前后端做帖子模块:发布帖子评论(社区管理平台的小程序)
  • 链表中的经典问题——反转链表
  • C#拾遗补漏之goto跳转语句
  • Centos安装mysql8
  • (day 2)JavaScript学习笔记(基础之变量、常量和注释)
  • 部署LVS+Keepalived高可用群集(抢占模式,非抢占模式,延迟模式)
  • MySQL利用逻辑备份恢复误删的数据库
  • vue 总结
  • redis使用笔记
  • 【Linux】线程封装_互斥
  • 怎么看待Groq
  • Redis缓存三大问题-穿透、击穿、雪崩
  • [ 一起学React系列 -- 8 ] React中的文件上传
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • 《深入 React 技术栈》
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • mysql常用命令汇总
  • V4L2视频输入框架概述
  • Vue小说阅读器(仿追书神器)
  • windows下mongoDB的环境配置
  • 表单中readonly的input等标签,禁止光标进入(focus)的几种方式
  • 浮现式设计
  • 构造函数(constructor)与原型链(prototype)关系
  • 机器人定位导航技术 激光SLAM与视觉SLAM谁更胜一筹?
  • 技术发展面试
  • 少走弯路,给Java 1~5 年程序员的建议
  • 译自由幺半群
  • ​MySQL主从复制一致性检测
  • (+3)1.3敏捷宣言与敏捷过程的特点
  • (9)STL算法之逆转旋转
  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • (第27天)Oracle 数据泵转换分区表
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (七)MySQL是如何将LRU链表的使用性能优化到极致的?
  • (三)终结任务
  • (转)socket Aio demo
  • .equal()和==的区别 怎样判断字符串为空问题: Illegal invoke-super to void nio.file.AccessDeniedException
  • .NET Core 中的路径问题
  • .NET DevOps 接入指南 | 1. GitLab 安装
  • .NET 将混合了多个不同平台(Windows Mac Linux)的文件 目录的路径格式化成同一个平台下的路径
  • .NET/C# 获取一个正在运行的进程的命令行参数
  • .net中生成excel后调整宽度
  • .Net组件程序设计之线程、并发管理(一)
  • /3GB和/USERVA开关
  • @Autowired和@Resource的区别
  • [Angularjs]asp.net mvc+angularjs+web api单页应用
  • [C/C++]_[初级]_[关于编译时出现有符号-无符号不匹配的警告-sizeof使用注意事项]
  • [C++] new和delete
  • [Contest20180313]灵大会议
  • [excel与dict] python 读取excel内容并放入字典、将字典内容写入 excel文件
  • [Godot] 3D拾取
  • [javaSE] GUI(Action事件)