-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add json-fix tools and AI tool call objects
- Loading branch information
Showing
6 changed files
with
118 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from pydantic import BaseModel, RootModel | ||
|
||
|
||
class MixinFromJson: | ||
|
||
@classmethod | ||
def from_json(cls, json_str): | ||
""" | ||
Error-tolerant deserialization | ||
""" | ||
from fmtr.tools import json_fix | ||
data = json_fix.from_json(json_str, default={}) | ||
|
||
if type(data) is dict: | ||
self = cls(**data) | ||
else: | ||
self = cls(data) | ||
|
||
return self | ||
|
||
|
||
class Base(BaseModel, MixinFromJson): | ||
""" | ||
Base model | ||
""" | ||
... | ||
|
||
|
||
class Root(RootModel, MixinFromJson): | ||
""" | ||
Root (list) model | ||
""" | ||
... |
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,20 @@ | ||
import json | ||
import json_repair | ||
|
||
from fmtr.tools.logging_tools import logger | ||
from fmtr.tools.tools import Raise | ||
|
||
|
||
def from_json(json_string, default=None): | ||
""" | ||
Error-tolerant JSON deserialization | ||
""" | ||
try: | ||
return json_repair.loads(json_string) | ||
except json.JSONDecodeError as exception: | ||
if default is Raise: | ||
raise exception | ||
logger.warning(f'Deserialization failed {repr(exception)}: {json_string}') | ||
return default |
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 +1 @@ | ||
0.9.1 | ||
0.9.2 |
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