jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'name' when running Functions.ipynb with Jupyter lab
Prerequisites
Please answer the following questions for yourself before submitting an issue.
- [x] I am running the latest code. Development is very rapid so there are no tagged versions as of now.
- [x] I carefully followed the README.md.
- [x] I searched using keywords relevant to my issue to make sure that I am creating a new issue that is not already open (or closed).
- [x] I reviewed the Discussions, and have a new bug or useful enhancement to share.
Expected Behavior
Output the temperature of San Francisco, Tokyo, and Paris
Current Behavior
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'name' INFO: 127.0.0.1:61901 - "POST /v1/chat/completions HTTP/1.1" 500 Internal Server Error
Environment and Context
Apple M3 Max macOS 14.3
Please provide detailed information about your computer setup. This is important in case the issue is not reproducible except for under certain specific conditions.
- Physical (or virtual) hardware you are using, e.g. for Linux:
$ lscpu
- Operating System, e.g. for Linux:
$ uname -a
- SDK version, e.g. for Linux:
$ python3 --version
$ make --version
$ g++ --version
Failure Information (for bugs)
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'name' INFO: 127.0.0.1:61901 - "POST /v1/chat/completions HTTP/1.1" 500 Internal Server Error
Steps to Reproduce
Please provide detailed steps for reproducing the issue. We are not sitting in front of your screen, so the more detail the better.
- python3 -m llama_cpp.server --model /Users/username/models/functionary-7b-v2-GGUF/functionary-7b-v2.q4_0.gguf --chat_format functionary-v2 --hf_pretrained_model_name_or_path /Users/username/models/functionary-7b-v2-GGUF --n_gpu_layers 16
- jupyter lab
- execute the cell of Function Calling with OpenAI Python Client with Jupyter Lab
- etc.
Note: Many issues seem to be regarding functional or performance issues / differences with llama.cpp. In these cases we need to confirm that you're comparing against the version of llama.cpp that was built with your python package, and which parameters you're passing to the context.
Try the following:
git clone https://github.com/abetlen/llama-cpp-pythoncd llama-cpp-pythonrm -rf _skbuild/# delete any old buildspython -m pip install .cd ./vendor/llama.cpp- Follow llama.cpp's instructions to
cmakellama.cpp - Run llama.cpp's
./mainwith the same arguments you previously passed to llama-cpp-python and see if you can reproduce the issue. If you can, log an issue with llama.cpp
Failure Logs
Please include any relevant log snippets or files. If it works under one configuration but not under another, please provide logs for both configurations and their corresponding outputs so it is easy to see where behavior changes.
Also, please try to avoid using screenshots if at all possible. Instead, copy/paste the console output and use Github's markdown to cleanly format your logs for easy readability.
Example environment info:
llama-cpp-python$ git log | head -1
commit 2355ce2227476ea69f1d6142dedd2f4b7f27a86b
llama-cpp-python$ python3 --version
Python 3.11.9
llama-cpp-python$ pip list | egrep "uvicorn|fastapi|sse-starlette|numpy"
fastapi 0.110.2
numpy 1.26.4
sse-starlette 2.1.0
uvicorn 0.29.0
llama-cpp-python/vendor/llama.cpp$ git log | head -3
commit 2355ce2227476ea69f1d6142dedd2f4b7f27a86b
Author: Olivier DEBAUCHE <[email protected]>
Date: Sun Apr 28 05:44:47 2024 +0200
FWIW changing the function calling example code (from https://github.com/abetlen/llama-cpp-python/blob/main/examples/notebooks/Functions.ipynb ) from a role of "tool" to that of "function":
messages.append( { "tool_call_id": tool_call.id, #"role": "tool", "role": "function", "name": function_name, "content": function_response, }
results in the code in llama_cpp/llama_chat_format.py, at line 1869-1872
for message in messages:
# Function call responses
if message["role"] == "function" and "name" in message:
message["name"] = f"functions.{message['name']}"
inserting the missing "name" attribute, and produces a "successful" final output of:
ChatCompletion(id='chatcmpl-b99e3895-ca15-45c7-b926-3bad998c1c90', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The current weather is as follows:\n- San Francisco: 72°F\n- Tokyo: 10°C\n- Paris: 22°C', role='assistant', function_call=None, tool_calls=None))], created=1714589154, model='functionary-7b-v2.1.q4_0.gguf', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=36, prompt_tokens=238, total_tokens=274))
But this is just a workaround/observation, and does not follow the OpenAI official example here: https://platform.openai.com/docs/guides/function-calling --section "Example invoking multiple function calls in one response"
FWIW changing the function calling example code (from https://github.com/abetlen/llama-cpp-python/blob/main/examples/notebooks/Functions.ipynb ) from a role of "tool" to that of "function":
messages.append( { "tool_call_id": tool_call.id, #"role": "tool", "role": "function", "name": function_name, "content": function_response, }
results in the code in llama_cpp/llama_chat_format.py, at line 1869-1872
for message in messages: # Function call responses if message["role"] == "function" and "name" in message: message["name"] = f"functions.{message['name']}"inserting the missing "name" attribute, and produces a "successful" final output of:
ChatCompletion(id='chatcmpl-b99e3895-ca15-45c7-b926-3bad998c1c90', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The current weather is as follows:\n- San Francisco: 72°F\n- Tokyo: 10°C\n- Paris: 22°C', role='assistant', function_call=None, tool_calls=None))], created=1714589154, model='functionary-7b-v2.1.q4_0.gguf', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=36, prompt_tokens=238, total_tokens=274))
But this is just a workaround/observation, and does not follow the OpenAI official example here: https://platform.openai.com/docs/guides/function-calling --section "Example invoking multiple function calls in one response"
Thanks.
I also ran into this using langchain tools and functionary so here's another workaround: I locally cloned https://huggingface.co/meetkai/functionary-small-v2.4-GGUF/tree/main and manually replaced message['name'] with 'tool' in token_config.json. Then pointed the llama-cpp-python server at the local folder with the cli arg --hf_pretrained_model_name_or_path
It seemed to call tools just fine.