简单剖析qwen-agent回答是怎么获取tool的

openai是一家伟大的公司(虽然是closedai),当他们提出agent的概念后,就很神奇。之前通过langchain的langgraph进行写demo,就很好奇,他是怎么基于我的话自动去识别这句话是大模型的闲聊,那句话是大模型去调用tool

1.现象

1.和大模型打招呼,大模型知道回答,这没啥稀奇

2.可是当问它某个地方的天气怎样后,它内部知道去调用工具就很神奇

2.简单分析

通过langchain的简单分析,是当交流天气时候,

大模型输出工具的名字name,以及对应的工具入参args。
然后本地的tool函数进行对应的函数调用,
然后结果再塞给大模型进行润色。

AIMessage(content=[{'id': 'toolu_01Y5EK4bw2LqsQXeaUv8iueF', 'input': {'query': 'weather in san francisco'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}], response_metadata={'id': 'msg_0132wQUcEduJ8UKVVVqwJzM4', 'model': 'claude-3-sonnet-20240229', 'stop_reason': 'tool_use', 'stop_sequence': None, 'usage': {'input_tokens': 269, 'output_tokens': 61}}, id='run-26d5e5e8-d4fd-46d2-a197-87b95b10e823-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'weather in san francisco'}, 'id': 'toolu_01Y5EK4bw2LqsQXeaUv8iueF'}], usage_metadata={'input_tokens': 269, 'output_tokens': 61, 'total_tokens': 330}),

也就是大模型当遇到需要调用工具的时候,就输出对应的工具名称和入参。
但是问题是,这是怎么回事?

3.基于qwen-agent进行剖析

要想支持tool_call,首先是有个训练集的,输入是一些问题,输出是一个json,如这里 https://hf-mirror.com/datasets/Salesforce/xlam-function-calling-60k

{
  "query": "Find the sum of all the multiples of 3 and 5 between 1 and 1000. Also find the product of the first five prime numbers.",
  "tools": [
    {
      "name": "math_toolkit.sum_of_multiples",
      "description": "Find the sum of all multiples of specified numbers within a specified range.",
      "parameters": {
        "lower_limit": {
          "type": "int",
          "description": "The start of the range (inclusive).",
          "required": true
        },
        "upper_limit": {
          "type": "int",
          "description": "The end of the range (inclusive).",
          "required": true
        },
        "multiples": {
          "type": "list",
          "description": "The numbers to find multiples of.",
          "required": true
        }
      }
    },
    {
      "name": "math_toolkit.product_of_primes",
      "description": "Find the product of the first n prime numbers.",
      "parameters": {
        "count": {
          "type": "int",
          "description": "The number of prime numbers to multiply together.",
          "required": true
        }
      }
    }
  ],
  "answers": [
    {
      "name": "math_toolkit.sum_of_multiples",
      "arguments": {
        "lower_limit": 1,
        "upper_limit": 1000,
        "multiples": [3, 5]
      }
    },
    {
      "name": "math_toolkit.product_of_primes",
      "arguments": {
        "count": 5
      }
    }
  ]
}

所以大模型就会生成一个完整的json,那qwen-agent是怎么回事呢如下面的截图,
miniconda3/lib/python3.10/site-packages/qwen_agent/llm/function_calling.py
下面的图片意思是:qwen是需要qwen-agent在调用qwen的时候,输入一些补充的prompt

下面图片的意思是,补充的prompt是补充在system_prompt的最后面

下面图片的意思是,qwen大模型的输出会有前面的输出文字,中间的toolcall部分,和后面的一些文字,为了保险,就需要处理下,把前后的文字弄掉,只输出中间的tool call部分

4.openai-client的包

在openai-client中找了下没qwen-agent这样的prompt的处理和输出的llm的处理,这个人有2种猜测
1)这些操作被隐藏在服务端了,但是觉得概率不大
2)还是大模型训练的好,通过function call的那些数据集进行大模型的训练,使得大模型能直接输出一个完整的json,openai-client就直接进行解析就行了。

posted @ 2024-07-15 17:34  仙守  阅读(13)  评论(0编辑  收藏  举报