当前位置: 首页 > news >正文

langchain学习笔记(十)

Bind runtime args | 🦜️🔗 Langchain

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

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"))

相关文章:

  • SMBGhost漏洞技术分析与防御方案
  • 【C++干货基地】揭秘C++11常用特性:内联函数 | 范围for | auto自动识别 | nullptr指针空值
  • 向日葵、Todesk、teamviewer等工具远程连接电脑时第三方应用显示白屏
  • 编程笔记 Golang基础 045 math包
  • chrome选项页面options page配置
  • AI网址啊
  • 【css面试题】BFC
  • 通过QScrollArea寻找最后一个弹簧并且设置弹簧大小
  • OpenCV 4基础篇| OpenCV图像的裁切
  • leetcode移除元素
  • AzerothCore安装记录
  • UniApp项目处理小程序分包
  • HarmonyOS 开发之———应用程序入口—UIAbility的使用
  • Java学习--学生管理系统(残破版)
  • 微信小程序云开发教程——墨刀原型工具入门(Axure导入)
  • ----------
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • docker-consul
  • maven工程打包jar以及java jar命令的classpath使用
  • MaxCompute访问TableStore(OTS) 数据
  • PHP的Ev教程三(Periodic watcher)
  • python大佬养成计划----difflib模块
  • 机器学习学习笔记一
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 聊聊flink的BlobWriter
  • 时间复杂度与空间复杂度分析
  • 数组大概知多少
  • 探索 JS 中的模块化
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • 《码出高效》学习笔记与书中错误记录
  • 国内开源镜像站点
  • ​Distil-Whisper:比Whisper快6倍,体积小50%的语音识别模型
  • ​LeetCode解法汇总2808. 使循环数组所有元素相等的最少秒数
  • # include “ “ 和 # include < >两者的区别
  • #传输# #传输数据判断#
  • (¥1011)-(一千零一拾一元整)输出
  • (2)Java 简介
  • (3)选择元素——(14)接触DOM元素(Accessing DOM elements)
  • (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令...
  • (Matlab)遗传算法优化的BP神经网络实现回归预测
  • (剑指Offer)面试题41:和为s的连续正数序列
  • (力扣题库)跳跃游戏II(c++)
  • (原創) 如何優化ThinkPad X61開機速度? (NB) (ThinkPad) (X61) (OS) (Windows)
  • .java 9 找不到符号_java找不到符号
  • .NET 8.0 中有哪些新的变化?
  • .net 设置默认首页
  • .NET 中 GetProcess 相关方法的性能
  • .net打印*三角形
  • .NET中的Event与Delegates,从Publisher到Subscriber的衔接!
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • [ 渗透测试面试篇 ] 渗透测试面试题大集合(详解)(十)RCE (远程代码/命令执行漏洞)相关面试题
  • [20171113]修改表结构删除列相关问题4.txt
  • [Android]一个简单使用Handler做Timer的例子
  • [BJDCTF2020]The mystery of ip
  • [C++] Boost智能指针——boost::scoped_ptr(使用及原理分析)