This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6e1ec1
commit b76fbca
Showing
11 changed files
with
60 additions
and
65 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
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
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
Empty file.
39 changes: 3 additions & 36 deletions
39
packages/openassistants/openassistants/data_models/function_input.py
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 |
---|---|---|
@@ -1,47 +1,14 @@ | ||
from typing import Any, Dict | ||
|
||
import jsonschema | ||
from pydantic import BaseModel | ||
|
||
|
||
class BaseJSONSchema(BaseModel): | ||
""" | ||
Validates a json_schema. top level must of the schema must be type object | ||
""" | ||
|
||
json_schema: Dict[str, Any] | ||
|
||
def schema_validator(cls, values): | ||
jsonschema.validate( | ||
values["json_schema"], jsonschema.Draft202012Validator.META_SCHEMA | ||
) | ||
jsonschema.validate( | ||
values["json_schema"], | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"type": {"type": "string", "enum": ["object"]}, | ||
"properties": {"type": "object"}, | ||
"required": {"type": "array", "items": {"type": "string"}}, | ||
}, | ||
"required": [ | ||
"type", | ||
], | ||
}, | ||
) | ||
return values | ||
|
||
def validate_args(self, args: dict): | ||
try: | ||
jsonschema.validate(args, self.json_schema) | ||
except jsonschema.exceptions.ValidationError as e: | ||
raise ValueError(f"invalid function arguments\n{e}") | ||
from openassistants.data_models.json_schema import JSONSchema | ||
|
||
|
||
class FunctionCall(BaseModel): | ||
name: str | ||
arguments: Dict[str, Any] | ||
|
||
|
||
class FunctionInputRequest(FunctionCall, BaseJSONSchema): | ||
pass | ||
class FunctionInputRequest(FunctionCall): | ||
json_schema: JSONSchema |
40 changes: 40 additions & 0 deletions
40
packages/openassistants/openassistants/data_models/json_schema.py
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,40 @@ | ||
from typing import Annotated, Any, Dict | ||
|
||
import jsonschema | ||
from pydantic import AfterValidator, TypeAdapter | ||
|
||
|
||
def _json_schema_meta_validator(value: Any): | ||
try: | ||
jsonschema.validate(value, jsonschema.Draft202012Validator.META_SCHEMA) | ||
except jsonschema.exceptions.ValidationError as e: | ||
raise ValueError(f"Invalid JSONSchema:\n{str(e)}\n") from e | ||
if value.get("type") != "object": | ||
raise ValueError( | ||
f"JSONSchema must have type='object'. Got '{value.get('type')}'" | ||
) | ||
return value | ||
|
||
|
||
JSONSchema = Annotated[Dict[str, Any], AfterValidator(_json_schema_meta_validator)] | ||
""" | ||
A JSONSchema is a dict that conforms to the JSONSchema specification. | ||
In order to validate an arbitrary dict | ||
``` | ||
from pydantic import TypeAdapter | ||
json_schema = TypeAdapter(JSONSchema).validate_python(some_dict) | ||
``` | ||
When used in a pydantic model as a field, the JSONSchema will be validated automatically. | ||
""" # noqa: E501 | ||
|
||
|
||
EMPTY_JSON_SCHEMA = TypeAdapter(JSONSchema).validate_python( | ||
{ | ||
"type": "object", | ||
"properties": {}, | ||
"required": [], | ||
} | ||
) |
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