Skip to content

Commit 2510bd3

Browse files
committed
Address PR feedback: optimize logging and encapsulate document_id access
- Gate debug logging behind isEnabledFor check to avoid unnecessary computation - Add Chunk.document_id property to safely handle metadata/chunk_metadata extraction - Simplify RAG memory code using new property
1 parent a14f79a commit 2510bd3

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

llama_stack/apis/vector_io/vector_io.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ def chunk_id(self) -> str:
9191

9292
return generate_chunk_id(str(uuid.uuid4()), str(self.content))
9393

94+
@property
95+
def document_id(self) -> str | None:
96+
"""Returns the document_id from either metadata or chunk_metadata, with metadata taking precedence."""
97+
# Check metadata first (takes precedence)
98+
doc_id = self.metadata.get("document_id")
99+
if isinstance(doc_id, str):
100+
return doc_id
101+
102+
# Fall back to chunk_metadata if available
103+
if self.chunk_metadata is not None:
104+
chunk_doc_id = getattr(self.chunk_metadata, "document_id", None)
105+
if isinstance(chunk_doc_id, str):
106+
return chunk_doc_id
107+
108+
return None
109+
94110

95111
@json_schema_type
96112
class QueryChunksResponse(BaseModel):

llama_stack/core/routers/vector_io.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# the root directory of this source tree.
66

77
import asyncio
8+
import logging
89
import uuid
910
from typing import Any
1011

@@ -101,13 +102,12 @@ async def insert_chunks(
101102
chunks: list[Chunk],
102103
ttl_seconds: int | None = None,
103104
) -> None:
104-
doc_ids = [
105-
getattr(chunk.chunk_metadata, "document_id", None) if chunk.chunk_metadata else None for chunk in chunks[:3]
106-
]
107-
logger.debug(
108-
f"VectorIORouter.insert_chunks: {vector_db_id}, {len(chunks)} chunks, "
109-
f"ttl_seconds={ttl_seconds}, chunk_ids={doc_ids}{' and more...' if len(chunks) > 3 else ''}"
110-
)
105+
if logger.isEnabledFor(logging.DEBUG):
106+
doc_ids = [chunk.document_id for chunk in chunks[:3]]
107+
logger.debug(
108+
f"VectorIORouter.insert_chunks: {vector_db_id}, {len(chunks)} chunks, "
109+
f"ttl_seconds={ttl_seconds}, chunk_ids={doc_ids}{' and more...' if len(chunks) > 3 else ''}"
110+
)
111111
provider = await self.routing_table.get_provider_impl(vector_db_id)
112112
await provider.insert_chunks(vector_db_id, chunks, ttl_seconds)
113113

llama_stack/providers/inline/tool_runtime/rag/memory.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,7 @@ async def query(
279279
return RAGQueryResult(
280280
content=picked,
281281
metadata={
282-
"document_ids": [
283-
c.metadata.get("document_id") or (c.chunk_metadata.document_id if c.chunk_metadata else None)
284-
for c in chunks[: len(picked)]
285-
],
282+
"document_ids": [c.document_id for c in chunks[: len(picked)]],
286283
"chunks": [c.content for c in chunks[: len(picked)]],
287284
"scores": scores[: len(picked)],
288285
"vector_db_ids": [c.metadata["vector_db_id"] for c in chunks[: len(picked)]],

0 commit comments

Comments
 (0)