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

fix: openai chat message dump logic #7849

Merged
merged 6 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions python/openai/openai_frontend/engine/triton_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ async def chat(
self._validate_chat_request(request, metadata)

conversation = [
{"role": str(message.role), "content": str(message.content)}
for message in request.messages
message.model_dump(exclude_none=True) for message in request.messages
]
add_generation_prompt = True

Expand Down
16 changes: 16 additions & 0 deletions python/openai/openai_frontend/schemas/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ class Detail(Enum):


class ImageUrl(BaseModel):
model_config = ConfigDict(use_enum_values=True)

url: AnyUrl = Field(
..., description="Either a URL of the image or the base64 encoded image data."
)
Expand All @@ -203,6 +205,8 @@ class ImageUrl(BaseModel):


class ChatCompletionRequestMessageContentPartImage(BaseModel):
model_config = ConfigDict(use_enum_values=True)

type: Type = Field(..., description="The type of the content part.")
image_url: ImageUrl

Expand All @@ -212,6 +216,8 @@ class Type1(Enum):


class ChatCompletionRequestMessageContentPartText(BaseModel):
model_config = ConfigDict(use_enum_values=True)

type: Type1 = Field(..., description="The type of the content part.")
text: str = Field(..., description="The text content.")

Expand All @@ -224,6 +230,8 @@ def __str__(self):


class ChatCompletionRequestSystemMessage(BaseModel):
model_config = ConfigDict(use_enum_values=True)

content: str = Field(..., description="The contents of the system message.")
role: Role = Field(
..., description="The role of the messages author, in this case `system`."
Expand Down Expand Up @@ -264,6 +272,8 @@ def __str__(self):


class ChatCompletionRequestToolMessage(BaseModel):
model_config = ConfigDict(use_enum_values=True)

role: Role3 = Field(
..., description="The role of the messages author, in this case `tool`."
)
Expand All @@ -281,6 +291,8 @@ def __str__(self):


class ChatCompletionRequestFunctionMessage(BaseModel):
model_config = ConfigDict(use_enum_values=True)

role: Role4 = Field(
..., description="The role of the messages author, in this case `function`."
)
Expand Down Expand Up @@ -656,6 +668,8 @@ class ChatCompletionRequestMessageContentPart(RootModel):


class ChatCompletionRequestUserMessage(BaseModel):
model_config = ConfigDict(use_enum_values=True)

content: Union[str, List[ChatCompletionRequestMessageContentPart]] = Field(
..., description="The contents of the user message.\n"
)
Expand Down Expand Up @@ -768,6 +782,8 @@ class CreateChatCompletionFunctionResponse(BaseModel):


class ChatCompletionRequestAssistantMessage(BaseModel):
model_config = ConfigDict(use_enum_values=True)

content: Optional[str] = Field(
None,
description="The contents of the assistant message. Required unless `tool_calls` or `function_call` is specified.\n",
Expand Down
31 changes: 31 additions & 0 deletions python/openai/tests/test_chat_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,37 @@ def test_chat_completions_system_prompt_only(self, client, model: str):
assert message["content"].strip()
assert message["role"] == "assistant"

def test_chat_completions_user_prompt_str(self, client, model: str):
# No system prompt provided
messages = [{"role": "user", "content": "What is machine learning?"}]

response = client.post(
"/v1/chat/completions", json={"model": model, "messages": messages}
)

assert response.status_code == 200
message = response.json()["choices"][0]["message"]
assert message["content"].strip()
assert message["role"] == "assistant"

def test_chat_completions_user_prompt_dict(self, client, model: str):
# No system prompt provided
messages = [
{
"role": "user",
"content": [{"type": "text", "text": "What is machine learning?"}],
}
]

response = client.post(
"/v1/chat/completions", json={"model": model, "messages": messages}
)

assert response.status_code == 200
message = response.json()["choices"][0]["message"]
assert message["content"].strip()
assert message["role"] == "assistant"

@pytest.mark.parametrize(
"param_key, param_value",
[
Expand Down
Loading