Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Llama3.2 tool calling OpenAI API not working #9991

Closed
1 task done
SinanAkkoyun opened this issue Nov 4, 2024 · 19 comments · Fixed by #9859
Closed
1 task done

[Bug]: Llama3.2 tool calling OpenAI API not working #9991

SinanAkkoyun opened this issue Nov 4, 2024 · 19 comments · Fixed by #9859
Labels
bug Something isn't working

Comments

@SinanAkkoyun
Copy link

SinanAkkoyun commented Nov 4, 2024

Your current environment

The output of `python collect_env.py`
Collecting environment information...                                                                                                                                                                                                                                                           
WARNING 11-04 11:59:16 cuda.py:81] Detected different devices in the system:                                                                                                                                                                                                                    
WARNING 11-04 11:59:16 cuda.py:81] NVIDIA A100 80GB PCIe                                                                                                                                                                                                                                        
WARNING 11-04 11:59:16 cuda.py:81] NVIDIA GeForce RTX 4090                                                                                                                                                                                                                                      
WARNING 11-04 11:59:16 cuda.py:81] NVIDIA GeForce RTX 4090                                                                                                                                                                                                                                      
WARNING 11-04 11:59:16 cuda.py:81] NVIDIA GeForce RTX 4090                                                                                                                                                                                                                                      
WARNING 11-04 11:59:16 cuda.py:81] Please make sure to set `CUDA_DEVICE_ORDER=PCI_BUS_ID` to avoid unexpected behavior.                                                                                                                                                                         
PyTorch version: 2.5.0+cu124                                                                                                                                                                                                                                                                    
Is debug build: False                                                                                                                                                                                                                                                                           
CUDA used to build PyTorch: 12.4                                                                                                                                                                                                                                                                
ROCM used to build PyTorch: N/A                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                
OS: Ubuntu 22.04.4 LTS (x86_64)                                                                                                                                                                                                                                                                 
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0                                                                                                                                                                                                                                              
Clang version: Could not collect                                                                                                                                                                                                                                                                
CMake version: version 3.30.0                                                                                                                                                                                                                                                                   
Libc version: glibc-2.35                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                
Python version: 3.10.13 (main, Sep 11 2023, 13:44:35) [GCC 11.2.0] (64-bit runtime)                                                                                                                                                                                                             
Python platform: Linux-5.15.0-122-generic-x86_64-with-glibc2.35                                                                                                                                                                                                                                 
Is CUDA available: True                                                                                                                                                                                                                                                                         
CUDA runtime version: Could not collect                                                                                                                                                                                                                                                         
CUDA_MODULE_LOADING set to: LAZY                                                                                                                                                                                                                                                                
GPU models and configuration:                                                                                                                                                                                                                                                                   
GPU 0: NVIDIA A100 80GB PCIe                                                                                                                                                                                                                                                                    
GPU 1: NVIDIA GeForce RTX 4090                                                                                                                                                                                                                                                                  
GPU 2: NVIDIA GeForce RTX 4090                                                                                                                                                                                                                                                                  
GPU 3: NVIDIA GeForce RTX 4090                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                
Nvidia driver version: 535.183.01                                                                                                                                                                                                                                                               
cuDNN version: Could not collect                                                                                                                                                                                                                                                                
HIP runtime version: N/A                                                                                                                                                                                                                                                                        
MIOpen runtime version: N/A                                                                                                                                                                                                                                                                     
Is XNNPACK available: True                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                
CPU:                                                                                                                                                                                                                                                                                            
Architecture:                         x86_64                                                                                                                                                                                                                                                    
CPU op-mode(s):                       32-bit, 64-bit                                                                                                                                                                                                                                            
Address sizes:                        46 bits physical, 48 bits virtual                                                                                                                                                                                                                         
Byte Order:                           Little Endian                                                                                                                                                                                                                                             
CPU(s):                               32                                                                                                                                                                                                                                                        
On-line CPU(s) list:                  0-31                                                                                                                                                                                                                                                      
Vendor ID:                            GenuineIntel                                                                                                                                                                                                                                              
Model name:                           13th Gen Intel(R) Core(TM) i9-13900KS                                                                                                                                                                                                                     
CPU family:                           6                                                                                                                                                                                                                                                         
Model:                                183                                                                                                                                                                                                                                                       
Thread(s) per core:                   2                                                                                                                                                                                                                                                         
Core(s) per socket:                   24                                                                                                                                                                                                                                                        
Socket(s):                            1                                                                                                                                                                                                                                                         
Stepping:                             1                                                                                                                                                                                                                                                         
CPU max MHz:                          6000.0000                                                                                                                                                                                                                                                 
CPU min MHz:                          800.0000                                                                                                                                                                                                                                                  
BogoMIPS:                             6374.40                                                                                                                                                                                                                                                   
Flags:                                fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known
_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority
 ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req umip pku ospke waitpkg gfni vaes vpclm
