|
14 | 14 |
|
15 | 15 | import jinja2 |
16 | 16 | from fastapi import Request |
17 | | -from openai.types.chat import ( |
18 | | - ChatCompletionAssistantMessageParam, |
19 | | - ChatCompletionMessageToolCallParam, |
20 | | - ChatCompletionToolMessageParam, |
21 | | -) |
22 | | -from openai.types.chat.chat_completion_message_tool_call_param import ( |
23 | | - Function as FunctionCallTool, |
24 | | -) |
25 | 17 | from openai.types.responses import ( |
26 | 18 | ResponseCodeInterpreterCallCodeDeltaEvent, |
27 | 19 | ResponseCodeInterpreterCallCodeDoneEvent, |
|
49 | 41 | ResponseWebSearchCallCompletedEvent, |
50 | 42 | ResponseWebSearchCallInProgressEvent, |
51 | 43 | ResponseWebSearchCallSearchingEvent, |
52 | | - ToolChoiceFunction, |
53 | 44 | response_function_web_search, |
54 | 45 | response_text_delta_event, |
55 | 46 | ) |
|
59 | 50 | ) |
60 | 51 | from openai.types.responses.tool import Tool |
61 | 52 | from openai_harmony import Message as OpenAIHarmonyMessage |
62 | | -from pydantic import TypeAdapter |
63 | 53 |
|
64 | 54 | from vllm import envs |
65 | 55 | from vllm.engine.protocol import EngineClient |
|
89 | 79 | from vllm.entrypoints.openai.protocol import ( |
90 | 80 | DeltaMessage, |
91 | 81 | ErrorResponse, |
92 | | - FunctionCall, |
93 | | - FunctionDefinition, |
94 | 82 | InputTokensDetails, |
95 | 83 | OutputTokensDetails, |
96 | 84 | RequestResponseMetadata, |
97 | 85 | ResponseCompletedEvent, |
98 | 86 | ResponseCreatedEvent, |
99 | 87 | ResponseInProgressEvent, |
100 | | - ResponseInputOutputItem, |
101 | 88 | ResponseReasoningPartAddedEvent, |
102 | 89 | ResponseReasoningPartDoneEvent, |
103 | 90 | ResponsesRequest, |
|
107 | 94 | ) |
108 | 95 | from vllm.entrypoints.openai.serving_engine import OpenAIServing |
109 | 96 | from vllm.entrypoints.openai.serving_models import OpenAIServingModels |
| 97 | +from vllm.entrypoints.responses_utils import construct_chat_message_with_tool_call |
110 | 98 | from vllm.entrypoints.tool_server import ToolServer |
111 | 99 | from vllm.inputs.data import TokensPrompt as EngineTokensPrompt |
112 | 100 | from vllm.logger import init_logger |
@@ -876,95 +864,6 @@ def _make_response_output_items( |
876 | 864 | outputs.extend(tool_call_items) |
877 | 865 | return outputs |
878 | 866 |
|
879 | | - def _parse_tool_calls_from_content( |
880 | | - self, |
881 | | - request: ResponsesRequest, |
882 | | - tokenizer: AnyTokenizer, |
883 | | - content: str | None = None, |
884 | | - ) -> tuple[list[FunctionCall] | None, str | None]: |
885 | | - function_calls = list[FunctionCall]() |
886 | | - |
887 | | - if not self.enable_auto_tools or not self.tool_parser: |
888 | | - # Tools are not enabled |
889 | | - return None, content |
890 | | - elif request.tool_choice is None: |
891 | | - # No tool calls. |
892 | | - return None, content |
893 | | - elif request.tool_choice and isinstance( |
894 | | - request.tool_choice, ToolChoiceFunction |
895 | | - ): |
896 | | - # Forced Function Call |
897 | | - function_calls.append( |
898 | | - FunctionCall(name=request.tool_choice.name, arguments=content) |
899 | | - ) |
900 | | - content = None # Clear content since tool is called. |
901 | | - elif request.tool_choice == "required": |
902 | | - assert content is not None |
903 | | - tool_calls = TypeAdapter(list[FunctionDefinition]).validate_json(content) |
904 | | - function_calls.extend( |
905 | | - [ |
906 | | - FunctionCall( |
907 | | - name=tool_call.name, |
908 | | - arguments=json.dumps(tool_call.parameters, ensure_ascii=False), |
909 | | - ) |
910 | | - for tool_call in tool_calls |
911 | | - ] |
912 | | - ) |
913 | | - content = None # Clear content since tool is called. |
914 | | - elif request.tool_choice == "auto" or request.tool_choice == "none": |
915 | | - try: |
916 | | - tool_parser = self.tool_parser(tokenizer) |
917 | | - except RuntimeError as e: |
918 | | - logger.exception("Error in tool parser creation.") |
919 | | - raise e |
920 | | - tool_call_info = tool_parser.extract_tool_calls( |
921 | | - content if content is not None else "", |
922 | | - request=request, # type: ignore |
923 | | - ) |
924 | | - if tool_call_info is not None and tool_call_info.tools_called: |
925 | | - # extract_tool_calls() returns a list of tool calls. |
926 | | - function_calls.extend( |
927 | | - FunctionCall( |
928 | | - name=tool_call.function.name, |
929 | | - arguments=tool_call.function.arguments, |
930 | | - ) |
931 | | - for tool_call in tool_call_info.tool_calls |
932 | | - ) |
933 | | - content = tool_call_info.content |
934 | | - else: |
935 | | - # No tool calls. |
936 | | - return None, content |
937 | | - else: |
938 | | - raise ValueError(f"Invalid tool_choice: {request.tool_choice}") |
939 | | - return function_calls, content |
940 | | - |
941 | | - def _construct_chat_message_with_tool_call( |
942 | | - self, item: ResponseInputOutputItem |
943 | | - ) -> ChatCompletionMessageParam: |
944 | | - if isinstance(item, ResponseFunctionToolCall): |
945 | | - # Append the function call as a tool call. |
946 | | - return ChatCompletionAssistantMessageParam( |
947 | | - role="assistant", |
948 | | - tool_calls=[ |
949 | | - ChatCompletionMessageToolCallParam( |
950 | | - id=item.call_id, |
951 | | - function=FunctionCallTool( |
952 | | - name=item.name, |
953 | | - arguments=item.arguments, |
954 | | - ), |
955 | | - type="function", |
956 | | - ) |
957 | | - ], |
958 | | - ) |
959 | | - elif item.get("type") == "function_call_output": |
960 | | - # Append the function call output as a tool message. |
961 | | - return ChatCompletionToolMessageParam( |
962 | | - role="tool", |
963 | | - content=item.get("output"), |
964 | | - tool_call_id=item.get("call_id"), |
965 | | - ) |
966 | | - return item # type: ignore |
967 | | - |
968 | 867 | def _make_response_output_items_with_harmony( |
969 | 868 | self, |
970 | 869 | context: HarmonyContext, |
@@ -1017,7 +916,7 @@ def _construct_input_messages( |
1017 | 916 | messages.append({"role": "user", "content": request.input}) |
1018 | 917 | else: |
1019 | 918 | for item in request.input: |
1020 | | - messages.append(self._construct_chat_message_with_tool_call(item)) |
| 919 | + messages.append(construct_chat_message_with_tool_call(item)) |
1021 | 920 | return messages |
1022 | 921 |
|
1023 | 922 | def _construct_harmony_system_input_message( |
|
0 commit comments