langchain学习(十三)

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

1、采用连接符"|",推荐
2、采用lamba表达式输入
3、采用pipe方法
python 复制代码
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的整体输入调用的,使前一个输出格式匹配下一个输入
python 复制代码
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)
相关推荐
code bean2 小时前
【LangChain】 文本分割器全景指南:从 RecursiveCharacterTextSplitter 到各类分割器对比
人工智能·自然语言处理·langchain
学计算机的计算基2 小时前
2026 年 AI 助手三国杀:Claude Code vs 腾讯马维斯 vs MiniMax Mavis,我同时用了三周,结论很意外
java·人工智能·python·算法·langchain
VipSoft3 小时前
LangChain 入门 Tools 工具
langchain
wuhen_n3 小时前
RAG 第一步:多格式文档加载与文本预处理实战
前端·langchain·ai编程
Irissgwe6 小时前
十、LangGraph能力详解:工作流的常见模式
python·langchain·ai编程·工作流·langgraph
雪碧聊技术7 小时前
LangChain实战:AI私厨管家—需求分析、代码实现
langchain
Irissgwe8 小时前
十、LangGraph能力详解:LangGraph 的其他特性
python·ai·langchain·langgraph
兆。20 小时前
Agent_RAG_智能食谱推荐系统
langchain·智能体
小刘|1 天前
揭秘RAG:检索增强生成技术解析
langchain·rag
菜到离谱但坚持1 天前
【小白零基础】RAG+LangChain 搭建私有知识库问答系统(完整可运行代码+超详细教程+避坑指南)
python·langchain·rag