ulqdq tme rdpid movdiri movdir64b fsrm md_clear serialize pconfig arch_lbr flush_l1d arch_capabilities                                                                                                                                                                                          
Virtualization:                       VT-x
L1d cache:                            896 KiB (24 instances)
L1i cache:                            1.3 MiB (24 instances)
L2 cache:                             32 MiB (12 instances)
L3 cache:                             36 MiB (1 instance)
NUMA node(s):                         1
NUMA node0 CPU(s):                    0-31
Vulnerability Gather data sampling:   Not affected
Vulnerability Itlb multihit:          Not affected
Vulnerability Mds:                    Not affected
Vulnerability Meltdown:               Not affected
Vulnerability Mmio stale data:        Not affected
Vulnerability Reg file data sampling: Mitigation; Clear Register File
Vulnerability Retbleed:               Not affected
Vulnerability Spec rstack overflow:   Not affected
Vulnerability Spec store bypass:      Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:             Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:             Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
Vulnerability Srbds:                  Not affected
Vulnerability Tsx async abort:        Not affected

Versions of relevant libraries:
[pip3] numpy==1.26.2
[pip3] nvidia-cublas-cu12==12.4.5.8
[pip3] nvidia-cuda-cupti-cu12==12.4.127
[pip3] nvidia-cuda-nvrtc-cu12==12.4.127
[pip3] nvidia-cuda-runtime-cu12==12.4.127
[pip3] nvidia-cudnn-cu12==9.1.0.70
[pip3] nvidia-cufft-cu12==11.2.1.3
[pip3] nvidia-curand-cu12==10.3.5.147
[pip3] nvidia-cusolver-cu12==11.6.1.9
[pip3] nvidia-cusparse-cu12==12.3.1.170
[pip3] nvidia-ml-py==12.560.30
[pip3] nvidia-nccl-cu12==2.21.5
[pip3] nvidia-nvjitlink-cu12==12.4.127
[pip3] nvidia-nvtx-cu12==12.4.127
[pip3] pyzmq==26.0.3
[pip3] torch==2.5.0
[pip3] torchvision==0.20.0
[pip3] transformers==4.46.0
[pip3] triton==3.1.0
[conda] numpy                     1.26.4                   pypi_0    pypi
[conda] nvidia-cublas-cu12        12.4.5.8                 pypi_0    pypi
[conda] nvidia-cuda-cupti-cu12    12.4.127                 pypi_0    pypi
[conda] nvidia-cuda-nvrtc-cu12    12.4.127                 pypi_0    pypi
[conda] nvidia-cuda-runtime-cu12  12.4.127                 pypi_0    pypi
[conda] nvidia-cudnn-cu12         9.1.0.70                 pypi_0    pypi
[conda] nvidia-cufft-cu12         11.2.1.3                 pypi_0    pypi
[conda] nvidia-curand-cu12        10.3.5.147               pypi_0    pypi
[conda] nvidia-cusolver-cu12      11.6.1.9                 pypi_0    pypi
[conda] nvidia-cusparse-cu12      12.3.1.170               pypi_0    pypi
[conda] nvidia-ml-py              12.560.30                pypi_0    pypi
[conda] nvidia-nccl-cu12          2.21.5                   pypi_0    pypi
[conda] nvidia-nvjitlink-cu12     12.4.127                 pypi_0    pypi
[conda] nvidia-nvtx-cu12          12.4.127                 pypi_0    pypi
[conda] pyzmq                     26.0.3                   pypi_0    pypi
[conda] torch                     2.5.0                    pypi_0    pypi
[conda] torchvision               0.20.0                   pypi_0    pypi
[conda] transformers              4.46.0                   pypi_0    pypi
[conda] triton                    3.1.0                    pypi_0    pypi
ROCM Version: Could not collect
Neuron SDK Version: N/A
vLLM Version: 0.6.3.post2.dev139+g622b7ab9
vLLM Build Flags:
CUDA Archs: Not Set; ROCm: Disabled; Neuron: Disabled
GPU Topology:
GPU0    GPU1    GPU2    GPU3    CPU Affinity    NUMA Affinity   GPU NUMA ID
GPU0     X      PHB     PHB     PHB     0-31    0               N/A
GPU1    PHB      X      PHB     PHB     0-31    0               N/A
GPU2    PHB     PHB      X      PHB     0-31    0               N/A
GPU3    PHB     PHB     PHB      X      0-31    0               N/A

