-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #616 from guardrails-ai/fix-v2-func-call
fix function calling schema for pydantic v2
Showing
7 changed files
with
363 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import pydantic.version | ||
|
||
PYDANTIC_VERSION = pydantic.version.VERSION | ||
|
||
if PYDANTIC_VERSION.startswith("1"): | ||
|
||
def dataclass(cls): # type: ignore | ||
return cls | ||
|
||
else: | ||
from dataclasses import dataclass # type: ignore # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from copy import deepcopy | ||
from typing import List | ||
from warnings import warn | ||
|
||
import pydantic.version | ||
import pytest | ||
from pydantic import BaseModel, Field | ||
|
||
from guardrails.utils.pydantic_utils.v1 import convert_pydantic_model_to_openai_fn | ||
|
||
PYDANTIC_VERSION = pydantic.version.VERSION | ||
|
||
|
||
class Foo(BaseModel): | ||
bar: str = Field(description="some string value") | ||
|
||
|
||
# fmt: off | ||
foo_schema = { | ||
"title": "Foo", | ||
"type": "object", | ||
"properties": { | ||
"bar": { | ||
"title": "Bar", | ||
"description": "some string value", | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"bar" | ||
] | ||
} | ||
# fmt: on | ||
|
||
|
||
# This test is descriptive, not prescriptive. | ||
@pytest.mark.skipif( | ||
not PYDANTIC_VERSION.startswith("1"), | ||
reason="Tests function calling syntax for Pydantic v1", | ||
) | ||
class TestConvertPydanticModelToOpenaiFn: | ||
def test_object_schema(self): | ||
expected_schema = deepcopy(foo_schema) | ||
# When pushed through BareModel it loses the description on any properties. | ||
del expected_schema["properties"]["bar"]["description"] | ||
|
||
# fmt: off | ||
expected_fn_params = { # noqa | ||
"name": "Foo", | ||
"parameters": expected_schema | ||
} | ||
# fmt: on | ||
|
||
actual_fn_params = convert_pydantic_model_to_openai_fn(Foo) | ||
|
||
# assert actual_fn_params == expected_fn_params | ||
warn("Function calling is disabled for pydantic 1.x") | ||
assert actual_fn_params == {} | ||
|
||
def test_list_schema(self): | ||
expected_schema = deepcopy(foo_schema) | ||
# When pushed through BareModel it loses the description on any properties. | ||
del expected_schema["properties"]["bar"]["description"] | ||
|
||
# fmt: off | ||
expected_schema = { | ||
"title": f"Array<{expected_schema.get('title')}>", | ||
"type": "array", | ||
"items": expected_schema | ||
} | ||
# fmt: on | ||
|
||
# fmt: off | ||
expected_fn_params = { # noqa | ||
"name": "Array<Foo>", | ||
"parameters": expected_schema | ||
} | ||
# fmt: on | ||
|
||
actual_fn_params = convert_pydantic_model_to_openai_fn(List[Foo]) | ||
|
||
# assert actual_fn_params == expected_fn_params | ||
warn("Function calling is disabled for pydantic 1.x") | ||
assert actual_fn_params == {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from copy import deepcopy | ||
from typing import List | ||
|
||
import pydantic.version | ||
import pytest | ||
from pydantic import BaseModel, Field | ||
|
||
from guardrails.utils.pydantic_utils.v2 import convert_pydantic_model_to_openai_fn | ||
|
||
PYDANTIC_VERSION = pydantic.version.VERSION | ||
|
||
|
||
class Foo(BaseModel): | ||
bar: str = Field(description="some string value") | ||
|
||
|
||
# fmt: off | ||
foo_schema = { | ||
"title": "Foo", | ||
"type": "object", | ||
"properties": { | ||
"bar": { | ||
"title": "Bar", | ||
"description": "some string value", | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"bar" | ||
] | ||
} | ||
# fmt: on | ||
|
||
|
||
# This test is descriptive, not prescriptive. | ||
@pytest.mark.skipif( | ||
not PYDANTIC_VERSION.startswith("2"), | ||
reason="Tests function calling syntax for Pydantic v2", | ||
) | ||
class TestConvertPydanticModelToOpenaiFn: | ||
def test_object_schema(self): | ||
expected_schema = deepcopy(foo_schema) | ||
|
||
# fmt: off | ||
expected_fn_params = { | ||
"name": "Foo", | ||
"parameters": expected_schema | ||
} | ||
# fmt: on | ||
|
||
actual_fn_params = convert_pydantic_model_to_openai_fn(Foo) | ||
|
||
assert actual_fn_params == expected_fn_params | ||
|
||
def test_list_schema(self): | ||
expected_schema = deepcopy(foo_schema) | ||
|
||
# fmt: off | ||
expected_schema = { | ||
"title": f"Array<{expected_schema.get('title')}>", | ||
"type": "array", | ||
"items": expected_schema | ||
} | ||
# fmt: on | ||
|
||
# fmt: off | ||
expected_fn_params = { | ||
"name": "Array<Foo>", | ||
"parameters": expected_schema | ||
} | ||
# fmt: on | ||
|
||
actual_fn_params = convert_pydantic_model_to_openai_fn(List[Foo]) | ||
|
||
assert actual_fn_params == expected_fn_params |