|
1 | 1 | """Utility functions for endpoint handlers.""" |
2 | 2 |
|
3 | 3 | from contextlib import suppress |
| 4 | +from typing import Any |
4 | 5 | from fastapi import HTTPException, status |
5 | 6 | from llama_stack_client._client import AsyncLlamaStackClient |
6 | 7 | from llama_stack_client.lib.agents.agent import AsyncAgent |
| 8 | +from pydantic import AnyUrl, ValidationError |
7 | 9 |
|
8 | 10 | import constants |
9 | 11 | from models.cache_entry import CacheEntry |
10 | 12 | from models.requests import QueryRequest |
| 13 | +from models.responses import ReferencedDocument |
11 | 14 | from models.database.conversations import UserConversation |
12 | 15 | from models.config import Action |
13 | 16 | from app.database import get_session |
14 | 17 | from configuration import AppConfig |
15 | 18 | from utils.suid import get_suid |
| 19 | +from utils.types import TurnSummary |
16 | 20 | from utils.types import GraniteToolParser |
17 | 21 |
|
18 | 22 |
|
@@ -344,3 +348,216 @@ async def get_temp_agent( |
344 | 348 | session_id = await agent.create_session(get_suid()) |
345 | 349 |
|
346 | 350 | return agent, session_id, conversation_id |
| 351 | + |
| 352 | + |
| 353 | +def create_rag_chunks_dict(summary: TurnSummary) -> list[dict[str, Any]]: |
| 354 | + """ |
| 355 | + Create dictionary representation of RAG chunks for streaming response. |
| 356 | +
|
| 357 | + Args: |
| 358 | + summary: TurnSummary containing RAG chunks |
| 359 | +
|
| 360 | + Returns: |
| 361 | + List of dictionaries with content, source, and score |
| 362 | + """ |
| 363 | + return [ |
| 364 | + {"content": chunk.content, "source": chunk.source, "score": chunk.score} |
| 365 | + for chunk in summary.rag_chunks |
| 366 | + ] |
| 367 | + |
| 368 | + |
| 369 | +def _process_http_source( |
| 370 | + src: str, doc_urls: set[str] |
| 371 | +) -> tuple[AnyUrl | None, str] | None: |
| 372 | + """Process HTTP source and return (doc_url, doc_title) tuple.""" |
| 373 | + if src not in doc_urls: |
| 374 | + doc_urls.add(src) |
| 375 | + try: |
| 376 | + validated_url = AnyUrl(src) |
| 377 | + except ValidationError: |
| 378 | + logger.warning("Invalid URL in chunk source: %s", src) |
| 379 | + validated_url = None |
| 380 | + |
| 381 | + doc_title = src.rsplit("/", 1)[-1] or src |
| 382 | + return (validated_url, doc_title) |
| 383 | + return None |
| 384 | + |
| 385 | + |
| 386 | +def _process_document_id( |
| 387 | + src: str, |
| 388 | + doc_ids: set[str], |
| 389 | + doc_urls: set[str], |
| 390 | + metas_by_id: dict[str, dict[str, Any]], |
| 391 | + metadata_map: dict[str, Any] | None, |
| 392 | +) -> tuple[AnyUrl | None, str] | None: |
| 393 | + """Process document ID and return (doc_url, doc_title) tuple.""" |
| 394 | + if src in doc_ids: |
| 395 | + return None |
| 396 | + doc_ids.add(src) |
| 397 | + |
| 398 | + meta = metas_by_id.get(src, {}) if metadata_map else {} |
| 399 | + doc_url = meta.get("docs_url") |
| 400 | + title = meta.get("title") |
| 401 | + # Type check to ensure we have the right types |
| 402 | + if not isinstance(doc_url, (str, type(None))): |
| 403 | + doc_url = None |
| 404 | + if not isinstance(title, (str, type(None))): |
| 405 | + title = None |
| 406 | + |
| 407 | + if doc_url: |
| 408 | + if doc_url in doc_urls: |
| 409 | + return None |
| 410 | + doc_urls.add(doc_url) |
| 411 | + |
| 412 | + try: |
| 413 | + validated_doc_url = None |
| 414 | + if doc_url and doc_url.startswith("http"): |
| 415 | + validated_doc_url = AnyUrl(doc_url) |
| 416 | + except ValidationError: |
| 417 | + logger.warning("Invalid URL in metadata: %s", doc_url) |
| 418 | + validated_doc_url = None |
| 419 | + |
| 420 | + doc_title = title or (doc_url.rsplit("/", 1)[-1] if doc_url else src) |
| 421 | + return (validated_doc_url, doc_title) |
| 422 | + |
| 423 | + |
| 424 | +def _add_additional_metadata_docs( |
| 425 | + doc_urls: set[str], |
| 426 | + metas_by_id: dict[str, dict[str, Any]], |
| 427 | +) -> list[tuple[AnyUrl | None, str]]: |
| 428 | + """Add additional referenced documents from metadata_map.""" |
| 429 | + additional_entries: list[tuple[AnyUrl | None, str]] = [] |
| 430 | + for meta in metas_by_id.values(): |
| 431 | + doc_url = meta.get("docs_url") |
| 432 | + title = meta.get("title") # Note: must be "title", not "Title" |
| 433 | + # Type check to ensure we have the right types |
| 434 | + if not isinstance(doc_url, (str, type(None))): |
| 435 | + doc_url = None |
| 436 | + if not isinstance(title, (str, type(None))): |
| 437 | + title = None |
| 438 | + if doc_url and doc_url not in doc_urls and title is not None: |
| 439 | + doc_urls.add(doc_url) |
| 440 | + try: |
| 441 | + validated_url = None |
| 442 | + if doc_url.startswith("http"): |
| 443 | + validated_url = AnyUrl(doc_url) |
| 444 | + except ValidationError: |
| 445 | + logger.warning("Invalid URL in metadata_map: %s", doc_url) |
| 446 | + validated_url = None |
| 447 | + |
| 448 | + additional_entries.append((validated_url, title)) |
| 449 | + return additional_entries |
| 450 | + |
| 451 | + |
| 452 | +def _process_rag_chunks_for_documents( |
| 453 | + rag_chunks: list, |
| 454 | + metadata_map: dict[str, Any] | None = None, |
| 455 | +) -> list[tuple[AnyUrl | None, str]]: |
| 456 | + """ |
| 457 | + Process RAG chunks and return a list of (doc_url, doc_title) tuples. |
| 458 | +
|
| 459 | + This is the core logic shared between both return formats. |
| 460 | + """ |
| 461 | + doc_urls: set[str] = set() |
| 462 | + doc_ids: set[str] = set() |
| 463 | + |
| 464 | + # Process metadata_map if provided |
| 465 | + metas_by_id: dict[str, dict[str, Any]] = {} |
| 466 | + if metadata_map: |
| 467 | + metas_by_id = {k: v for k, v in metadata_map.items() if isinstance(v, dict)} |
| 468 | + |
| 469 | + document_entries: list[tuple[AnyUrl | None, str]] = [] |
| 470 | + |
| 471 | + for chunk in rag_chunks: |
| 472 | + src = chunk.source |
| 473 | + if not src or src == constants.DEFAULT_RAG_TOOL: |
| 474 | + continue |
| 475 | + |
| 476 | + if src.startswith("http"): |
| 477 | + entry = _process_http_source(src, doc_urls) |
| 478 | + if entry: |
| 479 | + document_entries.append(entry) |
| 480 | + else: |
| 481 | + entry = _process_document_id( |
| 482 | + src, doc_ids, doc_urls, metas_by_id, metadata_map |
| 483 | + ) |
| 484 | + if entry: |
| 485 | + document_entries.append(entry) |
| 486 | + |
| 487 | + # Add any additional referenced documents from metadata_map not already present |
| 488 | + if metadata_map: |
| 489 | + additional_entries = _add_additional_metadata_docs(doc_urls, metas_by_id) |
| 490 | + document_entries.extend(additional_entries) |
| 491 | + |
| 492 | + return document_entries |
| 493 | + |
| 494 | + |
| 495 | +def create_referenced_documents( |
| 496 | + rag_chunks: list, |
| 497 | + metadata_map: dict[str, Any] | None = None, |
| 498 | + return_dict_format: bool = False, |
| 499 | +) -> list[ReferencedDocument] | list[dict[str, str | None]]: |
| 500 | + """ |
| 501 | + Create referenced documents from RAG chunks with optional metadata enrichment. |
| 502 | +
|
| 503 | + This unified function processes RAG chunks and creates referenced documents with |
| 504 | + optional metadata enrichment, deduplication, and proper URL handling. It can return |
| 505 | + either ReferencedDocument objects (for query endpoint) or dictionaries (for streaming). |
| 506 | +
|
| 507 | + Args: |
| 508 | + rag_chunks: List of RAG chunks with source information |
| 509 | + metadata_map: Optional mapping containing metadata about referenced documents |
| 510 | + return_dict_format: If True, returns list of dicts; if False, returns list of |
| 511 | + ReferencedDocument objects |
| 512 | +
|
| 513 | + Returns: |
| 514 | + List of ReferencedDocument objects or dictionaries with doc_url and doc_title |
| 515 | + """ |
| 516 | + document_entries = _process_rag_chunks_for_documents(rag_chunks, metadata_map) |
| 517 | + |
| 518 | + if return_dict_format: |
| 519 | + return [ |
| 520 | + { |
| 521 | + "doc_url": str(doc_url) if doc_url else None, |
| 522 | + "doc_title": doc_title, |
| 523 | + } |
| 524 | + for doc_url, doc_title in document_entries |
| 525 | + ] |
| 526 | + return [ |
| 527 | + ReferencedDocument(doc_url=doc_url, doc_title=doc_title) |
| 528 | + for doc_url, doc_title in document_entries |
| 529 | + ] |
| 530 | + |
| 531 | + |
| 532 | +# Backward compatibility functions |
| 533 | +def create_referenced_documents_with_metadata( |
| 534 | + summary: TurnSummary, metadata_map: dict[str, Any] |
| 535 | +) -> list[ReferencedDocument]: |
| 536 | + """ |
| 537 | + Create referenced documents from RAG chunks with metadata enrichment for streaming. |
| 538 | +
|
| 539 | + This function now returns ReferencedDocument objects for consistency with the query endpoint. |
| 540 | + """ |
| 541 | + document_entries = _process_rag_chunks_for_documents( |
| 542 | + summary.rag_chunks, metadata_map |
| 543 | + ) |
| 544 | + return [ |
| 545 | + ReferencedDocument(doc_url=doc_url, doc_title=doc_title) |
| 546 | + for doc_url, doc_title in document_entries |
| 547 | + ] |
| 548 | + |
| 549 | + |
| 550 | +def create_referenced_documents_from_chunks( |
| 551 | + rag_chunks: list, |
| 552 | +) -> list[ReferencedDocument]: |
| 553 | + """ |
| 554 | + Create referenced documents from RAG chunks for query endpoint. |
| 555 | +
|
| 556 | + This is a backward compatibility wrapper around the unified |
| 557 | + create_referenced_documents function. |
| 558 | + """ |
| 559 | + document_entries = _process_rag_chunks_for_documents(rag_chunks) |
| 560 | + return [ |
| 561 | + ReferencedDocument(doc_url=doc_url, doc_title=doc_title) |
| 562 | + for doc_url, doc_title in document_entries |
| 563 | + ] |
0 commit comments