abetlen / llama-cpp-python

Python bindings for llama.cpp
https://llama-cpp-python.readthedocs.io
MIT License
8.17k stars 974 forks source link

Assistant message with tool_calls and without content raises an error #1805

Open feloy opened 1 month ago

feloy commented 1 month ago

Prerequisites

Please answer the following questions for yourself before submitting an issue.

Expected Behavior

Sending this request:

{
  "model" : my-model,
  "messages" : [ {
    "role" : "system",
    "content" : "You are an helpful assistant"
  }, {
    "role" : "user",
    "content" : "What is the square root of 101?"
  }, {
    "role" : "assistant",
    "tool_calls" : [ {
      "id" : "call__0_sqrt_cmpl-c359a11f-68fa-4b8e-9960-1917b96a8b23",
      "type" : "function",
      "function" : {
        "name" : "sqrt",
        "arguments" : "{\"arg0\": 101}"
      }
    } ]
  }, {
    "role" : "tool",
    "tool_call_id" : "call__0_sqrt_cmpl-c359a11f-68fa-4b8e-9960-1917b96a8b23",
    "content" : "10.05"
  } ],
  "temperature" : 0.7
}

the request should be valid.

Current Behavior

The request raises the following error:

{
  "error":{
    "message":"[
      {
        'type': 'literal_error', 
        'loc': ('body', 'messages', 2, 'typed-dict', 'role'), 
        'msg': \"Input should be 'system'\", 
        'input': 'assistant', 
        'ctx': {'expected': \"'system'\"}
      }, {
        'type': 'missing', 
        'loc': ('body', 'messages', 2, 'typed-dict', 'content'), 
        'msg': 'Field required', 
        'input': {'role': 'assistant', 'tool_calls': [{'id': 'call__0_sqrt_cmpl-c359a11f-68fa-4b8e-9960-1917b96a8b23', 'type': 'function', 'function': {'name': 'sqrt', 'arguments': '{\"arg0\": 101}'}}]}
     }, {
       'type': 'literal_error', 
       'loc': ('body', 'messages', 2, 'typed-dict', 'role'), 
       'msg': \"Input should be 'user'\", 
       'input': 'assistant', 
       'ctx': {'expected': \"'user'\"}
     }, {
       'type': 'missing', 
       'loc': ('body', 'messages', 2, 'typed-dict', 'content'), 
       'msg': 'Field required', 
       'input': {'role': 'assistant', 'tool_calls': [{'id': 'call__0_sqrt_cmpl-c359a11f-68fa-4b8e-9960-1917b96a8b23', 'type': 'function', 'function': {'name': 'sqrt', 'arguments': '{\"arg0\": 101}'}}]}
     }, {
       'type': 'missing',
       'loc': ('body', 'messages', 2, 'typed-dict', 'content'), 
       'msg': 'Field required', 
       'input': {'role': 'assistant', 'tool_calls': [{'id': 'call__0_sqrt_cmpl-c359a11f-68fa-4b8e-9960-1917b96a8b23', 'type': 'function', 'function': {'name': 'sqrt', 'arguments': '{\"arg0\": 101}'}}]}
     }, {
       'type': 'literal_error', 
       'loc': ('body', 'messages', 2, 'typed-dict', 'role'),
       'msg': \"Input should be 'tool'\", 
       'input': 'assistant', 
       'ctx': {'expected': \"'tool'\"}
     }, {
       'type': 'missing', 
       'loc': ('body', 'messages', 2, 'typed-dict', 'content'), 
       'msg': 'Field required', 
       'input': {'role': 'assistant', 'tool_calls': [{'id': 'call__0_sqrt_cmpl-c359a11f-68fa-4b8e-9960-1917b96a8b23', 'type': 'function', 'function': {'name': 'sqrt', 'arguments': '{\"arg0\": 101}'}}]}
     }, {
       'type': 'missing', 
       'loc': ('body', 'messages', 2, 'typed-dict', 'tool_call_id'), 
       'msg': 'Field required', 
       'input': {'role': 'assistant', 'tool_calls': [{'id': 'call__0_sqrt_cmpl-c359a11f-68fa-4b8e-9960-1917b96a8b23', 'type': 'function', 'function': {'name': 'sqrt', 'arguments': '{\"arg0\": 101}'}}]}
     }, {
       'type': 'literal_error', 
       'loc': ('body', 'messages', 2, 'typed-dict', 'role'),
       'msg': \"Input should be 'function'\", 'input': 'assistant', 'ctx': {'expected': \"'function'\"}}, {'type': 'missing', 'loc': ('body', 'messages', 2, 'typed-dict', 'content'), 'msg': 'Field required', 'input': {'role': 'assistant', 'tool_calls': [{'id': 'call__0_sqrt_cmpl-c359a11f-68fa-4b8e-9960-1917b96a8b23', 'type': 'function', 'function': {'name': 'sqrt', 'arguments': '{\"arg0\": 101}'}}]}
     }, {
       'type': 'missing', 
       'loc': ('body', 'messages', 2, 'typed-dict', 'name'), 
       'msg': 'Field required', 
       'input': {'role': 'assistant', 'tool_calls': [{'id': 'call__0_sqrt_cmpl-c359a11f-68fa-4b8e-9960-1917b96a8b23', 'type': 'function', 'function': {'name': 'sqrt', 'arguments': '{\"arg0\": 101}'}}]}
    }
    ]","type":"internal_server_error","param":null,"code":null}}

Note that if "content": null is added to the third message (the one with tool_calls), the request succeeds.

The same request on GPT API works.

feloy commented 1 month ago

I propose the fix #1807 , which makes content not required (instead of optional) in assistant message.