mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-10-04 04:04:14 +00:00
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
This commit is contained in:
parent
a14f79a362
commit
2510bd349e
3 changed files with 24 additions and 11 deletions
|
@ -91,6 +91,22 @@ class Chunk(BaseModel):
|
||||||
|
|
||||||
return generate_chunk_id(str(uuid.uuid4()), str(self.content))
|
return generate_chunk_id(str(uuid.uuid4()), str(self.content))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def document_id(self) -> str | None:
|
||||||
|
"""Returns the document_id from either metadata or chunk_metadata, with metadata taking precedence."""
|
||||||
|
# Check metadata first (takes precedence)
|
||||||
|
doc_id = self.metadata.get("document_id")
|
||||||
|
if isinstance(doc_id, str):
|
||||||
|
return doc_id
|
||||||
|
|
||||||
|
# Fall back to chunk_metadata if available
|
||||||
|
if self.chunk_metadata is not None:
|
||||||
|
chunk_doc_id = getattr(self.chunk_metadata, "document_id", None)
|
||||||
|
if isinstance(chunk_doc_id, str):
|
||||||
|
return chunk_doc_id
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
@json_schema_type
|
@json_schema_type
|
||||||
class QueryChunksResponse(BaseModel):
|
class QueryChunksResponse(BaseModel):
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
# the root directory of this source tree.
|
# the root directory of this source tree.
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import logging
|
||||||
import uuid
|
import uuid
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
@ -101,9 +102,8 @@ class VectorIORouter(VectorIO):
|
||||||
chunks: list[Chunk],
|
chunks: list[Chunk],
|
||||||
ttl_seconds: int | None = None,
|
ttl_seconds: int | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
doc_ids = [
|
if logger.isEnabledFor(logging.DEBUG):
|
||||||
getattr(chunk.chunk_metadata, "document_id", None) if chunk.chunk_metadata else None for chunk in chunks[:3]
|
doc_ids = [chunk.document_id for chunk in chunks[:3]]
|
||||||
]
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
f"VectorIORouter.insert_chunks: {vector_db_id}, {len(chunks)} chunks, "
|
f"VectorIORouter.insert_chunks: {vector_db_id}, {len(chunks)} chunks, "
|
||||||
f"ttl_seconds={ttl_seconds}, chunk_ids={doc_ids}{' and more...' if len(chunks) > 3 else ''}"
|
f"ttl_seconds={ttl_seconds}, chunk_ids={doc_ids}{' and more...' if len(chunks) > 3 else ''}"
|
||||||
|
|
|
@ -279,10 +279,7 @@ class MemoryToolRuntimeImpl(ToolGroupsProtocolPrivate, ToolRuntime, RAGToolRunti
|
||||||
return RAGQueryResult(
|
return RAGQueryResult(
|
||||||
content=picked,
|
content=picked,
|
||||||
metadata={
|
metadata={
|
||||||
"document_ids": [
|
"document_ids": [c.document_id for c in chunks[: len(picked)]],
|
||||||
c.metadata.get("document_id") or (c.chunk_metadata.document_id if c.chunk_metadata else None)
|
|
||||||
for c in chunks[: len(picked)]
|
|
||||||
],
|
|
||||||
"chunks": [c.content for c in chunks[: len(picked)]],
|
"chunks": [c.content for c in chunks[: len(picked)]],
|
||||||
"scores": scores[: len(picked)],
|
"scores": scores[: len(picked)],
|
||||||
"vector_db_ids": [c.metadata["vector_db_id"] for c in chunks[: len(picked)]],
|
"vector_db_ids": [c.metadata["vector_db_id"] for c in chunks[: len(picked)]],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue