langchain学习笔记之工作流编排

1.LCEL

工作流编排工具,可以从基本组件构建复杂任务链条,支持流处理、并行处理、日志记录等开箱即用的功能。

2.ruanable interface

简化了自定义链的创建流程。是一个标准接口,包括

stream:返回响应的数据块

invoke:对输入调用链

batch:对输入列表调用链

还有相应的异步方法,与asyncio一起使用await语法以实现并发:

astream:异步返回响应的数据块

ainvoke:异步对输入调用链

abatch:异步对输入列表调用链

astream_log:异步返回中间步骤以及最终响应

astream_event:beta流式传输链中发生的事件

同步streamAPI:

from langchain_openai import ChatOpenAI
from pydantic import SecretStr
model = ChatOpenAI(model='deepseek-chat',
                    api_key=SecretStr('mykey'),
                    base_url='https://api.deepseek.com')
chunks =[]
for chunk in model.stream("迪迦奥特曼有几种形态"):
    chunks.append(chunk)
    print(chunk.content,end='',flush=True)

异步调用:

from langchain_openai import ChatOpenAI
from pydantic import SecretStr
import asyncio
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template("请和我介绍一下{aoteman}奥特曼")
model = ChatOpenAI(model='deepseek-chat',
                    api_key=SecretStr('mykey'),
                    base_url='https://api.deepseek.com')
para = StrOutputParser()
chain = prompt|model|para


async def dijia():
    async for chunk in chain.astream({"aoteman":"迪迦"}):
        print(chunk,end='',flush=True)
"""async def yake():
    async for chunk in chain.astream({"aoteman": "亚刻"}):
        print(chunk, end='', flush=True)"""
asyncio.run(dijia())

事件查看:

from langchain_openai import ChatOpenAI
from pydantic import SecretStr
import asyncio
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate

#prompt = ChatPromptTemplate.from_template("请和我介绍一下{aoteman}奥特曼")
model = ChatOpenAI(model='deepseek-chat',
                    api_key=SecretStr('mykey'),
                    base_url='https://api.deepseek.com')
'''para = StrOutputParser()
chain = prompt|model|para
'''

"""async def dijia():
    async for chunk in chain.astream({"aoteman":"迪迦"}):
        print(chunk,end='',flush=True)"""
"""async def yake():
    async for chunk in chain.astream({"aoteman": "亚刻"}):
        print(chunk, end='', flush=True)"""
async  def event_stream():
    events = []
    async for event in model.astream_events(input = '你好',version='v2'):
        events.append(event)
    print(events)
asyncio.run(event_stream())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一尾清风915

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值