Changes in embedding.py, lexeme.py, phrase.py, word.py

Defines typed_schema for seralisation
    Moved serialisation method to lexeme
This commit is contained in:
Christoph Eder committed 2025-12-13 18:23:26 +01:00
1 parent 43064b1572
commit d7fe47b71c
4 files changed
+54 -53

No files matched your search

+11 -3
View File
@@ -30,18 +30,26 @@ class Embedding:
@classmethod
def __get_pydantic_core_schema__(cls, source_type: Any, handler):
from pydantic_core import core_schema
def validate_embedding(value):
if isinstance(value, cls):
return value
if isinstance(value, dict):
return cls.from_dict(value)
raise ValueError(f"Cannot convert {type(value)} to Embedding")
return core_schema.with_info_plain_validator_function(
python_schema = core_schema.with_info_plain_validator_function(
lambda value, _: validate_embedding(value),
serialization=core_schema.plain_serializer_function_ser_schema(
lambda x: {"data": x.data.tolist()},
when_used="json",
),
)
return core_schema.json_or_python_schema(
json_schema=core_schema.typed_dict_schema({
'data': core_schema.typed_dict_field(core_schema.list_schema())
}),
python_schema=python_schema,
)
+42 -3
View File
@@ -19,6 +19,45 @@ class Lexeme(ABC):
def embedding(self) -> Embedding:
"""Embedding(s) associated with the lexeme."""
pass
@classmethod
@abstractmethod
def from_dict(cls, data: dict) -> "Lexeme":
"""Create instance from dictionary."""
pass
@classmethod
def __get_pydantic_core_schema__(cls, source_type: Any, handler):
from pydantic_core import core_schema
def validate_lexeme(value):
if isinstance(value, cls):
return value
if isinstance(value, dict):
return cls.from_dict(value)
raise ValueError(f"Cannot convert {type(value)} to {cls.__name__}")
python_schema = core_schema.with_info_plain_validator_function(
lambda value, _: validate_lexeme(value),
serialization=core_schema.plain_serializer_function_ser_schema(
lambda x: {
"id": x.id,
"content": x.content,
"embedding": {"data": x.embedding.data.tolist()},
},
when_used="json",
),
)
return core_schema.json_or_python_schema(
json_schema=core_schema.typed_dict_schema(
{
"id": core_schema.typed_dict_field(core_schema.int_schema()),
"content": core_schema.typed_dict_field(core_schema.str_schema()),
"embedding": core_schema.typed_dict_field(
handler.generate_schema(Embedding)
),
}
),
python_schema=python_schema,
)
+1 -24
View File
@@ -25,27 +25,4 @@ class Phrase(Lexeme):
id=data["id"],
content=data["content"],
embedding=Embedding.from_dict(data["embedding"]),
)
@classmethod
def __get_pydantic_core_schema__(cls, source_type: Any, handler):
from pydantic_core import core_schema
def validate_phrase(value):
if isinstance(value, cls):
return value
if isinstance(value, dict):
return cls.from_dict(value)
raise ValueError(f"Cannot convert {type(value)} to Phrase")
return core_schema.with_info_plain_validator_function(
lambda value, _: validate_phrase(value),
serialization=core_schema.plain_serializer_function_ser_schema(
lambda x: {
"id": x.id,
"content": x.content,
"embedding": {"data": x.embedding.data.tolist()},
},
when_used="json",
),
)
)
-23
View File
@@ -31,26 +31,3 @@ class Word(Lexeme):
content=data["content"],
embedding=Embedding.from_dict(data["embedding"]),
)
@classmethod
def __get_pydantic_core_schema__(cls, source_type: Any, handler):
from pydantic_core import core_schema
def validate_word(value):
if isinstance(value, cls):
return value
if isinstance(value, dict):
return cls.from_dict(value)
raise ValueError(f"Cannot convert {type(value)} to Word")
return core_schema.with_info_plain_validator_function(
lambda value, _: validate_word(value),
serialization=core_schema.plain_serializer_function_ser_schema(
lambda x: {
"id": x.id,
"content": x.content,
"embedding": {"data": x.embedding.data.tolist()},
},
when_used="json",
),
)