马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
1. langchain的function calling 非常简便
- 在方法名分析方法用途和参数作用
- 增长@tool 标签
- langchain方法自动把@tool转为方法界说,后续方法调用都很简便
下面代码用支持单函数与多函数调用,自己体验一下
query = “3 加 4的和 的 5 倍是多少?” 这个会调用两次函数
query = " 4的 5 倍是多少?"
2. 代码
- from langchain_core.messages import HumanMessage
- from langchain_core.tools import tool
- from langchain_openai import ChatOpenAI
- @tool
- def add(a: int, b: int) -> int:
- """Add two integers together.
- Args:
- a: The first integer.
- b: The second integer.
- """
- return a + b
- @tool
- def multiply(a: int, b: int) -> int:
- """Multiply two integers together.
- Args:
- a: The first integer.
- b: The second integer.
- """
- return a * b
- import json
- # model_name = 'gpt-4o-mini'
- # model_name = 'gpt-4'
- # llm = ChatOpenAI(model_name=model_name)
- llm = ChatOpenAI()
- llm_with_tools = llm.bind_tools([add, multiply])
- query = "3 加 4的和 的 5 倍是多少?"
- # query = " 4的 5 倍是多少?"
- messages = [HumanMessage(content=query)]
- output = llm_with_tools.invoke(messages)
- print(output)
- print("------------tool_calls:")
- print(json.dumps(output.tool_calls, indent=4))
- messages.append(output)
- avaliable_tools = {"add":add, "multiply": multiply}
- for tool_call in output.tool_calls:
- selected_tools = avaliable_tools[tool_call["name"].lower()] # tool_call就是一个字典
- tool_msg = selected_tools.invoke(tool_call)
- messages.append(tool_msg)
- print("tool_msg:, type:", type(tool_msg)) # <class 'langchain_core.messages.tool.ToolMessage'>
- print(tool_msg)
- # content='20' name='multiply' tool_call_id='call_e5EY7klNZlBD8W68y7X0BcYD'
- new_output = llm_with_tools.invoke(messages)
- for message in messages:
- print("message.dump:")
- print(json.dumps(message.model_dump(), indent=4, ensure_ascii=False))
- print(new_output.content)
复制代码 3. 实验效果:
- C:\ProgramData\anaconda3\envs\gptLearning\python.exe E:\workspace\gptLearning\gptLearning\ls10\functionCalling\ls01_tools.py
- content='' additional_kwargs={'tool_calls': [{'id': 'call_0HGpTdeo2zm9G6zZPbewVi8g', 'function': {'arguments': '{"a": 3, "b": 4}', 'name': 'add'}, 'type': 'function'}, {'id': 'call_m6Uc0OfQ9ea0uTyFNbwEWIBq', 'function': {'arguments': '{"a": 7, "b": 5}', 'name': 'multiply'}, 'type': 'function'}], 'refusal': None} response_metadata={'token_usage': {'completion_tokens': 49, 'prompt_tokens': 121, 'total_tokens': 170, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': 'fp_0165350fbb', 'finish_reason': 'tool_calls', 'logprobs': None} id='run-e83caecd-ca6f-4851-b35e-b5ea8f2804a2-0' tool_calls=[{'name': 'add', 'args': {'a': 3, 'b': 4}, 'id': 'call_0HGpTdeo2zm9G6zZPbewVi8g', 'type': 'tool_call'}, {'name': 'multiply', 'args': {'a': 7, 'b': 5}, 'id': 'call_m6Uc0OfQ9ea0uTyFNbwEWIBq', 'type': 'tool_call'}] usage_metadata={'input_tokens': 121, 'output_tokens': 49, 'total_tokens': 170, 'input_token_details': {}, 'output_token_details': {}}
- ------------tool_calls:
- [
- {
- "name": "add",
- "args": {
- "a": 3,
- "b": 4
- },
- "id": "call_0HGpTdeo2zm9G6zZPbewVi8g",
- "type": "tool_call"
- },
- {
- "name": "multiply",
- "args": {
- "a": 7,
- "b": 5
- },
- "id": "call_m6Uc0OfQ9ea0uTyFNbwEWIBq",
- "type": "tool_call"
- }
- ]
- tool_msg:, type: <class 'langchain_core.messages.tool.ToolMessage'>
- content='7' name='add' tool_call_id='call_0HGpTdeo2zm9G6zZPbewVi8g'
- tool_msg:, type: <class 'langchain_core.messages.tool.ToolMessage'>
- content='35' name='multiply' tool_call_id='call_m6Uc0OfQ9ea0uTyFNbwEWIBq'
- message.dump:
- {
- "content": "3 加 4的和 的 5 倍是多少?",
- "additional_kwargs": {},
- "response_metadata": {},
- "type": "human",
- "name": null,
- "id": null,
- "example": false
- }
- message.dump:
- {
- "content": "",
- "additional_kwargs": {
- "tool_calls": [
- {
- "id": "call_0HGpTdeo2zm9G6zZPbewVi8g",
- "function": {
- "arguments": "{"a": 3, "b": 4}",
- "name": "add"
- },
- "type": "function"
- },
- {
- "id": "call_m6Uc0OfQ9ea0uTyFNbwEWIBq",
- "function": {
- "arguments": "{"a": 7, "b": 5}",
- "name": "multiply"
- },
- "type": "function"
- }
- ],
- "refusal": null
- },
- "response_metadata": {
- "token_usage": {
- "completion_tokens": 49,
- "prompt_tokens": 121,
- "total_tokens": 170,
- "completion_tokens_details": null,
- "prompt_tokens_details": null
- },
- "model_name": "gpt-3.5-turbo-0125",
- "system_fingerprint": "fp_0165350fbb",
- "finish_reason": "tool_calls",
- "logprobs": null
- },
- "type": "ai",
- "name": null,
- "id": "run-e83caecd-ca6f-4851-b35e-b5ea8f2804a2-0",
- "example": false,
- "tool_calls": [
- {
- "name": "add",
- "args": {
- "a": 3,
- "b": 4
- },
- "id": "call_0HGpTdeo2zm9G6zZPbewVi8g",
- "type": "tool_call"
- },
- {
- "name": "multiply",
- "args": {
- "a": 7,
- "b": 5
- },
- "id": "call_m6Uc0OfQ9ea0uTyFNbwEWIBq",
- "type": "tool_call"
- }
- ],
- "invalid_tool_calls": [],
- "usage_metadata": {
- "input_tokens": 121,
- "output_tokens": 49,
- "total_tokens": 170,
- "input_token_details": {},
- "output_token_details": {}
- }
- }
- message.dump:
- {
- "content": "7",
- "additional_kwargs": {},
- "response_metadata": {},
- "type": "tool",
- "name": "add",
- "id": null,
- "tool_call_id": "call_0HGpTdeo2zm9G6zZPbewVi8g",
- "artifact": null,
- "status": "success"
- }
- message.dump:
- {
- "content": "35",
- "additional_kwargs": {},
- "response_metadata": {},
- "type": "tool",
- "name": "multiply",
- "id": null,
- "tool_call_id": "call_m6Uc0OfQ9ea0uTyFNbwEWIBq",
- "artifact": null,
- "status": "success"
- }
- 3 加 4 的和是 7,7 的 5 倍是 35。
- Process finished with exit code 0
- ``
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |