Changes in embedding.py, phrase.py, word.py
Adopted serialisation schema to last commit
This commit is contained in:
1 parent
0a07ea6d39
commit
43064b1572
3 files changed
+40
-39
No files matched your search
@@ -26,16 +26,22 @@ class Embedding:
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict) -> "Embedding":
|
||||
return cls(data=np.array(data["data"], dtype=np.float32))
|
||||
|
||||
|
||||
@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(
|
||||
lambda value, _: cls.from_dict(value) if isinstance(value, dict) else value,
|
||||
lambda value, _: validate_embedding(value),
|
||||
serialization=core_schema.plain_serializer_function_ser_schema(
|
||||
lambda x: {"data": x.data.tolist()},
|
||||
when_used="json",
|
||||
),
|
||||
)
|
||||
|
||||
@@ -14,39 +14,38 @@ class Phrase(Lexeme):
|
||||
@property
|
||||
def content(self) -> str:
|
||||
return self._content
|
||||
|
||||
|
||||
@property
|
||||
def embedding(self) -> Embedding:
|
||||
return self._embedding
|
||||
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict) -> 'Phrase':
|
||||
def from_dict(cls, data: dict) -> "Phrase":
|
||||
return cls(
|
||||
id=data['id'],
|
||||
content=data['content'],
|
||||
embedding=Embedding.from_dict(data['embedding'])
|
||||
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
|
||||
|
||||
return core_schema.no_info_after_validator_function(
|
||||
lambda x: x if isinstance(x, cls) else cls(**x),
|
||||
core_schema.typed_dict_schema(
|
||||
{
|
||||
'id': core_schema.typed_dict_field(handler.generate_schema(int)),
|
||||
'content': core_schema.typed_dict_field(handler.generate_schema(str)),
|
||||
'embedding': core_schema.typed_dict_field(handler.generate_schema(Embedding)),
|
||||
}
|
||||
),
|
||||
|
||||
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': x.embedding
|
||||
"id": x.id,
|
||||
"content": x.content,
|
||||
"embedding": {"data": x.embedding.data.tolist()},
|
||||
},
|
||||
when_used='json',
|
||||
when_used="json",
|
||||
),
|
||||
)
|
||||
|
||||
@@ -36,24 +36,20 @@ class Word(Lexeme):
|
||||
def __get_pydantic_core_schema__(cls, source_type: Any, handler):
|
||||
from pydantic_core import core_schema
|
||||
|
||||
return core_schema.no_info_after_validator_function(
|
||||
lambda x: x if isinstance(x, cls) else cls(**x),
|
||||
core_schema.typed_dict_schema(
|
||||
{
|
||||
"id": core_schema.typed_dict_field(handler.generate_schema(int)),
|
||||
"content": core_schema.typed_dict_field(
|
||||
handler.generate_schema(str)
|
||||
),
|
||||
"embedding": core_schema.typed_dict_field(
|
||||
handler.generate_schema(Embedding)
|
||||
),
|
||||
}
|
||||
),
|
||||
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": x.embedding, # Let Pydantic serialize Embedding
|
||||
"embedding": {"data": x.embedding.data.tolist()},
|
||||
},
|
||||
when_used="json",
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user