mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-10-04 04:04:14 +00:00
Some checks failed
SqlStore Integration Tests / test-postgres (3.13) (push) Failing after 1s
Python Package Build Test / build (3.12) (push) Failing after 1s
Test External Providers Installed via Module / test-external-providers-from-module (venv) (push) Has been skipped
SqlStore Integration Tests / test-postgres (3.12) (push) Failing after 18s
Update ReadTheDocs / update-readthedocs (push) Failing after 15s
Python Package Build Test / build (3.13) (push) Failing after 19s
Test External API and Providers / test-external (venv) (push) Failing after 17s
Integration Auth Tests / test-matrix (oauth2_token) (push) Failing after 23s
Integration Tests (Replay) / Integration Tests (, , , client=, ) (push) Failing after 22s
Unit Tests / unit-tests (3.12) (push) Failing after 19s
Unit Tests / unit-tests (3.13) (push) Failing after 19s
Vector IO Integration Tests / test-matrix (push) Failing after 23s
UI Tests / ui-tests (22) (push) Successful in 44s
Pre-commit / pre-commit (push) Successful in 1m32s
83 lines
3.1 KiB
Python
83 lines
3.1 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.tools.rag_tool import RAGQueryConfig
|
|
from llama_stack.apis.vector_io import (
|
|
Chunk,
|
|
ChunkMetadata,
|
|
QueryChunksResponse,
|
|
)
|
|
from llama_stack.providers.inline.tool_runtime.rag.memory import MemoryToolRuntimeImpl
|
|
|
|
|
|
class TestRagQuery:
|
|
async def test_query_raises_on_empty_vector_db_ids(self):
|
|
rag_tool = MemoryToolRuntimeImpl(
|
|
config=MagicMock(), vector_io_api=MagicMock(), inference_api=MagicMock(), files_api=MagicMock()
|
|
)
|
|
with pytest.raises(ValueError):
|
|
await rag_tool.query(content=MagicMock(), vector_db_ids=[])
|
|
|
|
async def test_query_chunk_metadata_handling(self):
|
|
rag_tool = MemoryToolRuntimeImpl(
|
|
config=MagicMock(), vector_io_api=MagicMock(), inference_api=MagicMock(), files_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
|
|
|
|
async def test_query_raises_incorrect_mode(self):
|
|
with pytest.raises(ValueError):
|
|
RAGQueryConfig(mode="invalid_mode")
|
|
|
|
async def test_query_accepts_valid_modes(self):
|
|
default_config = RAGQueryConfig() # Test default (vector)
|
|
assert default_config.mode == "vector"
|
|
vector_config = RAGQueryConfig(mode="vector") # Test vector
|
|
assert vector_config.mode == "vector"
|
|
keyword_config = RAGQueryConfig(mode="keyword") # Test keyword
|
|
assert keyword_config.mode == "keyword"
|
|
hybrid_config = RAGQueryConfig(mode="hybrid") # Test hybrid
|
|
assert hybrid_config.mode == "hybrid"
|
|
|
|
# Test that invalid mode raises an error
|
|
with pytest.raises(ValueError):
|
|
RAGQueryConfig(mode="wrong_mode")
|