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)
相关推荐
中国胖子风清扬2 小时前
SpringAI和 Langchain4j等 AI 框架之间的差异和开发经验
java·数据库·人工智能·spring boot·spring cloud·ai·langchain
岁月宁静3 小时前
LangGraph 技术详解:基于图结构的 AI 工作流与多智能体编排框架
前端·python·langchain
岁月宁静3 小时前
LangChain 技术栈全解析:从模型编排到 RAG 实战
前端·python·langchain
人肉推土机4 小时前
推荐一个langchain开发工具包:langchain-dev-utils
langchain·langgraph·多agent·langchain utils
春天的菠菜8 小时前
【LangChain第2章】使用之Model I/O
langchain
idkmn_9 小时前
Agentic AI 基础概念
人工智能·python·深度学习·chatgpt·langchain
lusasky18 小时前
AgentScope、LangChain、AutoGen 全方位对比 + 混用可行性指南
microsoft·langchain
前端阿森纳1 天前
从产品经理视角拆解 LangChain 的抽象设计
langchain·llm·aigc
大模型真好玩1 天前
LangGraph1.0速通指南(一)—— LangGraph1.0 核心概念、点、边
人工智能·langchain·agent
阿里云云原生1 天前
AgentRun Sandbox SDK 正式开源!集成 LangChain 等主流框架,一键开启智能体沙箱新体验
阿里云·langchain·开源·serverless·agentarun