|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from aiperf.common.decorators import implements_protocol |
| 9 | +from aiperf.common.enums import EndpointType |
| 10 | +from aiperf.common.factories import EndpointFactory |
| 11 | +from aiperf.common.models import ( |
| 12 | + ParsedResponse, |
| 13 | +) |
| 14 | +from aiperf.common.models.metadata import EndpointMetadata |
| 15 | +from aiperf.common.models.record_models import RAGSources, RequestInfo, TextResponseData |
| 16 | +from aiperf.common.protocols import EndpointProtocol, InferenceServerResponse |
| 17 | +from aiperf.common.types import JsonObject, RequestOutputT |
| 18 | +from aiperf.endpoints.base_endpoint import BaseEndpoint |
| 19 | + |
| 20 | + |
| 21 | +@implements_protocol(EndpointProtocol) |
| 22 | +@EndpointFactory.register(EndpointType.SOLIDO_RAG) |
| 23 | +class SolidoEndpoint(BaseEndpoint): |
| 24 | + """SOLIDO RAG endpoint. |
| 25 | +
|
| 26 | + SOLIDO is a RAG (Retrieval-Augmented Generation) endpoint that processes |
| 27 | + queries with filters and inference model specifications. Supports streaming |
| 28 | + responses. |
| 29 | + """ |
| 30 | + |
| 31 | + @classmethod |
| 32 | + def metadata(cls) -> EndpointMetadata: |
| 33 | + """Return SOLIDO endpoint metadata.""" |
| 34 | + return EndpointMetadata( |
| 35 | + endpoint_path="/rag/api/prompt", |
| 36 | + supports_streaming=True, |
| 37 | + produces_tokens=True, |
| 38 | + tokenizes_input=True, |
| 39 | + metrics_title="SOLIDO RAG Metrics", |
| 40 | + ) |
| 41 | + |
| 42 | + def format_payload(self, request_info: RequestInfo) -> RequestOutputT: |
| 43 | + """Format SOLIDO RAG request payload from RequestInfo. |
| 44 | +
|
| 45 | + Args: |
| 46 | + request_info: Request context including model endpoint, metadata, and turns |
| 47 | +
|
| 48 | + Returns: |
| 49 | + SOLIDO API payload with query, filters, and inference_model fields |
| 50 | + """ |
| 51 | + if not request_info.turns: |
| 52 | + raise ValueError("SOLIDO endpoint requires at least one turn.") |
| 53 | + |
| 54 | + turn = request_info.turns[-1] |
| 55 | + model_endpoint = request_info.model_endpoint |
| 56 | + |
| 57 | + # Extract query text from turn |
| 58 | + query = [content for text in turn.texts for content in text.contents if content] |
| 59 | + |
| 60 | + # Default filters for SOLIDO |
| 61 | + filters = {"family": "Solido", "tool": "SDE"} |
| 62 | + |
| 63 | + # Use the model name from the turn or model endpoint |
| 64 | + inference_model = turn.model or model_endpoint.primary_model_name |
| 65 | + |
| 66 | + payload: dict[str, Any] = { |
| 67 | + "query": query, |
| 68 | + "filters": filters, |
| 69 | + "inference_model": inference_model, |
| 70 | + } |
| 71 | + |
| 72 | + if model_endpoint.endpoint.extra: |
| 73 | + payload.update(model_endpoint.endpoint.extra) |
| 74 | + |
| 75 | + self.debug(lambda: f"Formatted SOLIDO payload: {payload}") |
| 76 | + return payload |
| 77 | + |
| 78 | + def parse_response( |
| 79 | + self, response: InferenceServerResponse |
| 80 | + ) -> ParsedResponse | None: |
| 81 | + """Parse SOLIDO API response. |
| 82 | +
|
| 83 | + Args: |
| 84 | + response: Raw response from inference server |
| 85 | +
|
| 86 | + Returns: |
| 87 | + Parsed response with extracted content or None if parsing fails |
| 88 | + """ |
| 89 | + json_obj = response.get_json() |
| 90 | + if not json_obj: |
| 91 | + self.debug(lambda: f"No JSON in response: {response.get_raw()}") |
| 92 | + return None |
| 93 | + |
| 94 | + data, sources = self._extract_solido_response_data(json_obj) |
| 95 | + return ( |
| 96 | + ParsedResponse(perf_ns=response.perf_ns, data=data, sources=sources) |
| 97 | + if data |
| 98 | + else None |
| 99 | + ) |
| 100 | + |
| 101 | + def _extract_solido_response_data( |
| 102 | + self, json_obj: JsonObject |
| 103 | + ) -> tuple[TextResponseData, RAGSources | None]: |
| 104 | + """Extract content from SOLIDO JSON response. |
| 105 | +
|
| 106 | + Args: |
| 107 | + json_obj: Deserialized SOLIDO response |
| 108 | +
|
| 109 | + Returns: |
| 110 | + Extracted response data or None if no content |
| 111 | + """ |
| 112 | + # SOLIDO responses contain a "content" field with the generated text |
| 113 | + content = json_obj.get("content") |
| 114 | + if not content: |
| 115 | + self.debug(lambda: f"No content found in SOLIDO response: {json_obj}") |
| 116 | + return None, None |
| 117 | + |
| 118 | + sources = json_obj.get("sources") |
| 119 | + if not sources: |
| 120 | + self.debug(lambda: f"No sources found in SOLIDO response: {json_obj}") |
| 121 | + |
| 122 | + return self.make_text_response_data(content), sources |
0 commit comments