langchain学习(十)

Bind runtime args | 🦜️🔗 Langchain

1、有时,我们希望使用常量参数调用Runnable序列中的Runnable,这些参数不是序列中前一个Runnable的输出的一部分,也不是用户的输入,这时可以用Runnable.bind()

9e9d89da3a0e4d44909e9d14e73880e5.png

from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI
prompt = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "用代数符号写出下面的方程,然后求解。 格式EQUATION:...换行SOLUTION:...换行",
        ),
        ("human", "{equation_statement}"),
    ]
)
model = ChatOpenAI(temperature=0)
runnable = (
    {"equation_statement": RunnablePassthrough()} | prompt | model | StrOutputParser()
)
print(runnable.invoke("x的三次方加7等于12"))
# 使用model.bind,此处限制输入某些字
runnable = (
    {"equation_statement": RunnablePassthrough()}
    | prompt
    | model.bind(stop="SOLUTION")
    | StrOutputParser()
)
print('model.bind:',runnable.invoke("x的三次方加7等于12"))

2、通过bind给openAI模型绑定openAI函数、openAI工具

注意以下的方程求解需要GPT4才能给出正确答案,奇怪的是上面不用bind function的gpt3.5可以回答正确

注意name的key不能为中文

function = {
    "name": "solver",
    "description": "Formulates and solves an equation",
    "parameters": {
        "type": "object",
        "properties": {
            "equation": {
                "type": "string",
                "description": "The algebraic expression of the equation",
            },
            "solution": {
                "type": "string",
                "description": "The solution to the equation",
            },
        },
        "required": ["equation", "solution"],
    },
}
prompt = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "Write out the following equation using algebraic symbols then solve it.",
        ),
        ("human", "{equation_statement}"),
    ]
)
model=model="gpt-3.5-turbo".bind(
    function_call={"name": "solver"}, functions=[function]
)
runnable = {"equation_statement": RunnablePassthrough()} | prompt | model
print(runnable.invoke("x raised to the third plus seven equals 12"))

2bf70ade788d4bb3a9917d3f15d904c1.png

 

 

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值