-
Notifications
You must be signed in to change notification settings - Fork 11
Improve trajectory storage and fix schema handling #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import datetime | ||
| import json | ||
| import logging | ||
| import uuid | ||
|
|
||
|
|
@@ -15,6 +16,22 @@ | |
| logging.basicConfig(level=logging.INFO) | ||
| logger = logging.getLogger("katas-db.milvus") | ||
|
|
||
|
|
||
| def serialize_content(content) -> str: | ||
| """Serialize content to string for Milvus storage.""" | ||
| if isinstance(content, str): | ||
| return content | ||
| return json.dumps(content) | ||
|
|
||
|
|
||
| def deserialize_content(content: str): | ||
| """Deserialize content from Milvus storage.""" | ||
| try: | ||
| return json.loads(content) | ||
| except (json.JSONDecodeError, TypeError): | ||
| return content | ||
|
|
||
|
|
||
| class MilvusKataBackend(BaseKataBackend): | ||
| milvus = MilvusClient(**milvus_client_settings.model_dump()) | ||
| embedding_model = SentenceTransformer(milvus_other_settings.embedding_model) | ||
|
|
@@ -93,27 +110,29 @@ def update_entities( | |
| if enable_conflict_resolution: | ||
| old_entities = [] | ||
| for entity in entities: | ||
| old_entities.extend(self.search_entities(namespace_id=namespace_id, query=entity.content)) | ||
| query_str = serialize_content(entity.content) | ||
| old_entities.extend(self.search_entities(namespace_id=namespace_id, query=query_str)) | ||
|
|
||
| updates = resolve_conflicts(old_entities, entities_with_temporary_ids) | ||
| for update in updates: | ||
| content_str = serialize_content(update.content) | ||
| match update.event: | ||
| case 'ADD': | ||
| entity_id = str(self.milvus.insert(collection_name=namespace_id, data={ | ||
| 'type': entity_type, | ||
| 'content': update.content, | ||
| 'content': content_str, | ||
| 'created_at': int(now.timestamp()), | ||
| 'embedding': self.embedding_model.encode(update.content), | ||
| 'embedding': self.embedding_model.encode(content_str), | ||
| 'metadata': update.metadata, | ||
| })['ids'][0]) | ||
| update.id = entity_id | ||
| case 'UPDATE': | ||
| self.milvus.upsert(collection_name=namespace_id, data={ | ||
| 'type': entity_type, | ||
| 'id': update.id, | ||
| 'content': update.content, | ||
| 'id': int(update.id), | ||
| 'content': content_str, | ||
| 'created_at': int(now.timestamp()), | ||
| 'embedding': self.embedding_model.encode(update.content), | ||
| 'embedding': self.embedding_model.encode(content_str), | ||
| 'metadata': update.metadata | ||
| }, partial_update=True) | ||
|
Comment on lines
130
to
137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential ValueError if The Suggested defensive handling case 'UPDATE':
+ try:
+ entity_id = int(update.id)
+ except ValueError:
+ logger.error(f"Invalid entity ID for UPDATE: {update.id}")
+ continue
self.milvus.upsert(collection_name=namespace_id, data={
'type': entity_type,
- 'id': int(update.id),
+ 'id': entity_id,
'content': content_str,🤖 Prompt for AI Agents |
||
| case 'DELETE': | ||
|
|
@@ -123,11 +142,12 @@ def update_entities( | |
| else: | ||
| updates = [] | ||
| for entity in entities: | ||
| content_str = serialize_content(entity.content) | ||
| entity_id = str(self.milvus.insert(collection_name=namespace_id, data={ | ||
| 'type': entity_type, | ||
| 'content': entity.content, | ||
| 'content': content_str, | ||
| 'created_at': int(now.timestamp()), | ||
| 'embedding': self.embedding_model.encode(entity.content), | ||
| 'embedding': self.embedding_model.encode(content_str), | ||
| 'metadata': entity.metadata | ||
| })['ids'][0]) | ||
| updates.append(EntityUpdate( | ||
|
|
@@ -175,7 +195,7 @@ def delete_entity_by_id(self, namespace_id: str, entity_id: str): | |
| # Keep it as an INT64 or else you won't be able to list all entities. | ||
| FieldSchema(name='id', is_primary=True, auto_id=True, dtype=DataType.INT64, max_length=128), | ||
| FieldSchema(name='type', dtype=DataType.VARCHAR, max_length=128), | ||
| FieldSchema(name='content', dtype=DataType.VARCHAR, max_length=512), | ||
| FieldSchema(name='content', dtype=DataType.VARCHAR, max_length=65535), | ||
| FieldSchema(name='created_at', dtype=DataType.INT64), | ||
| FieldSchema(name='embedding', dtype=DataType.FLOAT_VECTOR, dim=384), | ||
| FieldSchema(name='metadata', dtype=DataType.JSON), | ||
|
|
@@ -185,5 +205,6 @@ def parse_milvus_entity(entity: dict) -> RecordedEntity: | |
| return RecordedEntity.model_validate({ | ||
| **entity, | ||
| 'id': str(entity['id']), | ||
| 'content': deserialize_content(entity['content']), | ||
| 'created_at': datetime.datetime.fromtimestamp(entity['created_at'], datetime.UTC), | ||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.