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

LangChain 核心模块学习:Chains

LangChain 核心模块学习:Chains

对于简单的大模型应用,单独使用语言模型(LLMs)是可以的。

但更复杂的大模型应用需要将 LLMsChat Models 链接在一起 - 要么彼此链接,要么与其他组件链接。

LangChain 为这种“链式”应用程序提供了 Chain 接口。

LangChain 以通用方式定义了 Chain,它是对组件进行调用序列的集合,其中可以包含其他链。

Router Chain: 实现条件判断的大模型调用

这段代码构建了一个可定制的链路系统,用户可以提供不同的输入提示,并根据这些提示获取适当的响应。

主要逻辑:从prompt_infos创建多个LLMChain对象,并将它们保存在一个字典中,然后创建一个默认的ConversationChain,最后创建一个带有路由功能的MultiPromptChain

在这里插入图片描述
from langchain.chains.router import MultiPromptChain
from langchain.llms import OpenAI
from langchain.chains import ConversationChain
from langchain.chains.llm import LLMChain
from langchain.prompts import PromptTemplate

physics_template = “”"你是一位非常聪明的物理教授。
你擅长以简洁易懂的方式回答关于物理的问题。
当你不知道某个问题的答案时,你会坦诚承认。

这是一个问题:
{input}“”"

math_template = “”"你是一位很棒的数学家。你擅长回答数学问题。
之所以如此出色,是因为你能够将难题分解成各个组成部分,
先回答这些组成部分,然后再将它们整合起来回答更广泛的问题。

这是一个问题:
{input}“”"

prompt_infos = [
{
“name”: “物理”,
“description”: “适用于回答物理问题”,
“prompt_template”: physics_template,
},
{
“name”: “数学”,
“description”: “适用于回答数学问题”,
“prompt_template”: math_template,
},
]

llm = OpenAI()

创建一个空的目标链字典,用于存放根据prompt_infos生成的LLMChain。

destination_chains = {}

遍历prompt_infos列表,为每个信息创建一个LLMChain。

for p_info in prompt_infos:
name = p_info[“name”] # 提取名称
prompt_template = p_info[“prompt_template”] # 提取模板
# 创建PromptTemplate对象
prompt = PromptTemplate(template=prompt_template, input_variables=[“input”])
# 使用上述模板和llm对象创建LLMChain对象
chain = LLMChain(llm=llm, prompt=prompt)
# 将新创建的chain对象添加到destination_chains字典中
destination_chains[name] = chain

创建一个默认的ConversationChain

default_chain = ConversationChain(llm=llm, output_key=“text”)

type(default_chain)

使用 LLMRouterChain 实现条件判断调用

这段代码定义了一个chain对象(LLMRouterChain),该对象首先使用router_chain来决定哪个destination_chain应该被执行,如果没有合适的目标链,则默认使用default_chain。

from langchain.chains.router.llm_router import LLMRouterChain, RouterOutputParser
from langchain.chains.router.multi_prompt_prompt import MULTI_PROMPT_ROUTER_TEMPLATE

从prompt_infos中提取目标信息并将其转化为字符串列表

destinations = [f"{p[‘name’]}: {p[‘description’]}" for p in prompt_infos]

使用join方法将列表转化为字符串,每个元素之间用换行符分隔

destinations_str = “\n”.join(destinations)

根据MULTI_PROMPT_ROUTER_TEMPLATE格式化字符串和destinations_str创建路由模板

router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(destinations=destinations_str)

创建路由的PromptTemplate

router_prompt = PromptTemplate(
template=router_template,
input_variables=[“input”],
output_parser=RouterOutputParser(),
)

使用上述路由模板和llm对象创建LLMRouterChain对象

router_chain = LLMRouterChain.from_llm(llm, router_prompt)

print(destinations)

print(destinations_str)

print(router_template)

创建MultiPromptChain对象,其中包含了路由链,目标链和默认链。

chain = MultiPromptChain(
router_chain=router_chain,
destination_chains=destination_chains,
default_chain=default_chain,
verbose=True,
)

print(chain.run(“黑体辐射是什么??”))

print(
chain.run(
“大于40的第一个质数是多少,使得这个质数加一能被3整除?”
)
)

router_chain.verbose = True

print(chain.run(“黑洞是什么?”))

http://www.xdnf.cn/news/81289.html

相关文章:

  • USB 共享神器 VirtualHere 局域网内远程使用打印机与扫描仪
  • 安宝特科技 | Vuzix Z100智能眼镜+AugmentOS:重新定义AI可穿戴设备的未来——从操作系统到硬件生态,如何掀起无感智能革命?
  • 麒麟系统网络连接问题排查
  • 乐视系列玩机------乐视2 x620红灯 黑砖刷写教程以及新版刷写工具的详细释义
  • C++IO流
  • AI 数字短视频数字人源码开发:多维赋能短视频生态革新​
  • 图像预处理-直方图均衡化
  • 卷积神经网络迁移学习:原理与实践指南
  • GSAP 动画引擎实战:打造丝滑动效交互组件库
  • 在 40 亿整数中捕获“恰好出现两次”的数字
  • Git管理
  • 离散化区间和 java c++
  • Springboot整合MyBatisplus和快速入门
  • lspci的资料
  • crewai与langchain分析某公司股票是否可购买
  • prtobuf的原理
  • 2.Spring MVC与WebFlux响应式编程
  • UOS+N 卡 + CUDA 环境下 X86 架构 DeepSeek 基于 vLLM 部署与 Dify 平台搭建指南
  • Nature Communications 面向形状可编程磁性软材料的数据驱动设计方法—基于随机设计探索与神经网络的协同优化框架
  • 30分钟编写十大排序算法完成
  • 施磊老师基于muduo网络库的集群聊天服务器(四)
  • 含锡废水具有显著的回收价值
  • kvm下的ceph主机启动io请求统计
  • AOSP Android14 Launcher3——RecentsView最近任务数据加载
  • Hive学习
  • 【数字图像处理】立体视觉基础(1)
  • 禁止ubuntu自动更新
  • 基于nlohmann/json 实现 从C++对象转换成JSON数据格式
  • c++内存池
  • 调整IntelliJ IDEA中当前文件所在目录的显示位置