当前位置: 首页 > 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(“黑洞是什么?”))

相关文章:

  • 配置spark
  • 嵌入式:ARM公司发展史与核心技术演进
  • 基于函数下降趋势的FISTA算法
  • el-form里面的input输入框按回车自动刷新页面的问题
  • Hyper-V 管理工具
  • 12.5/Q1,GBD高分文章解读
  • 深度强化学习 pdf 董豪| 马尔科夫性质,马尔科夫过程,马尔科夫奖励过程,马尔科夫决策过程
  • 香港免费云服务器申请教程,配置4核8G
  • 制作一款打飞机游戏17:敌人
  • G1 人形机器人软件系统架构与 Python SDK
  • SystemV-消息队列与责任链模式
  • Discuz!+DeepSeek:传统论坛的智能化蜕变之路
  • 鸿蒙移动应用开发--渲染控制实验
  • C++异步操作 - future async package_task promise
  • Vue项目依赖注入的使用
  • 深度学习模型搭建的基础原理详细介绍
  • C++学习笔记(三十六)——STL之排序算法
  • Java中内部类
  • 电脑硬盘丢失怎么找回?解决硬盘数据恢复的2种方法
  • win10中打开python的交互模式
  • 神舟十九号航天员乘组计划于4月29日返回东风着陆场
  • 佩索阿稳定常销,陀翁不断加印,青少年喜欢黑塞
  • 云南城投去年营收约19.29亿元,亏损4945万元
  • 细说汇率 ⑬ 美元进入“全是坏消息”阶段
  • A股三大股指涨跌互现:黄金股再度走强,两市成交10900亿元
  • 中保协:当前普通型人身保险产品预定利率研究值为2.13%