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

feat: inherit all Enums from str to make JSON serialization possible #132

Merged
merged 1 commit into from
Jul 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
4 changes: 2 additions & 2 deletions aidial_sdk/chat_completion/enums.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from enum import Enum


class FinishReason(Enum):
class FinishReason(str, Enum):
STOP = "stop"
LENGTH = "length"
FUNCTION_CALL = "function_call"
TOOL_CALLS = "tool_calls"
CONTENT_FILTER = "content_filter"


class Status(Enum):
class Status(str, Enum):
COMPLETED = "completed"
FAILED = "failed"
2 changes: 1 addition & 1 deletion aidial_sdk/chat_completion/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ToolCall(ExtraForbidModel):
function: FunctionCall


class Role(Enum):
class Role(str, Enum):
SYSTEM = "system"
USER = "user"
ASSISTANT = "assistant"
Expand Down
19 changes: 19 additions & 0 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import json

from aidial_sdk.chat_completion import Message, Role


def test_message_ser():
msg_obj = Message(role=Role.SYSTEM, content="test")
actual_dict = msg_obj.dict(exclude_none=True)
expected_dict = {"role": "system", "content": "test"}

assert json.loads(json.dumps(actual_dict)) == expected_dict


def test_message_deser():
msg_dict = {"role": "system", "content": "test"}
actual_obj = Message.parse_raw(json.dumps(msg_dict))
expected_obj = Message(role=Role.SYSTEM, content="test")

assert actual_obj == expected_obj