Legend:

  X    = Self
  SYS  = Connection traversing PCIe as well as the SMP interconnect between NUMA nodes (e.g., QPI/UPI)
  NODE = Connection traversing PCIe as well as the interconnect between PCIe Host Bridges within a NUMA node
  PHB  = Connection traversing PCIe as well as a PCIe Host Bridge (typically the CPU)
  PXB  = Connection traversing multiple PCIe bridges (without traversing the PCIe Host Bridge)
  PIX  = Connection traversing at most a single PCIe bridge
  NV#  = Connection traversing a bonded set of # NVLinks

Model Input Dumps

No response

🐛 Describe the bug

When trying to run Llama3.2 tool calling via python -m vllm.entrypoints.openai.api_server --model meta-llama/Llama-3.2-1B-Instruct --enable-auto-tool-choice --tool-call-parser llama3_json I do not get the OpenAI API function calling functionality but rather just get the tool call string:

❯ current time?
<|python_tag|>{"type": "function", "function": "get_time", "parameters": {"timezone":
"America/New_York"}}

Using the official OpenAI API with 4o and Ollama works with my code

Before submitting a new issue...

  • Make sure you already searched for relevant issues, and asked the chatbot living at the bottom right corner of the documentation page, which can answer lots of frequently asked questions.
@SinanAkkoyun SinanAkkoyun added the bug Something isn't working label Nov 4, 2024
@SinanAkkoyun
Copy link
Author

ERROR 11-04 12:09:18 llama_tool_parser.py:116] Error in extracting tool call from response.             
ERROR 11-04 12:09:18 llama_tool_parser.py:116] Traceback (most recent call last):                  
ERROR 11-04 12:09:18 llama_tool_parser.py:116]   File "/home/ai/.mconda3/envs/vllm/lib/python3.10/site-packages/vllm/entrypoints/openai/tool_parsers/llama_tool_parser.py", lin
e 97, in extract_tool_calls                                                                                  
ERROR 11-04 12:09:18 llama_tool_parser.py:116]     tool_calls: List[ToolCall] = [    
ERROR 11-04 12:09:18 llama_tool_parser.py:116]   File "/home/ai/.mconda3/envs/vllm/lib/python3.10/site-packages/vllm/entrypoints/openai/tool_parsers/llama_tool_parser.py", lin
e 101, in <listcomp>                                                                  
ERROR 11-04 12:09:18 llama_tool_parser.py:116]     name=raw_function_call["name"],
ERROR 11-04 12:09:18 llama_tool_parser.py:116] KeyError: 'name'

@DarkLight1337
Copy link
Member

Can you show your code? cc @K-Mistele

@SinanAkkoyun
Copy link
Author

Can you show your code?

I am writing a NodeJS TS client, but this is what the default OpenAI JS library gets from my end:

tools.push({
          type: 'function',
          function: {
            name: message.function.name,
            parameters: adaptToFunctionParameters(message.function.parameters)
          }
        } as ChatCompletionTool)

// ...

const chatCompletion = await this.client.chat.completions.create({
          messages,
          tools: (tools && tools.length > 0) ? tools : undefined,
          model: this.model,
        })

which works with the official OpenAI API

@K-Mistele
Copy link
Contributor

It looks like the Llama 3 tool parser applies to Llama 3.1, but not Llama 3.2. based on some of Meta's code and docs, they state this:

For Llama3.2 1B and 3B instruct models, we are introducing a new format for zero shot function calling. This new format is designed to be more flexible and powerful than the previous format. All available functions can be provided in the system message. A key difference is in the format of how the assistant responds with function calls. It is pythonic in the form of [func1(params_name=params_value, params_name2=params_value2...), func2(params)] instead of the json or tag that were defined in Llama3.1.

