openai-java icon indicating copy to clipboard operation
openai-java copied to clipboard

Parallel function calling多个tool返回,token计算有误

Open SoulBlaze4j opened this issue 2 years ago • 2 comments

使用Parallel function calling,会返回多个role为tool的message。 调用模型为gpt-4-1106-preview

第一次tool calling 先不讨论,因为一般人不会用流式调用第一次tool calls,

第二次tool calls调用prompt比官方多了1。 例子:

{
    "messages": [
        {
            "role": "system",
            "content": "你是一个数学达人"
        },
        {
            "role": "user",
            "content": "两道计算题 20+70; 30x100"
        },
        {
            "role": "assistant",
            "content": null,
            "tool_calls": [
                {
                    "id": "call_ouziXPZBrGmtdxh5BnBooKI3",
                    "type": "function",
                    "function": {
                        "name": "plus",
                        "arguments": "{\"numbers\": [20, 70]}"
                    }
                },
                {
                    "id": "call_jxP70EbkqL5L78EMVK76jFDh",
                    "type": "function",
                    "function": {
                        "name": "product",
                        "arguments": "{\"numbers\": [30, 100]}"
                    }
                }
            ]
        },
        {
            "tool_call_id": "call_ouziXPZBrGmtdxh5BnBooKI3",
            "role": "tool",
            "name": "plus",
            "content": "{\"numbers\": [20, 70], \"result\": \"90\"}"
        },
        {
            "tool_call_id": "call_jxP70EbkqL5L78EMVK76jFDh",
            "role": "tool",
            "name": "product",
            "content": "{\"numbers\": [30, 100], \"result\": \"30000\"}"
        }
    ]
}

OpenAI返回

"usage": {
    "prompt_tokens": 121,
    "completion_tokens": 29,
    "total_tokens": 150
  }

本库返回 prompt_tokens 122

SoulBlaze4j avatar Feb 02 '24 10:02 SoulBlaze4j

When tool message's size equal 1, it use String to calculate even if the String is a JSON format. When the size is large than 1 and tool message's content is a valid JSON, it will be calculated just like arguments.

forestwanglin avatar Feb 03 '24 05:02 forestwanglin

这个用例还是差了一些和官方返回差了点 本库prompt返回200, 官方usage返回197

{
    "model": "gpt-4-1106-preview",
    "messages": [
        {
            "role": "system",
            "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."
        },
        {
            "role": "user",
            "content": "what is the weather going to be like in San Francisco and Glasgow over the next 4 days"
        },
        {
            "role": "assistant",
            "content": null,
            "tool_calls": [
                {
                    "id": "call_fgcP33C9qI4003cva1nXA362",
                    "type": "function",
                    "function": {
                        "name": "get_n_day_weather_forecast",
                        "arguments": "{\"location\": \"San Francisco, CA\", \"format\": \"fahrenheit\", \"num_days\": 4}"
                    }
                },
                {
                    "id": "call_wyNLB4rCWivuMVBKPrMF2Pk1",
                    "type": "function",
                    "function": {
                        "name": "get_n_day_weather_forecast",
                        "arguments": "{\"location\": \"Glasgow\", \"format\": \"celsius\", \"num_days\": 4}"
                    }
                }
            ]
        },
        {
            "tool_call_id": "call_fgcP33C9qI4003cva1nXA362",
            "role": "tool",
            "name": "get_n_day_weather_forecast",
            "content": "{\"location\": \"San Francisco, CA\", \"format\": \"celsius\", \"num_days\": 4, \"temperature\": \"24\"}"
        },
        {
            "tool_call_id": "call_wyNLB4rCWivuMVBKPrMF2Pk1",
            "role": "tool",
            "name": "get_n_day_weather_forecast",
            "content": "{\"location\": \"Glasgow\", \"format\": \"celsius\", \"num_days\": 4, \"temperature\": \"21\"}"
        }
    ]
}

SoulBlaze4j avatar Feb 03 '24 09:02 SoulBlaze4j