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)
相关推荐
chaors1 小时前
从零学RAG0x0d:AdvancedRAG检索后优化
langchain·llm·ai编程
前进的李工1 小时前
LangChain使用之Model IO(提示词模版之PromptTemplate)
开发语言·人工智能·python·langchain
赵小川1 小时前
5分钟跑通 LangChain,第一个 AI Demo(超详细)
langchain·openai·ai编程
console.log('npc')2 小时前
Cursor,Trae,Claude Code如何协作生产出一套前后台app?
前端·人工智能·react.js·设计模式·ai·langchain·ai编程
大傻^4 小时前
LangChain4j AI Services 深度解析:声明式 API 与接口驱动开发
人工智能·langchain·openai·langchain4j
霍格沃兹测试学院-小舟畅学6 小时前
LangChain + DeepSeek 实战拆解:从 LCEL 到智能体,如何真正“做出”一个可控 AI 系统?
java·人工智能·langchain
爱喝可乐的老王6 小时前
LangChain内置中间件总结
中间件·langchain
爱喝可乐的老王6 小时前
LangChain自定义中间件
中间件·langchain
汀沿河6 小时前
3 LangChain 1.0 中间件(Middleware)- after_model、after_agent
前端·中间件·langchain
java1234_小锋7 小时前
基于LangChain的RAG与Agent智能体开发 - 使用LangChain调用大语言模型
人工智能·语言模型·langchain·rag