Changes in embedding.py, phrase.py, symbol.py and word.py
Added new serialisation
This commit is contained in:
1 parent
6a5a04cd72
commit
78e5e5b878
4 files changed
+27
-18
No files matched your search
@@ -27,13 +27,14 @@ class Embedding:
|
||||
@classmethod
|
||||
def __get_pydantic_core_schema__(cls, source_type: Any, handler):
|
||||
def serialize(value: "Embedding") -> dict:
|
||||
serialized_data = handler(source_type=value.data, value=value.data)
|
||||
|
||||
return {"data": serialized_data}
|
||||
serialized_data = handler(source_type=value.data.__class__, value=value.data)
|
||||
return {
|
||||
'data': serialized_data
|
||||
}
|
||||
|
||||
return core_schema.json_or_python_schema(
|
||||
python_schema=handler(cls),
|
||||
python_schema=core_schema.dataclass_schema(cls),
|
||||
json_schema=core_schema.plain_serializer_function_ser_schema(
|
||||
serialize, when_used="json"
|
||||
serialize, when_used='json'
|
||||
),
|
||||
)
|
||||
@@ -22,16 +22,20 @@ class Phrase(Lexeme):
|
||||
@classmethod
|
||||
def __get_pydantic_core_schema__(cls, source_type: Any, handler):
|
||||
def serialize(value: Phrase) -> dict:
|
||||
serialized_embedding = handler(source_type=value.embedding.__class__, value=value.embedding)
|
||||
|
||||
return {
|
||||
"id": value.id,
|
||||
"content": value.content,
|
||||
"embedding": value.embedding.data.tolist(),
|
||||
"embedding": serialized_embedding, # Use handler for robustness
|
||||
}
|
||||
|
||||
return core_schema.no_info_after_validator_function(
|
||||
lambda x: x,
|
||||
core_schema.is_instance_schema(cls),
|
||||
serialization=core_schema.plain_serializer_function_ser_schema(
|
||||
return core_schema.json_or_python_schema(
|
||||
python_schema=core_schema.no_info_after_validator_function(
|
||||
lambda x: x,
|
||||
core_schema.is_instance_schema(cls)
|
||||
),
|
||||
json_schema=core_schema.plain_serializer_function_ser_schema(
|
||||
serialize, when_used="json"
|
||||
),
|
||||
)
|
||||
@@ -15,15 +15,16 @@ class Symbol:
|
||||
@classmethod
|
||||
def __get_pydantic_core_schema__(cls, source_type: Any, handler):
|
||||
def serialize(value: "Symbol") -> dict:
|
||||
serialized_image = handler(
|
||||
source_type=value.image.__class__, value=value.image
|
||||
)
|
||||
|
||||
return {"id": value.id, "label": value.label, "image": serialized_image}
|
||||
serialized_image = handler(source_type=value.image.__class__, value=value.image)
|
||||
return {
|
||||
'id': value.id,
|
||||
'label': value.label,
|
||||
'image': serialized_image
|
||||
}
|
||||
|
||||
return core_schema.json_or_python_schema(
|
||||
python_schema=handler(cls),
|
||||
python_schema=core_schema.dataclass_schema(cls),
|
||||
json_schema=core_schema.plain_serializer_function_ser_schema(
|
||||
serialize, when_used="json"
|
||||
serialize, when_used='json'
|
||||
),
|
||||
)
|
||||
@@ -36,7 +36,10 @@ class Word(Lexeme):
|
||||
}
|
||||
|
||||
return core_schema.json_or_python_schema(
|
||||
python_schema=core_schema.is_instance_schema(cls),
|
||||
python_schema=core_schema.no_info_after_validator_function(
|
||||
lambda x: x,
|
||||
core_schema.is_instance_schema(cls)
|
||||
),
|
||||
json_schema=core_schema.plain_serializer_function_ser_schema(
|
||||
serialize, when_used='json'
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user