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

Python: fix discrimator field for CMC #8417

Merged
merged 1 commit into from
Aug 29, 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
2 changes: 1 addition & 1 deletion python/.vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"pytest",
"tests/unit/",
"--last-failed",
"-v"
"-vv"
],
"group": "test",
"presentation": {
Expand Down
17 changes: 5 additions & 12 deletions python/semantic_kernel/contents/chat_message_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
from enum import Enum
from html import unescape
from typing import Any, ClassVar, Literal, Union, overload
from typing import Annotated, Any, ClassVar, Literal, overload
from xml.etree.ElementTree import Element # nosec

from defusedxml import ElementTree
Expand All @@ -26,7 +26,6 @@
from semantic_kernel.contents.function_result_content import FunctionResultContent
from semantic_kernel.contents.image_content import ImageContent
from semantic_kernel.contents.kernel_content import KernelContent
from semantic_kernel.contents.streaming_text_content import StreamingTextContent
from semantic_kernel.contents.text_content import TextContent
from semantic_kernel.contents.utils.author_role import AuthorRole
from semantic_kernel.contents.utils.finish_reason import FinishReason
Expand All @@ -41,15 +40,9 @@
IMAGE_CONTENT_TAG: ImageContent,
}

ITEM_TYPES = Union[
AnnotationContent,
ImageContent,
TextContent,
StreamingTextContent,
FunctionResultContent,
FunctionCallContent,
FileReferenceContent,
]
ITEM_TYPES = (
AnnotationContent | ImageContent | TextContent | FunctionResultContent | FunctionCallContent | FileReferenceContent
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -78,7 +71,7 @@ class ChatMessageContent(KernelContent):
tag: ClassVar[str] = CHAT_MESSAGE_CONTENT_TAG
role: AuthorRole
name: str | None = None
items: list[ITEM_TYPES] = Field(default_factory=list, discriminator=DISCRIMINATOR_FIELD)
items: list[Annotated[ITEM_TYPES, Field(..., discriminator=DISCRIMINATOR_FIELD)]] = Field(default_factory=list)
encoding: str | None = None
finish_reason: FinishReason | None = None

Expand Down
92 changes: 91 additions & 1 deletion python/tests/unit/contents/test_chat_message_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,98 @@ def test_cmc_to_dict_keys():
"content": [{"type": "text", "text": "Hello, "}, {"type": "text", "text": "world!"}],
},
),
(
{
"role": "user",
"items": [
{"content_type": "text", "text": "Hello, "},
{"content_type": "text", "text": "world!"},
],
},
{
"role": "user",
"content": [{"type": "text", "text": "Hello, "}, {"type": "text", "text": "world!"}],
},
),
(
{
"role": "user",
"items": [
{"content_type": "annotation", "file_id": "test"},
],
},
{
"role": "user",
"content": [{"type": "text", "text": "test None (Start Index=None->End Index=None)"}],
},
),
(
{
"role": "user",
"items": [
{"content_type": "file_reference", "file_id": "test"},
],
},
{
"role": "user",
"content": [{"file_id": "test"}],
},
),
(
{
"role": "user",
"items": [
{"content_type": "function_call", "name": "test-test"},
],
},
{
"role": "user",
"content": [{"id": None, "type": "function", "function": {"name": "test-test", "arguments": None}}],
},
),
(
{
"role": "user",
"items": [
{"content_type": "function_call", "name": "test-test"},
{"content_type": "function_result", "name": "test-test", "result": "test", "id": "test"},
],
},
{
"role": "user",
"content": [
{"id": None, "type": "function", "function": {"name": "test-test", "arguments": None}},
{"tool_call_id": "test", "content": "test"},
],
},
),
(
{
"role": "user",
"items": [
{"content_type": "image", "uri": "http://test"},
],
},
{
"role": "user",
"content": [{"image_url": {"url": "http://test/"}, "type": "image_url"}],
},
),
],
ids=[
"user_content",
"user_with_name",
"user_item",
"function_call",
"function_result",
"multiple_items",
"multiple_items_serialize",
"annotations_serialize",
"file_reference_serialize",
"function_call_serialize",
"function_result_serialize",
"image_serialize",
],
ids=["user_content", "user_with_name", "user_item", "function_call", "function_result", "multiple_items"],
)
def test_cmc_to_dict_items(input_args, expected_dict):
message = ChatMessageContent(**input_args)
Expand Down
Loading