It seems like we may need a separate tool parser for 3.2 1B and 3B models? I was not aware that the format had been changed. we should probably clarify this in the docs as well, that the parser is for llama 3.1 and the larger 3.2 models, not llama 3.2 1B and 3B.

cc @maxdebayser, happy to investigate & work with you on this if interested

@maxdebayser
Copy link
Contributor

I'm definitely interested in looking into this. Maybe we need to add different tests then, because our existing ones are passing for 3.2

@K-Mistele
Copy link
Contributor

I'm definitely interested in looking into this. Maybe we need to add different tests then, because our existing ones are passing for 3.2

which 3.2 model are your tests running against? The docs I linked to make it seem like 1B and 3B have a different format compared to the larger 3.2 models

@K-Mistele
Copy link
Contributor

#9859 has a parser for this

@SinanAkkoyun
Copy link
Author

SinanAkkoyun commented Nov 7, 2024

Another question: When testing 3.1 8B, it ALWAYS wants to call a tool, even when I just tell it 'Hi', can someone reproduce that behaviour?

To be specific: When I give it a tool to execute bash commands, it always responds with an echo "Hi" even when I tell it to not use any tools. If I take that tool away, it tries to get the current time etc.
It seems as if the model is being forced to call a tool...

What other model could I try for comparison?

@SinanAkkoyun
Copy link
Author

Not even mistral works for me:
python -m vllm.entrypoints.openai.api_server --model mistralai/Mistral-7B-Instruct-v0.3 --enable-auto-tool-choice --tool-call-parser mistral --config_format mistral --load_format mistral
It just doesn't call tools, but is aware of them

@K-Mistele
Copy link
Contributor

Another question: When testing 3.1 8B, it ALWAYS wants to call a tool, even when I just tell it 'Hi', can someone reproduce that behaviour?

To be specific: When I give it a tool to execute bash commands, it always responds with an echo "Hi" even when I tell it to not use any tools. If I take that tool away, it tries to get the current time etc. It seems as if the model is being forced to call a tool...

What other model could I try for comparison?

Yeah, this is a known issue with Llama 3.1 8B. Basically, meta's chat template's system prompt implicitly instructs the model to call a tool always. The model is designed for one-off tool calls where it receives a prompt, and generates a tool call. If you try passing a tool result back for it to interpret or use, it'll usually just try to call another tool. This isn't a tool parser or vLLM issue as much as a poorly designed chat template / system prompt.

My recommendation every time that this comes up is to use a better chat template that alters their default system prompt. This is the chat template I always use for Llama 3.1 8B, and while it doesn't get perfect results (because the above behavior is how the model was trained as best as I can tell), it does improve behavior significantly at the cost of extra tokens.

Compare meta's prompt & chat template with the one I use:
https://gist.github.com/K-Mistele/820d142b4dab50bd8ef0c7bbcad4515c

@K-Mistele
Copy link
Contributor

K-Mistele commented Nov 8, 2024

Not even mistral works for me: python -m vllm.entrypoints.openai.api_server --model mistralai/Mistral-7B-Instruct-v0.3 --enable-auto-tool-choice --tool-call-parser mistral --config_format mistral --load_format mistral It just doesn't call tools, but is aware of them

I think I recall that a recent change with the mistral tokenizer may have affected mistral tool calling, but I can't for the life of me remember what the issue number is.

Fwiw, this model (mistralai/Mistral-7B-Instruct-v0.3) was implemented since it was one of the few small tool-calling models that was available at the time, but I have found that it doesn't work unless you set temperature=0 (Mistral does this in all their docs & examples too).

Definitely should be fixed if there's a problem, but if you're just trying to test it out there are much better small tool calling models out there.

I checked on the config that is being used for running vLLM with mistral for tools for testing in CI, and this is the config:

"mistral": {
        "model":
        "mistralai/Mistral-7B-Instruct-v0.3",
        "arguments": [
            "--tool-call-parser", "mistral", "--chat-template",
            str(VLLM_PATH / "examples/tool_chat_template_mistral.jinja"),
            "--ignore-patterns=\"consolidated.safetensors\""
        ],
        "system_prompt":
        "You are a helpful assistant with access to tools. If a tool"
        " that you have would be helpful to answer a user query, "
        "call the tool. Otherwise, answer the user's query directly "
        "without calling a tool. DO NOT CALL A TOOL THAT IS IRRELEVANT "
        "to the user's question - just respond to it normally."
    },

