跳到主要内容

如何绑定模型特定的工具

提供程序对于格式化工具模式采用不同的约定。例如,OpenAI 使用如下格式:

  • type:工具的类型。在撰写本文时,这始终是 "function"
  • function:包含工具参数的对象。
  • function.name:要输出的模式的名称。
  • function.description:要输出的模式的高级描述。
  • function.parameters:要提取的模式的嵌套详细信息,格式为 JSON 模式字典。

如果愿意,我们也可以将这种模型特定的格式直接绑定到模型。这是一个例子:

from langchain_openai import ChatOpenAI

model = ChatOpenAI()

model_with_tools = model.bind(
tools=[
{
"type": "function",
"function": {
"name": "multiply",
"description": "Multiply two integers together.",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "number", "description": "First integer"},
"b": {"type": "number", "description": "Second integer"},
},
"required": ["a", "b"],
},
},
}
]
)

model_with_tools.invoke("Whats 119 times 8?")
API 参考:ChatOpenAI
AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_mn4ELw1NbuE0DFYhIeK0GrPe', 'function': {'arguments': '{"a":119,"b":8}', 'name': 'multiply'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 17, 'prompt_tokens': 62, 'total_tokens': 79}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_c2295e73ad', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-353e8a9a-7125-4f94-8c68-4f3da4c21120-0', tool_calls=[{'name': 'multiply', 'args': {'a': 119, 'b': 8}, 'id': 'call_mn4ELw1NbuE0DFYhIeK0GrPe'}])

这在功能上等同于 bind_tools() 方法。


此页面是否对您有所帮助?