Skip to content

Commit

Permalink
Adding serialize function to fit set-related cache bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
parkervg committed Oct 18, 2024
1 parent e2072c2 commit 880c2b1
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion blendsql/models/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .._constants import IngredientKwarg
from ..db.utils import truncate_df_content
from ..db._database import Database
from ..ingredients.few_shot import Example

CONTEXT_TRUNCATION_LIMIT = 100
ModelObj = TypeVar("ModelObj")
Expand All @@ -38,6 +39,21 @@ def run(self):
self.init_fn()


def serialize(v: Any) -> str:
if isinstance(v, set):
return sorted(v)
elif isinstance(v, Example):
return v.to_string(context_formatter=lambda df: df.to_markdown(index=False))
elif isinstance(v, list) and isinstance(v[0], Example):
return "\n".join(
[
_v.to_string(context_formatter=lambda df: df.to_markdown(index=False))
for _v in v
]
)
return str(v)


@attrs
class Model:
"""Parent class for all BlendSQL Models."""
Expand Down Expand Up @@ -151,7 +167,7 @@ def _create_key(self, program: Type[Program], **kwargs) -> str:
options_str = str(
sorted(
[
(k, str(sorted(v) if isinstance(v, set) else v))
(k, serialize(v))
for k, v in kwargs.items()
if not callable(v)
and not isinstance(v, Database)
Expand Down

0 comments on commit 880c2b1

Please sign in to comment.