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: added json schema option for response_format #217

Merged
merged 2 commits into from
Feb 6, 2025
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: 4 additions & 0 deletions aidial_sdk/chat_completion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
MessageContentTextPart,
Request,
ResponseFormat,
ResponseFormatJsonObject,
ResponseFormatJsonSchema,
ResponseFormatJsonSchemaObject,
ResponseFormatText,
Role,
)
from aidial_sdk.chat_completion.request import Stage as RequestStage
Expand Down
32 changes: 30 additions & 2 deletions aidial_sdk/chat_completion/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ConstrainedFloat,
ConstrainedInt,
ConstrainedList,
Field,
PositiveInt,
StrictBool,
StrictInt,
Expand Down Expand Up @@ -170,8 +171,35 @@ class ToolChoice(ExtraForbidModel):
function: FunctionChoice


class ResponseFormat(ExtraForbidModel):
type: Literal["text", "json_object"]
class ResponseFormatText(ExtraForbidModel):
type: Literal["text"]


class ResponseFormatJsonObject(ExtraForbidModel):
type: Literal["json_object"]


class ResponseFormatJsonSchemaObject(ExtraForbidModel):
description: Optional[StrictStr] = None
name: StrictStr
schema_: Dict[str, Any] = Field(..., alias="schema")
strict: Optional[StrictBool] = False

def dict(self, *args, **kwargs):
kwargs["by_alias"] = True
return super().dict(*args, **kwargs)


class ResponseFormatJsonSchema(ExtraForbidModel):
type: Literal["json_schema"]
json_schema: ResponseFormatJsonSchemaObject


ResponseFormat = Union[
ResponseFormatText,
ResponseFormatJsonObject,
ResponseFormatJsonSchema,
]


class AzureChatCompletionRequest(ExtraForbidModel):
Expand Down
53 changes: 52 additions & 1 deletion tests/test_serialization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

from aidial_sdk.chat_completion import Message, Role
from aidial_sdk.chat_completion import Message, ResponseFormatJsonSchema, Role
from aidial_sdk.chat_completion.request import ResponseFormatJsonSchemaObject


def test_message_ser():
Expand All @@ -17,3 +18,53 @@ def test_message_deser():
expected_obj = Message(role=Role.SYSTEM, content="test")

assert actual_obj == expected_obj


def test_response_format_serialization():
format_obj = ResponseFormatJsonSchema(
type="json_schema",
json_schema=ResponseFormatJsonSchemaObject(
description="desc",
name="name",
schema={"key": "value"},
),
)

actual_dict = format_obj.dict()

expected_dict = {
"type": "json_schema",
"json_schema": {
"description": "desc",
"name": "name",
"schema": {"key": "value"},
"strict": False,
},
}

assert actual_dict == expected_dict


def test_response_format_deserialization():
format_dict = {
"type": "json_schema",
"json_schema": {
"description": "desc",
"name": "name",
"schema": {"key": "value"},
},
}

actual_obj = ResponseFormatJsonSchema.parse_obj(format_dict)

expected_obj = ResponseFormatJsonSchema(
type="json_schema",
json_schema=ResponseFormatJsonSchemaObject(
description="desc",
name="name",
schema={"key": "value"},
strict=False,
),
)

assert actual_obj == expected_obj