Skip to content

Commit

Permalink
do not change protocol digest when setting descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
zmezei committed Jul 5, 2023
1 parent 9147527 commit 59ea5ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/uagents/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import hashlib
from typing import Type, Union, Dict
from typing import Type, Union, Dict, ClassVar, Any

from pydantic import BaseModel
from pydantic.schema import model_schema, default_ref_template


class Model(BaseModel):
_schema_without_descrs: ClassVar[Union[Dict[str, Any], None]] = None

@staticmethod
def _remove_descriptions(
model: Type["Model"], orig_descriptions: Dict[str, Union[str, Dict]]
Expand Down Expand Up @@ -39,18 +41,21 @@ def _refresh_schema_cache(model: Type["Model"]):

@staticmethod
def build_schema_digest(model: Union["Model", Type["Model"]]) -> str:
orig_descriptions: Dict[str, Union[str, Dict]] = {}
type_obj = model if isinstance(model, type) else model.__class__
Model._remove_descriptions(type_obj, orig_descriptions)
if type_obj._schema_without_descrs is None:
orig_descriptions: Dict[str, Union[str, Dict]] = {}
Model._remove_descriptions(type_obj, orig_descriptions)
digest = (
hashlib.sha256(
model.schema_json(indent=None, sort_keys=True).encode("utf8")
)
.digest()
.hex()
)
Model._restore_descriptions(type_obj, orig_descriptions)
Model._refresh_schema_cache(type_obj)
if type_obj._schema_without_descrs is None:
type_obj._schema_without_descrs = type_obj.schema()
Model._restore_descriptions(type_obj, orig_descriptions)
Model._refresh_schema_cache(type_obj)
return f"model:{digest}"


Expand Down
8 changes: 7 additions & 1 deletion src/uagents/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def manifest(self) -> Dict[str, Any]:

for schema_digest, model in all_models.items():
manifest["models"].append(
{"digest": schema_digest, "schema": model.schema()}
{"digest": schema_digest, "schema": model._schema_without_descrs}
)

for request, responses in self._replies.items():
Expand All @@ -199,6 +199,12 @@ def manifest(self) -> Dict[str, Any]:
encoded = json.dumps(manifest, indent=None, sort_keys=True).encode("utf8")
metadata["digest"] = f"proto:{hashlib.sha256(encoded).digest().hex()}"

manifest["models"] = []
for schema_digest, model in all_models.items():
manifest["models"].append(
{"digest": schema_digest, "schema": model.schema()}
)

final_manifest: Dict[str, Any] = copy.deepcopy(manifest)
final_manifest["metadata"] = metadata

Expand Down

0 comments on commit 59ea5ac

Please sign in to comment.