mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-06-27 18:50:41 +00:00
# What does this PR do? Adding `ChunkMetadata` so we can properly delete embeddings later. More specifically, this PR refactors and extends the chunk metadata handling in the vector database and introduces a distinction between metadata used for model context and backend-only metadata required for chunk management, storage, and retrieval. It also improves chunk ID generation and propagation throughout the stack, enhances test coverage, and adds new utility modules. ```python class ChunkMetadata(BaseModel): """ `ChunkMetadata` is backend metadata for a `Chunk` that is used to store additional information about the chunk that will NOT be inserted into the context during inference, but is required for backend functionality. Use `metadata` in `Chunk` for metadata that will be used during inference. """ document_id: str | None = None chunk_id: str | None = None source: str | None = None created_timestamp: int | None = None updated_timestamp: int | None = None chunk_window: str | None = None chunk_tokenizer: str | None = None chunk_embedding_model: str | None = None chunk_embedding_dimension: int | None = None content_token_count: int | None = None metadata_token_count: int | None = None ``` Eventually we can migrate the document_id out of the `metadata` field. I've introduced the changes so that `ChunkMetadata` is backwards compatible with `metadata`. <!-- If resolving an issue, uncomment and update the line below --> Closes https://github.com/meta-llama/llama-stack/issues/2501 ## Test Plan Added unit tests --------- Signed-off-by: Francisco Javier Arceo <farceo@redhat.com>
62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
# All rights reserved.
|
|
#
|
|
# This source code is licensed under the terms described in the LICENSE file in
|
|
# the root directory of this source tree.
|
|
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
from llama_stack.apis.vector_io import (
|
|
Chunk,
|
|
ChunkMetadata,
|
|
QueryChunksResponse,
|
|
)
|
|
from llama_stack.providers.inline.tool_runtime.rag.memory import MemoryToolRuntimeImpl
|
|
|
|
|
|
class TestRagQuery:
|
|
@pytest.mark.asyncio
|
|
async def test_query_raises_on_empty_vector_db_ids(self):
|
|
rag_tool = MemoryToolRuntimeImpl(config=MagicMock(), vector_io_api=MagicMock(), inference_api=MagicMock())
|
|
with pytest.raises(ValueError):
|
|
await rag_tool.query(content=MagicMock(), vector_db_ids=[])
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_query_chunk_metadata_handling(self):
|
|
rag_tool = MemoryToolRuntimeImpl(config=MagicMock(), vector_io_api=MagicMock(), inference_api=MagicMock())
|
|
content = "test query content"
|
|
vector_db_ids = ["db1"]
|
|
|
|
chunk_metadata = ChunkMetadata(
|
|
document_id="doc1",
|
|
chunk_id="chunk1",
|
|
source="test_source",
|
|
metadata_token_count=5,
|
|
)
|
|
interleaved_content = MagicMock()
|
|
chunk = Chunk(
|
|
content=interleaved_content,
|
|
metadata={
|
|
"key1": "value1",
|
|
"token_count": 10,
|
|
"metadata_token_count": 5,
|
|
# Note this is inserted into `metadata` during MemoryToolRuntimeImpl().insert()
|
|
"document_id": "doc1",
|
|
},
|
|
stored_chunk_id="chunk1",
|
|
chunk_metadata=chunk_metadata,
|
|
)
|
|
|
|
query_response = QueryChunksResponse(chunks=[chunk], scores=[1.0])
|
|
|
|
rag_tool.vector_io_api.query_chunks = AsyncMock(return_value=query_response)
|
|
result = await rag_tool.query(content=content, vector_db_ids=vector_db_ids)
|
|
|
|
assert result is not None
|
|
expected_metadata_string = (
|
|
"Metadata: {'chunk_id': 'chunk1', 'document_id': 'doc1', 'source': 'test_source', 'key1': 'value1'}"
|
|
)
|
|
assert expected_metadata_string in result.content[1].text
|
|
assert result.content is not None
|