Skip to content

Commit a1762d7

Browse files
committed
Implement feedback on refactoring and unused code
1 parent 5588b50 commit a1762d7

5 files changed

Lines changed: 19 additions & 69 deletions

File tree

src/app/endpoints/rags.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from authentication.interface import AuthTuple
1212
from authorization.middleware import authorize
1313
from client import AsyncLlamaStackClientHolder
14-
from configuration import LogicError, configuration
15-
from models.config import Action
14+
from configuration import configuration
15+
from models.config import Action, ByokRag
1616
from models.responses import (
1717
ForbiddenResponse,
1818
InternalServerErrorResponse,
@@ -104,7 +104,7 @@ async def rags_endpoint_handler(
104104
raise HTTPException(**response.model_dump()) from e
105105

106106

107-
def _resolve_rag_id_to_vector_db_id(rag_id: str) -> str:
107+
def _resolve_rag_id_to_vector_db_id(rag_id: str, byok_rags: list[ByokRag]) -> str:
108108
"""Resolve a user-facing rag_id to the llama-stack vector_db_id.
109109
110110
Checks if the given ID matches a rag_id in the BYOK config and returns
@@ -113,15 +113,11 @@ def _resolve_rag_id_to_vector_db_id(rag_id: str) -> str:
113113
114114
Parameters:
115115
rag_id: The user-provided RAG identifier.
116+
byok_rags: List of BYOK RAG config entries.
116117
117118
Returns:
118119
The llama-stack vector_db_id, or the original ID if no mapping found.
119120
"""
120-
try:
121-
byok_rags = configuration.configuration.byok_rag
122-
except (AttributeError, RuntimeError, LogicError):
123-
return rag_id
124-
125121
for brag in byok_rags:
126122
if brag.rag_id == rag_id:
127123
return brag.vector_db_id
@@ -164,7 +160,9 @@ async def get_rag_endpoint_handler(
164160
logger.info("Llama stack config: %s", llama_stack_configuration)
165161

166162
# Resolve user-facing rag_id to llama-stack vector_db_id
167-
vector_db_id = _resolve_rag_id_to_vector_db_id(rag_id)
163+
vector_db_id = _resolve_rag_id_to_vector_db_id(
164+
rag_id, configuration.configuration.byok_rag
165+
)
168166

169167
try:
170168
# try to get Llama Stack client

src/models/context.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"""Context objects for internal operations."""
22

33
from dataclasses import dataclass, field
4-
from typing import Any
5-
64
from llama_stack_client import AsyncLlamaStackClient
75

86
from models.requests import QueryRequest
@@ -24,7 +22,6 @@ class ResponseGeneratorContext: # pylint: disable=too-many-instance-attributes
2422
query_request: The query request object
2523
started_at: Timestamp when the request started (ISO 8601 format)
2624
client: The Llama Stack client for API interactions
27-
metadata_map: Dictionary for storing metadata from tool responses
2825
vector_store_ids: Vector store IDs used in the query for source resolution.
2926
rag_id_mapping: Mapping from vector_db_id to user-facing rag_id.
3027
"""
@@ -43,7 +40,6 @@ class ResponseGeneratorContext: # pylint: disable=too-many-instance-attributes
4340

4441
# Dependencies & State
4542
client: AsyncLlamaStackClient
46-
metadata_map: dict[str, dict[str, Any]] = field(default_factory=dict)
4743

4844
# RAG index identification
4945
vector_store_ids: list[str] = field(default_factory=list)

src/utils/endpoints.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -196,27 +196,6 @@ def check_configuration_loaded(config: AppConfig) -> None:
196196
raise HTTPException(**response.model_dump()) from e
197197

198198

199-
def create_rag_chunks_dict(summary: TurnSummary) -> list[dict[str, Any]]:
200-
"""
201-
Create dictionary representation of RAG chunks for streaming response.
202-
203-
Args:
204-
summary: TurnSummary containing RAG chunks
205-
206-
Returns:
207-
List of dictionaries with content, source, score, and attributes.
208-
"""
209-
return [
210-
{
211-
"content": chunk.content,
212-
"source": chunk.source,
213-
"score": chunk.score,
214-
"attributes": chunk.attributes,
215-
}
216-
for chunk in summary.rag_chunks
217-
]
218-
219-
220199
def _process_http_source(
221200
src: str, doc_urls: set[str]
222201
) -> Optional[tuple[Optional[AnyUrl], str]]:

src/utils/responses.py

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def _get_token_value(original: str, header: str) -> str | None:
407407
return tools
408408

409409

410-
def parse_referenced_documents(
410+
def parse_referenced_documents( # pylint: disable=too-many-locals
411411
response: Optional[OpenAIResponseObject],
412412
vector_store_ids: Optional[list[str]] = None,
413413
rag_id_mapping: Optional[dict[str, str]] = None,
@@ -430,16 +430,17 @@ def parse_referenced_documents(
430430
if response is None or not response.output:
431431
return documents
432432

433-
resolved_source = _resolve_single_store_source(
434-
vector_store_ids or [], rag_id_mapping or {}
435-
)
433+
vs_ids = vector_store_ids or []
434+
id_mapping = rag_id_mapping or {}
436435

437436
for output_item in response.output:
438437
item_type = getattr(output_item, "type", None)
439438

440439
if item_type == "file_search_call":
441440
results = getattr(output_item, "results", []) or []
442441
for result in results:
442+
resolved_source = _resolve_source_for_result(result, vs_ids, id_mapping)
443+
443444
# Handle both object and dict access
444445
if isinstance(result, dict):
445446
attributes = result.get("attributes", {})
@@ -472,26 +473,6 @@ def parse_referenced_documents(
472473
return documents
473474

474475

475-
def _resolve_single_store_source(
476-
vector_store_ids: list[str],
477-
rag_id_mapping: dict[str, str],
478-
) -> Optional[str]:
479-
"""Resolve source name when there is exactly one vector store.
480-
481-
Parameters:
482-
vector_store_ids: The vector store IDs used in the query.
483-
rag_id_mapping: Mapping from vector_db_id to user-facing rag_id.
484-
485-
Returns:
486-
The resolved rag_id (or raw store_id as fallback) if exactly one
487-
store is used, None otherwise.
488-
"""
489-
if len(vector_store_ids) == 1:
490-
store_id = vector_store_ids[0]
491-
return rag_id_mapping.get(store_id, store_id)
492-
return None
493-
494-
495476
def extract_token_usage(
496477
response: Optional[OpenAIResponseObject], model_id: str
497478
) -> TokenCounter:

tests/unit/app/endpoints/test_rags.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -374,20 +374,16 @@ def __init__(self) -> None:
374374
assert response.id == "ocp-4.18-docs"
375375

376376

377-
def test_resolve_rag_id_to_vector_db_id_with_mapping(
378-
mocker: MockerFixture, tmp_path: Path
379-
) -> None:
377+
def test_resolve_rag_id_to_vector_db_id_with_mapping(tmp_path: Path) -> None:
380378
"""Test that _resolve_rag_id_to_vector_db_id maps rag_id to vector_db_id."""
381379
byok_config = _make_byok_config(str(tmp_path))
382-
mocker.patch("app.endpoints.rags.configuration", byok_config)
383-
assert _resolve_rag_id_to_vector_db_id("ocp-4.18-docs") == "vs_abc123"
384-
assert _resolve_rag_id_to_vector_db_id("company-kb") == "vs_def456"
380+
byok_rags = byok_config.configuration.byok_rag
381+
assert _resolve_rag_id_to_vector_db_id("ocp-4.18-docs", byok_rags) == "vs_abc123"
382+
assert _resolve_rag_id_to_vector_db_id("company-kb", byok_rags) == "vs_def456"
385383

386384

387-
def test_resolve_rag_id_to_vector_db_id_passthrough(
388-
mocker: MockerFixture, tmp_path: Path
389-
) -> None:
385+
def test_resolve_rag_id_to_vector_db_id_passthrough(tmp_path: Path) -> None:
390386
"""Test that unmapped IDs are passed through unchanged."""
391387
byok_config = _make_byok_config(str(tmp_path))
392-
mocker.patch("app.endpoints.rags.configuration", byok_config)
393-
assert _resolve_rag_id_to_vector_db_id("vs_unknown") == "vs_unknown"
388+
byok_rags = byok_config.configuration.byok_rag
389+
assert _resolve_rag_id_to_vector_db_id("vs_unknown", byok_rags) == "vs_unknown"

0 commit comments

Comments
 (0)