-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Add extraction of default and descriptions for fields in Pydantic models #44
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
Open
Mahhheshh (Mahhheshh)
wants to merge
3
commits into
braintrustdata:main
Choose a base branch
from
Mahhheshh:fix/31
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+204
−13
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,158 @@ | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
| from braintrust.parameters import _extract_pydantic_fields, parameters_to_json_schema | ||
|
|
||
| DEFAULT_VALUE = "You are a helpful assistant." | ||
| DEFAULT_DESCRIPTION = "System prompt for the model" | ||
| SCHEMA_TITLE = "SystemPrompt" | ||
| PARAM_NAME = "system_prompt" | ||
|
|
||
|
|
||
| def test_extract_single_field_with_default_and_description(): | ||
| schema = { | ||
| "value": {"type": "string", "default": DEFAULT_VALUE, "description": DEFAULT_DESCRIPTION}, | ||
| } | ||
| defaults, descriptions = _extract_pydantic_fields(schema) | ||
| assert defaults == DEFAULT_VALUE | ||
| assert descriptions == DEFAULT_DESCRIPTION | ||
|
|
||
|
|
||
| def test_extract_single_field_missing_default_and_description(): | ||
| schema = { | ||
| "value": {"type": "string"}, | ||
| } | ||
| defaults, descriptions = _extract_pydantic_fields(schema) | ||
| assert defaults is None | ||
| assert descriptions is None | ||
|
|
||
|
|
||
| def test_extract_multi_field(): | ||
| schema = { | ||
| "temperature": {"type": "number", "default": 0.7, "description": "Sampling temperature"}, | ||
| "max_tokens": {"type": "integer", "default": 1024, "description": "Maximum tokens to generate"}, | ||
| } | ||
| defaults, descriptions = _extract_pydantic_fields(schema) | ||
| assert defaults == {"temperature": 0.7, "max_tokens": 1024} | ||
| assert descriptions == {"temperature": "Sampling temperature", "max_tokens": "Maximum tokens to generate"} | ||
|
|
||
|
|
||
| def test_extract_multi_field_partial_metadata(): | ||
| schema = { | ||
| "temperature": {"type": "number", "default": 0.7}, | ||
| "max_tokens": {"type": "integer", "description": "Maximum tokens to generate"}, | ||
| } | ||
| defaults, descriptions = _extract_pydantic_fields(schema) | ||
| assert defaults == {"temperature": 0.7, "max_tokens": None} | ||
| assert descriptions == {"temperature": None, "max_tokens": "Maximum tokens to generate"} | ||
|
|
||
|
|
||
| def test_extract_empty_schema(): | ||
| defaults, descriptions = _extract_pydantic_fields({}) | ||
| assert defaults == {} | ||
| assert descriptions == {} | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def v2_model(): | ||
| def _make(default=DEFAULT_VALUE, description=DEFAULT_DESCRIPTION): | ||
| model = MagicMock() | ||
| model.model_json_schema.return_value = { | ||
| "title": SCHEMA_TITLE, | ||
| "type": "object", | ||
| "properties": {"value": {"type": "string", "default": default, "description": description}}, | ||
| } | ||
| del model.get | ||
| return model | ||
|
|
||
| return _make | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def v1_model(): | ||
| def _make(default=DEFAULT_VALUE): | ||
| model = MagicMock() | ||
| del model.model_json_schema | ||
| model.schema.return_value = { | ||
| "title": SCHEMA_TITLE, | ||
| "type": "object", | ||
| "properties": {"value": {"type": "string", "default": default}}, | ||
| } | ||
| del model.get | ||
| return model | ||
|
|
||
| return _make | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def v2_multi_field_model(): | ||
| def _make(): | ||
| model = MagicMock() | ||
| model.model_json_schema.return_value = { | ||
| "title": "ModelConfig", | ||
| "type": "object", | ||
| "properties": { | ||
| "temperature": {"type": "number", "default": 0.7, "description": "Sampling temperature"}, | ||
| "max_tokens": {"type": "integer", "default": 1024, "description": "Maximum tokens to generate"}, | ||
| }, | ||
| } | ||
| del model.get | ||
| return model | ||
|
|
||
| return _make | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def v1_multi_field_model(): | ||
| def _make(): | ||
| model = MagicMock() | ||
| del model.model_json_schema | ||
| model.schema.return_value = { | ||
| "title": "ModelConfig", | ||
| "type": "object", | ||
| "properties": { | ||
| "temperature": {"type": "number", "default": 0.7}, | ||
| "max_tokens": {"type": "integer", "default": 1024}, | ||
| }, | ||
| } | ||
| del model.get | ||
| return model | ||
|
|
||
| return _make | ||
|
|
||
|
|
||
| def test_pydantic_v2_model(v2_model): | ||
| schema = parameters_to_json_schema({PARAM_NAME: v2_model()}) | ||
|
|
||
| assert schema[PARAM_NAME]["type"] == "data" | ||
| assert schema[PARAM_NAME]["default"] == DEFAULT_VALUE | ||
| assert schema[PARAM_NAME]["description"] == DEFAULT_DESCRIPTION | ||
|
|
||
|
|
||
| def test_pydantic_v1_model(v1_model): | ||
| schema = parameters_to_json_schema({PARAM_NAME: v1_model()}) | ||
|
|
||
| assert schema[PARAM_NAME]["type"] == "data" | ||
| assert schema[PARAM_NAME]["default"] == DEFAULT_VALUE | ||
| assert schema[PARAM_NAME]["description"] is None | ||
|
|
||
|
|
||
| def test_pydantic_v2_multi_field_model(v2_multi_field_model): | ||
| schema = parameters_to_json_schema({PARAM_NAME: v2_multi_field_model()}) | ||
|
|
||
| assert schema[PARAM_NAME]["type"] == "data" | ||
| assert schema[PARAM_NAME]["schema"]["title"] == "ModelConfig" | ||
| assert schema[PARAM_NAME]["default"] == {"temperature": 0.7, "max_tokens": 1024} | ||
| assert schema[PARAM_NAME]["description"] == { | ||
| "temperature": "Sampling temperature", | ||
| "max_tokens": "Maximum tokens to generate", | ||
| } | ||
|
|
||
|
|
||
| def test_pydantic_v1_multi_field_model(v1_multi_field_model): | ||
| schema = parameters_to_json_schema({PARAM_NAME: v1_multi_field_model()}) | ||
|
|
||
| assert schema[PARAM_NAME]["type"] == "data" | ||
| assert schema[PARAM_NAME]["schema"]["title"] == "ModelConfig" | ||
| assert schema[PARAM_NAME]["default"] == {"temperature": 0.7, "max_tokens": 1024} | ||
| assert schema[PARAM_NAME]["description"] == {"temperature": None, "max_tokens": None} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.