Skip to content

Commit d238a82

Browse files
authored
Merge pull request #1300 from max-svistunov/lcore-1377-parse-index-name-from-chunk-metadata
LCORE-1377 Parse index name from chunk metadata source attribute
2 parents eb97491 + 5fb28b9 commit d238a82

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

src/utils/responses.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,16 @@ def _resolve_source_for_result(
842842

843843
if len(vector_store_ids) > 1:
844844
attributes = getattr(result, "attributes", {}) or {}
845+
846+
# Primary: read index name embedded directly by rag-content.
847+
# This value is already the user-facing rag_id, not a vector_db_id,
848+
# so no mapping is needed.
849+
attr_source: Optional[str] = attributes.get("source")
850+
if attr_source:
851+
return attr_source
852+
853+
# Fallback: if llama-stack ever populates vector_store_id in results,
854+
# use it with the rag_id_mapping.
845855
attr_store_id: Optional[str] = attributes.get("vector_store_id")
846856
if attr_store_id:
847857
return rag_id_mapping.get(attr_store_id, attr_store_id)

tests/unit/utils/test_responses.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,69 @@ def test_multiple_stores_attribute_not_in_mapping(
20432043
)
20442044
assert source == "vs-unknown"
20452045

2046+
def test_multiple_stores_source_attribute_fallback(
2047+
self, mocker: MockerFixture
2048+
) -> None:
2049+
"""Test resolution falls back to source attribute when no vector_store_id."""
2050+
mock_result = mocker.Mock()
2051+
mock_result.filename = "file-abc123"
2052+
mock_result.attributes = {"source": "ocp-documentation"}
2053+
2054+
source = _resolve_source_for_result(
2055+
mock_result,
2056+
["vs-001", "vs-002"],
2057+
{"vs-001": "ocp-4.18-docs"},
2058+
)
2059+
assert source == "ocp-documentation"
2060+
2061+
def test_multiple_stores_source_attribute_ignores_mapping(
2062+
self, mocker: MockerFixture
2063+
) -> None:
2064+
"""Test source attribute is returned directly without rag_id_mapping lookup."""
2065+
mock_result = mocker.Mock()
2066+
mock_result.filename = "file-abc123"
2067+
mock_result.attributes = {"source": "custom-index"}
2068+
2069+
source = _resolve_source_for_result(
2070+
mock_result,
2071+
["vs-001", "vs-002"],
2072+
{"custom-index": "should-not-be-used"},
2073+
)
2074+
assert source == "custom-index"
2075+
2076+
def test_multiple_stores_source_preferred_over_vector_store_id(
2077+
self, mocker: MockerFixture
2078+
) -> None:
2079+
"""Test source attribute takes precedence over vector_store_id."""
2080+
mock_result = mocker.Mock()
2081+
mock_result.filename = "file-abc123"
2082+
mock_result.attributes = {
2083+
"vector_store_id": "vs-002",
2084+
"source": "ocp-documentation",
2085+
}
2086+
2087+
source = _resolve_source_for_result(
2088+
mock_result,
2089+
["vs-001", "vs-002"],
2090+
{"vs-002": "rhel-9-docs"},
2091+
)
2092+
assert source == "ocp-documentation"
2093+
2094+
def test_multiple_stores_no_vector_store_id_no_source(
2095+
self, mocker: MockerFixture
2096+
) -> None:
2097+
"""Test resolution returns None when neither vector_store_id nor source present."""
2098+
mock_result = mocker.Mock()
2099+
mock_result.filename = "file-abc123"
2100+
mock_result.attributes = {"title": "some doc"}
2101+
2102+
source = _resolve_source_for_result(
2103+
mock_result,
2104+
["vs-001", "vs-002"],
2105+
{"vs-001": "ocp-docs"},
2106+
)
2107+
assert source is None
2108+
20462109

20472110
class TestBuildChunkAttributes:
20482111
"""Tests for _build_chunk_attributes function."""

0 commit comments

Comments
 (0)