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

langchain学习(十三)

一、将其他chain的输入作为新chain的输出,三种方式

1、采用连接符"|",推荐
2、采用lamba表达式输入
3、采用pipe方法
from langchain_community.chat_models import ChatOllama
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableParallel
prompt=ChatPromptTemplate.from_template("tell me a joke about {topic}"
)
model=ChatOllama(model='qwen:7b')
chain=prompt|model|StrOutputParser()
##批量
# res=chain.batch([{'topic':'bear'},{'topic':'chair'}])
##chain的连接,本例子通过一个chain分析模型的输出结果
analysis_promt=ChatPromptTemplate.from_template("is this a funcy joke?{joke}"
)
###方式1
composed_chian={"joke":chain}|analysis_promt|model|StrOutputParser()
res=composed_chian.invoke({'topic':"bear"})
###方式2
composed_chian_with_lamba=(chain|(lambda x:{"joke":x})|analysis_promt|model|StrOutputParser()
)
res=composed_chian_with_lamba.invoke({'topic':"bear"})
###方式3
composed_chain_with_pipe=(RunnableParallel({'joke':chain}).pipe(analysis_promt).pipe(model).pipe(StrOutputParser())
)
res=composed_chain_with_pipe.invoke({'topic':'bear'})
print(res)

二、RunnableParallel

并行,每个值都是用RunnableParallel的整体输入调用的,使前一个输出格式匹配下一个输入
from langchain_community.vectorstores import FAISS
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough,RunnableParallel
from langchain_community.chat_models import ChatOllama
from langchain_community.embeddings import OllamaEmbeddings
vectorstore=FAISS.from_texts(['harrison worked at kensho'],embedding=OllamaEmbeddings(model='qwen:7b')
)
retriever=vectorstore.as_retriever()
template='''
Answer the question based only on the following context:{context}
Question:{question}
'''
prompt=ChatPromptTemplate.from_template(template)
model=ChatOllama(model='qwen:7b')
retrieval_chain=({'context':retriever,"question":RunnablePassthrough()}##4种等价# RunnableParallel({"context":retriever,"question":RunnablePassthrough()})# RunnableParallel(context=retriever,question=RunnablePassthrough())# {"context":retriever,"question":itemgetter("question")}|prompt|model|StrOutputParser()
)
res=retrieval_chain.invoke({"question":"where did harrison work"})
print(res)

http://www.lryc.cn/news/357756.html

相关文章:

  • 【Nginx】深入解析Nginx配置文件
  • 深入了解Nginx(一):Nginx核心原理
  • 产品经理-流程图结构图(四)
  • 15、Spring系统-AOP
  • 服务器感染了. rmallox勒索病毒,如何确保数据文件完整恢复?
  • [每日一练]按日期分组销售产品的最优解法
  • 免费wordpress中文主题
  • 单链表经典算法题理解
  • STM32的时钟介绍
  • FindBI学习总结
  • k8s——Pod详解
  • Visual Studio 的调试
  • mysql语句大全及用法
  • 如何找出真正的交易信号?Anzo Capital昂首资本总结7个
  • JavaScript-内存分配
  • 理论知识.质数打表
  • FFMPEG+ANativeWinodow渲染播放视频
  • 使用AXI MIG/Proc Sys Reset
  • Android基础-Kotlin语言的作用及优缺点
  • 手机投屏技巧:手机怎么投屏到电脑显示屏上?精选6招解决!
  • 内存函数<C语言>
  • 华为校招机试 - LRU模拟(20240515)
  • AI预测福彩3D采取888=3策略+和值012路一缩定乾坤测试5月29日预测第5弹
  • 03_前端三大件CSS
  • 十种常用数据分析模型
  • salesforce 公式字段 判断一个字段是否在某个多选列表中
  • C++STL容器系列(三)list的详细用法和底层实现
  • IEEE Latex模版踩雷避坑指南
  • 【C++】类与对象——多态详解
  • WordPress建网站公司 建易WordPress建站