Can you try

vllm serve mistralai/Mistral-7B-Instruct-v0.3 \
--enable-auto-tool-choice \
--tool-call-parser mistral \
--chat-template examples/tool_chat_template_mistral.jinja \
--ignore-patterns="consolidated.safetensors"

It seems like the difference is that it's not using --config_format mistral --load_format mistral (the PR that added Mistral predates those options). Can you try that and see if it works?

I will try to find the issue about tool calling and the mistral tokenizer, and we can either move this part of the conversation there or open a new issue

@K-Mistele
Copy link
Contributor

Re: the llama 3.2 tool issue, as I mentioned, there is an open PR in #9859 that should resolve this, but there is also a test that uses llama 3.2 in CI that does pass, here is the config:

vllm serve meta-llama/Llama-3.2-3B-Instruct \
--enable-auto-tool-choice \
--tool-call-parser llama3_json \
--chat-template examples/tool_chat_template_llama3.2_json.jinja

@SinanAkkoyun
Copy link
Author

@K-Mistele Thank you so much for all the info and advice!!
I will test out everything you provided.

Could you please tell me the best working small tool calling models?

@SinanAkkoyun
Copy link
Author

@K-Mistele The llama config didn't work, it was able to chat and see the tools but not able to call any. Same thing with the mistral config strangely.

@SinanAkkoyun
Copy link
Author

The thing is that with ollama and llama3.2 the function calling at least works (despite it always calling functions, it is able to call multi-turn functions etc)

❯ Hello. How are you?
assistant 
  execute_shell_command(command: echo "I am functioning within optimal parameters") [Io4QnH/FTLGkFWon7xGVBQ/0]

> execute_shell_command = {} [Io4QnH/FTLGkFWon7xGVBQ/0]

Acknowledged.
❯ Cool. Get the time in berlin
assistant 
  get_time(timezone: Europe/Berlin) [Io4QnH/FTLGkFWon7xGVBQ/1]

> get_time = "2024-11-08T10:25:15.689+01:00" [Io4QnH/FTLGkFWon7xGVBQ/1]

Current local time in Berlin is 10:25:16, November 8, 2024 (CET).
Parameters: Time zone offset = +01:00, UTC offset = +02:00.

@K-Mistele
Copy link
Contributor

@K-Mistele Thank you so much for all the info and advice!! I will test out everything you provided.

Could you please tell me the best working small tool calling models?

I'm not incredibly familiar with < 7B models for tool use since in my experience they still aren't really sufficiently reliable unless you're using guided generation

@K-Mistele
Copy link
Contributor

The thing is that with ollama and llama3.2 the function calling at least works (despite it always calling functions, it is able to call multi-turn functions etc)

❯ Hello. How are you?
assistant 
  execute_shell_command(command: echo "I am functioning within optimal parameters") [Io4QnH/FTLGkFWon7xGVBQ/0]

> execute_shell_command = {} [Io4QnH/FTLGkFWon7xGVBQ/0]

Acknowledged.
❯ Cool. Get the time in berlin
assistant 
  get_time(timezone: Europe/Berlin) [Io4QnH/FTLGkFWon7xGVBQ/1]

> get_time = "2024-11-08T10:25:15.689+01:00" [Io4QnH/FTLGkFWon7xGVBQ/1]

Current local time in Berlin is 10:25:16, November 8, 2024 (CET).
Parameters: Time zone offset = +01:00, UTC offset = +02:00.

I'm not familiar with ollama's implementation but it's possible they're using guided decoding or a grammar or something.

@K-Mistele
Copy link
Contributor

@K-Mistele Thank you so much for all the info and advice!! I will test out everything you provided.
Could you please tell me the best working small tool calling models?

I'm not incredibly familiar with < 7B models for tool use since in my experience they still aren't really sufficiently reliable unless you're using guided generation

You might check out xLAM though - https://huggingface.co/Salesforce/xLAM-1b-fc-r

@SinanAkkoyun
Copy link
Author

You might check out xLAM though

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants