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)
相关推荐
Mr.朱鹏1 小时前
9-检索增强生成RAG详解
python·gpt·langchain·大模型·llm·rag
怪祝浙2 小时前
AI学习-LangChain实战-多模态识别agent
人工智能·学习·langchain
情绪总是阴雨天~2 小时前
深入理解A2A协议:从零搭建多Agent协作系统实战
python·langchain·langgraph·a2a
C137的本贾尼2 小时前
融会贯通:打造完整的 RAG 问答链
python·langchain
暗不需求4 小时前
深入浅出 LangChain Memory:从无状态到有记忆的智能对话
面试·langchain·ai编程
怪祝浙4 小时前
AI实战之LangChain开发(prompt;tools;memory)
langchain
古怪今人4 小时前
大语言模型运行工具及格式 Ollama操作大模型 LangChain应用开发框架【2026】
人工智能·语言模型·langchain
禁默6 小时前
解密 LangChain:LLM 应用开发的核心框架与“超级武器”
android·adb·langchain·vibe coding
BU摆烂会噶1 天前
【LangGraph】短期记忆与中断行为
人工智能·python·langchain·人机交互
nix.gnehc1 天前
LangX实战:从Spring生态理解LLM应用开发
人工智能·langchain·langgraph·langfuse