Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions tests/tool_parsers/test_functiongemma_tool_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest

from tests.tool_parsers.utils import run_tool_extraction
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
from vllm.tool_parsers.functiongemma_tool_parser import FunctionGemmaToolParser

Expand Down Expand Up @@ -92,6 +93,78 @@ def test_multiple_tool_calls(self, parser, mock_request):
assert result.tool_calls[0].function.name == "get_weather"
assert result.tool_calls[1].function.name == "get_time"

def test_text_after_tool_call(self, parser, mock_request):
model_output = (
"<start_function_call>call:get_weather{location:<escape>Paris<escape>}"
"<end_function_call>"
"Let me check that for you."
)
result = parser.extract_tool_calls(model_output, mock_request)

assert result.tools_called is True
assert len(result.tool_calls) == 1
assert result.tool_calls[0].function.name == "get_weather"
assert result.content == "Let me check that for you."

def test_text_around_multiple_tool_calls(self, parser, mock_request):
model_output = (
"Sure. "
"<start_function_call>call:get_weather{location:<escape>London<escape>}"
"<end_function_call>"
" Now the time. "
"<start_function_call>call:get_time{timezone:<escape>UTC<escape>}"
"<end_function_call>"
" Done."
)
result = parser.extract_tool_calls(model_output, mock_request)

assert result.tools_called is True
assert [tc.function.name for tc in result.tool_calls] == [
"get_weather",
"get_time",
]
assert result.content == "Sure. Now the time. Done."


class TestStreamingParity:
"""Non-streaming must return the same content the streaming path emits."""

@pytest.mark.parametrize(
"model_output",
[
"<start_function_call>call:get_weather{location:<escape>Paris<escape>}"
"<end_function_call>Let me check that for you.",
"Sure. "
"<start_function_call>call:get_weather{location:<escape>London<escape>}"
"<end_function_call> Now the time. "
"<start_function_call>call:get_time{timezone:<escape>UTC<escape>}"
"<end_function_call> Done.",
],
)
def test_content_matches_streaming(self, default_tokenizer, model_output):
request = ChatCompletionRequest(messages=[], model="test-model")

content, tool_calls = run_tool_extraction(
FunctionGemmaToolParser(default_tokenizer),
model_output,
request,
streaming=False,
)
streamed_content, streamed_tool_calls = run_tool_extraction(
FunctionGemmaToolParser(default_tokenizer),
model_output,
request,
streaming=True,
)

assert [tc.function.name for tc in tool_calls] == [
tc.function.name for tc in streamed_tool_calls
]
assert [tc.function.arguments for tc in tool_calls] == [
tc.function.arguments for tc in streamed_tool_calls
]
assert content == streamed_content.strip()


class TestParseArguments:
def test_empty_arguments(self, parser):
Expand Down
20 changes: 13 additions & 7 deletions vllm/tool_parsers/functiongemma_tool_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,25 @@ def extract_tool_calls(
)

try:
matches = self.tool_call_regex.findall(model_output)
matches = list(self.tool_call_regex.finditer(model_output))

if not matches:
return ExtractedToolCallInformation(
tools_called=False, tool_calls=[], content=model_output
)

tool_calls: list[ToolCall] = []
# Text outside the function call blocks (before, between and
# after them) is content, the same as the streaming path emits.
content_parts: list[str] = []
last_end = 0

for match in matches:
func_name = match[0] if match[0] else match[2]
args_str = match[1] if match[1] else match[3]
content_parts.append(model_output[last_end : match.start()])
last_end = match.end()

func_name = match.group(1) or match.group(3)
args_str = match.group(2) or match.group(4) or ""

if not func_name:
continue
Expand All @@ -122,11 +129,10 @@ def extract_tool_calls(
)
)

content_parts.append(model_output[last_end:])

if tool_calls:
content_end = model_output.find(self.tool_call_start_token)
content = (
model_output[:content_end].strip() if content_end > 0 else None
)
content = "".join(content_parts).strip()

return ExtractedToolCallInformation(
tools_called=True,
Expand Down
Loading