langchain学习(八)

RunnableLambda: Run Custom Functions | 🦜️🔗 Langchain

可以在pipeline中使用任意函数,但要注意所有的输入都只能是“1”个参数,当函数需要多个参数时需要采用字典来包装

itemgetter用法见langchain学习笔记(六)_runnablepassthrough-CSDN博客
from operator import itemgetter
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableLambda
from langchain_community.chat_models import ChatOllama
def single_arg(arg):#单个参数
    return arg
def _multiple_arg(arg1, arg2):
    return arg1*arg2
def multiple_arg(_dict):#多个参数需要通过字典包装,逻辑在调用的函数内实现
    return _multiple_arg(_dict["arg1"], _dict["arg2"])
prompt = ChatPromptTemplate.from_template("what is {a} + {b}")
model = ChatOllama(model="qwen:0.5b-chat", temperature=0)
chain1 = prompt | model
chain = (
    {
        "a": itemgetter("arg1") | RunnableLambda(single_arg),
        "b": {"arg1": itemgetter("arg1"), "arg2": itemgetter("arg2")}| RunnableLambda(multiple_arg),
    }
    | prompt
    | model
)
print(chain.invoke({'arg1':1,'arg2':2}))

 

RunnableConfig用于将回调、标记和其他配置信息传递嵌套运行。

from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableConfig,RunnableLambda
import json
def parse_or_fix(text: str, config: RunnableConfig):
    fixing_chain = (
        ChatPromptTemplate.from_template(
            "Fix the following text:\n\n```text\n{input}\n```\nError: {error}"
            " Don't narrate, just respond with the fixed data."
        )
        | ChatOpenAI()
        | StrOutputParser()
    )
    for _ in range(3):
        try:
            return json.loads(text)
        except Exception as e:
            text = fixing_chain.invoke({"input": text, "error": e}, config)
    return "Failed to parse"

from langchain.callbacks import get_openai_callback
with get_openai_callback() as cb:
    output = RunnableLambda(parse_or_fix).invoke(
        "{foo: bar}", {"tags": ["my-tag"], "callbacks": [cb]}
    )
    print(output)
    print(cb)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值