Skip to content
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

Add missing awaits in azure chat store #16645

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,12 @@ async def aset_messages(self, key: str, messages: List[ChatMessage]) -> None:
self.chat_table_name
)
entities = chat_client.query_entities(f"PartitionKey eq '{key}'")
all_entities = []
async for entity in entities:
all_entities.append(entity)

delete_operations = (
(TransactionOperation.DELETE, entity) for entity in entities
(TransactionOperation.DELETE, entity) for entity in all_entities
)
create_operations = (
(
Expand Down Expand Up @@ -233,7 +237,7 @@ async def async_add_message(self, key: str, message: ChatMessage, idx: int = Non
metadata_client = await self._atable_service_client.create_table_if_not_exists(
self.metadata_table_name
)
metadata = self._get_or_default_metadata(metadata_client, key)
metadata = await self._get_or_default_metadata(metadata_client, key)
next_index = int(metadata["MessageCount"])

if idx is not None and idx > next_index:
Expand All @@ -245,7 +249,7 @@ async def async_add_message(self, key: str, message: ChatMessage, idx: int = Non
chat_client = await self._atable_service_client.create_table_if_not_exists(
self.chat_table_name
)
chat_client.create_entity(
await chat_client.create_entity(
serialize(
self.service_mode,
{
Expand All @@ -271,8 +275,12 @@ async def adelete_messages(self, key: str) -> Optional[List[ChatMessage]]:
self.chat_table_name
)
entities = chat_client.query_entities(f"PartitionKey eq '{key}'")
all_entities = []
async for entity in entities:
all_entities.append(entity)

await chat_client.submit_transaction(
(TransactionOperation.DELETE, entity) for entity in entities
(TransactionOperation.DELETE, entity) for entity in all_entities
)

metadata_client = await self._atable_service_client.create_table_if_not_exists(
Expand Down Expand Up @@ -313,7 +321,7 @@ async def adelete_message(self, key: str, idx: int) -> Optional[ChatMessage]:
if idx == message_count - 1:
metadata["LastMessageRowKey"] = self._to_row_key(idx - 1)
metadata["MessageCount"] = message_count - 1
metadata_client.upsert_entity(metadata, mode=UpdateMode.MERGE)
await metadata_client.upsert_entity(metadata, mode=UpdateMode.MERGE)

def delete_last_message(self, key: str) -> Optional[ChatMessage]:
"""Delete last message for a key."""
Expand Down Expand Up @@ -408,13 +416,15 @@ def _get_default_metadata(self, key: str) -> dict:
"MessageCount": 0,
}

def _get_or_default_metadata(self, metadata_client: TableClient, key: str) -> dict:
async def _get_or_default_metadata(
self, metadata_client: TableClient, key: str
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TableClient import in line 7 is unsuitable here.
from azure.data.tables.aio import TableClient is better.

) -> dict:
"""
Retrieve metadata if it exists, otherwise return default metadata
structure.
"""
try:
return metadata_client.get_entity(
return await metadata_client.get_entity(
partition_key=self.metadata_partition_key, row_key=key
)
except ResourceNotFoundError:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ exclude = ["**/BUILD"]
license = "MIT"
name = "llama-index-storage-chat-store-azure"
readme = "README.md"
version = "0.2.2"
version = "0.2.3"

[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
Expand Down
Loading