|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | + |
| 4 | +import json |
| 5 | + |
| 6 | +import pytest |
| 7 | +from openai_harmony import (Conversation, DeveloperContent, |
| 8 | + HarmonyEncodingName, Message, Role, SystemContent, |
| 9 | + load_harmony_encoding) |
| 10 | + |
| 11 | +from vllm.entrypoints.openai.protocol import FunctionCall, ToolCall |
| 12 | +from vllm.entrypoints.openai.tool_parsers import OpenAIToolParser |
| 13 | +from vllm.transformers_utils.tokenizer import get_tokenizer |
| 14 | + |
| 15 | +MODEL = "gpt2" |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture(scope="module") |
| 19 | +def openai_tokenizer(): |
| 20 | + # The parser does not use the tokenizer, but the constructor requires it. |
| 21 | + return get_tokenizer(MODEL) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def openai_tool_parser(openai_tokenizer): |
| 26 | + return OpenAIToolParser(openai_tokenizer) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture(scope="module") |
| 30 | +def harmony_encoding(): |
| 31 | + return load_harmony_encoding(HarmonyEncodingName.HARMONY_GPT_OSS) |
| 32 | + |
| 33 | + |
| 34 | +def assert_tool_calls( |
| 35 | + actual_tool_calls: list[ToolCall], |
| 36 | + expected_tool_calls: list[ToolCall], |
| 37 | +): |
| 38 | + assert len(actual_tool_calls) == len(expected_tool_calls) |
| 39 | + |
| 40 | + for actual_tool_call, expected_tool_call in zip(actual_tool_calls, |
| 41 | + expected_tool_calls): |
| 42 | + assert isinstance(actual_tool_call.id, str) |
| 43 | + assert len(actual_tool_call.id) > 16 # Default from protocol.py |
| 44 | + assert actual_tool_call.type == "function" |
| 45 | + assert actual_tool_call.function == expected_tool_call.function |
| 46 | + |
| 47 | + |
| 48 | +def test_extract_tool_calls_no_tools(openai_tool_parser, harmony_encoding): |
| 49 | + convo = Conversation.from_messages([ |
| 50 | + Message.from_role_and_content( |
| 51 | + Role.SYSTEM, |
| 52 | + SystemContent.new(), |
| 53 | + ), |
| 54 | + Message.from_role_and_content( |
| 55 | + Role.DEVELOPER, |
| 56 | + DeveloperContent.new().with_instructions("Talk like a pirate!")), |
| 57 | + Message.from_role_and_content(Role.USER, "Arrr, how be you?"), |
| 58 | + Message.from_role_and_content(Role.ASSISTANT, |
| 59 | + "This is a test").with_channel("final") |
| 60 | + ]) |
| 61 | + token_ids = harmony_encoding.render_conversation_for_completion( |
| 62 | + convo, Role.ASSISTANT) |
| 63 | + extracted_info = openai_tool_parser.extract_tool_calls( |
| 64 | + "", |
| 65 | + request=None, |
| 66 | + token_ids=token_ids, |
| 67 | + ) |
| 68 | + assert not extracted_info.tools_called |
| 69 | + assert extracted_info.tool_calls == [] |
| 70 | + assert extracted_info.content == "This is a test" |
| 71 | + |
| 72 | + |
| 73 | +def test_extract_tool_calls_single_tool(openai_tool_parser, harmony_encoding): |
| 74 | + convo = Conversation.from_messages([ |
| 75 | + Message.from_role_and_content(Role.USER, |
| 76 | + "What is the weather in Tokyo?"), |
| 77 | + Message.from_role_and_content( |
| 78 | + Role.ASSISTANT, |
| 79 | + 'User asks: "What is the weather in Tokyo?" We need to use get_current_weather tool.', # noqa: E501 |
| 80 | + ).with_channel("analysis"), |
| 81 | + Message.from_role_and_content( |
| 82 | + Role.ASSISTANT, |
| 83 | + '{"location": "Tokyo"}').with_channel("commentary").with_recipient( |
| 84 | + "functions.get_current_weather").with_content_type("json"), |
| 85 | + ]) |
| 86 | + token_ids = harmony_encoding.render_conversation_for_completion( |
| 87 | + convo, Role.ASSISTANT) |
| 88 | + |
| 89 | + extracted_info = openai_tool_parser.extract_tool_calls( |
| 90 | + "", |
| 91 | + request=None, |
| 92 | + token_ids=token_ids, |
| 93 | + ) |
| 94 | + assert extracted_info.tools_called |
| 95 | + expected_tool_calls = [ |
| 96 | + ToolCall(function=FunctionCall( |
| 97 | + name="get_current_weather", |
| 98 | + arguments=json.dumps({"location": "Tokyo"}), |
| 99 | + )) |
| 100 | + ] |
| 101 | + assert_tool_calls(extracted_info.tool_calls, expected_tool_calls) |
| 102 | + assert extracted_info.content is None |
| 103 | + |
| 104 | + |
| 105 | +def test_extract_tool_calls_multiple_tools( |
| 106 | + openai_tool_parser, |
| 107 | + harmony_encoding, |
| 108 | +): |
| 109 | + convo = Conversation.from_messages([ |
| 110 | + Message.from_role_and_content( |
| 111 | + Role.USER, "What is the weather in Tokyo based on where I'm at?"), |
| 112 | + Message.from_role_and_content( |
| 113 | + Role.ASSISTANT, |
| 114 | + 'User asks: "What is the weather in Tokyo?" based on their location. We need to use get_current_weather tool and get_user_location tool.', # noqa: E501 |
| 115 | + ).with_channel("analysis"), |
| 116 | + Message.from_role_and_content( |
| 117 | + Role.ASSISTANT, |
| 118 | + '{"location": "Tokyo"}').with_channel("commentary").with_recipient( |
| 119 | + "functions.get_current_weather").with_content_type("json"), |
| 120 | + Message.from_role_and_content( |
| 121 | + Role.ASSISTANT, |
| 122 | + '{"location": "Tokyo"}').with_channel("commentary").with_recipient( |
| 123 | + "functions.get_user_location").with_content_type("json"), |
| 124 | + ]) |
| 125 | + token_ids = harmony_encoding.render_conversation_for_completion( |
| 126 | + convo, |
| 127 | + Role.ASSISTANT, |
| 128 | + ) |
| 129 | + |
| 130 | + extracted_info = openai_tool_parser.extract_tool_calls( |
| 131 | + "", |
| 132 | + request=None, |
| 133 | + token_ids=token_ids, |
| 134 | + ) |
| 135 | + assert extracted_info.tools_called |
| 136 | + expected_tool_calls = [ |
| 137 | + ToolCall(function=FunctionCall( |
| 138 | + name="get_current_weather", |
| 139 | + arguments=json.dumps({"location": "Tokyo"}), |
| 140 | + )), |
| 141 | + ToolCall(function=FunctionCall( |
| 142 | + name="get_user_location", |
| 143 | + arguments=json.dumps({"location": "Tokyo"}), |
| 144 | + )) |
| 145 | + ] |
| 146 | + assert_tool_calls(extracted_info.tool_calls, expected_tool_calls) |
| 147 | + assert extracted_info.content is None |
0 commit comments