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)
相关推荐
我爱学Python!9 小时前
基于 LangChain 的自动化测试用例的生成与执行
人工智能·自然语言处理·langchain·自动化·llm·测试用例·大语言模型
贪玩懒悦2 天前
用langchain+streamlit应用RAG实现个人知识库助手搭建
人工智能·ai·语言模型·langchain·aigc
AI-智能2 天前
《开源大模型食用指南》,一杯奶茶速通大模型!新增Examples最佳实践!
人工智能·自然语言处理·langchain·开源·prompt·ai大模型
AI知识分享官5 天前
大模型增量训练--基于transformer制作一个大模型聊天机器人
人工智能·深度学习·算法·数据挖掘·langchain·机器人·transformer
weijie.zwj7 天前
LLM基础概念:Prompt
人工智能·python·langchain
玩转AI大模型7 天前
AI产品经理学习路径:从零基础到精通,从此篇开始!
人工智能·学习·语言模型·自然语言处理·langchain·transformer·产品经理
高垚淼8 天前
如何构建智能应用:深入探索Langchain的强大功能与应用潜力
人工智能·python·langchain
AI-入门8 天前
AI大模型:是走向新的巅峰还是陷入发展的僵局?
数据库·人工智能·缓存·langchain·prompt·agi
weijie.zwj9 天前
11. LCEL:LangChain Expression Language
人工智能·langchain
weijie.zwj9 天前
9. LangChain流式输出
人工智能·python·langchain