File size: 6,475 Bytes
53ae001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"""

Response Models for RAG Agent API



Pydantic models for API responses.



Models:

    SourceReference: Reference to a documentation source

    QueryResponse: Successful query response with answer and sources

    ErrorResponse: Error response with details

"""

from pydantic import BaseModel, Field, HttpUrl
from typing import List, Optional


class SourceReference(BaseModel):
    """

    Reference to a documentation source used in answer generation.



    Attributes:

        url: URL of the source documentation page

        page_title: Human-readable title of the page

        relevance_score: Similarity score from vector search (0.0-1.0)

        chunk_index: Position of chunk within the page (optional)



    Example:

        {

            "url": "https://docs.ros.org/...",

            "page_title": "Writing a simple publisher",

            "relevance_score": 0.8642,

            "chunk_index": 2

        }

    """

    url: str = Field(
        ...,
        description="URL of the source documentation page"
    )

    page_title: str = Field(
        ...,
        min_length=1,
        description="Human-readable title of the documentation page"
    )

    relevance_score: float = Field(
        ...,
        ge=0.0,
        le=1.0,
        description="Similarity score from vector search (0.0-1.0, higher is more relevant)"
    )

    chunk_index: Optional[int] = Field(
        default=None,
        ge=0,
        description="Position of the chunk within the page (0-indexed)"
    )

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "url": "https://docs.ros.org/en/rolling/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Py-Publisher.html",
                    "page_title": "Writing a simple publisher (Python)",
                    "relevance_score": 0.8642,
                    "chunk_index": 2
                }
            ]
        }
    }


class QueryResponse(BaseModel):
    """

    Successful response to a query request.



    Attributes:

        query: Original user query (echoed back)

        answer: Generated natural language answer

        sources: List of source references (deduplicated by URL)

        model_used: Actual OpenAI model used for generation

        tokens_used: Total tokens consumed (prompt + completion)

        retrieval_count: Number of chunks retrieved before deduplication



    Example:

        {

            "query": "How do I create a ROS 2 publisher?",

            "answer": "To create a ROS 2 publisher...",

            "sources": [...],

            "model_used": "gpt-3.5-turbo",

            "tokens_used": 456,

            "retrieval_count": 5

        }

    """

    query: str = Field(
        ...,
        description="Original user query (echoed back)"
    )

    answer: str = Field(
        ...,
        min_length=1,
        description="Generated natural language answer"
    )

    sources: List[SourceReference] = Field(
        ...,
        description="List of source references (deduplicated by URL)"
    )

    model_used: str = Field(
        ...,
        description="Actual OpenAI model used for generation"
    )

    tokens_used: int = Field(
        ...,
        ge=0,
        description="Total tokens consumed (prompt + completion)"
    )

    retrieval_count: int = Field(
        ...,
        ge=0,
        description="Number of chunks retrieved before deduplication"
    )

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "query": "How do I create a ROS 2 publisher in Python?",
                    "answer": "To create a ROS 2 publisher in Python, you need to import rclpy, create a node class, and use create_publisher()...",
                    "sources": [
                        {
                            "url": "https://docs.ros.org/en/rolling/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Py-Publisher.html",
                            "page_title": "Writing a simple publisher (Python)",
                            "relevance_score": 0.8642,
                            "chunk_index": 2
                        }
                    ],
                    "model_used": "gpt-3.5-turbo",
                    "tokens_used": 456,
                    "retrieval_count": 5
                }
            ]
        }
    }


class ErrorResponse(BaseModel):
    """

    Error response for failed requests.



    Attributes:

        error: Error flag (always True for error responses)

        error_type: Error classification (ValidationError, RetrievalError, etc.)

        error_message: Human-readable error description

        query: Original query if available

        status_code: HTTP status code



    Example:

        {

            "error": true,

            "error_type": "ValidationError",

            "error_message": "Query cannot be empty",

            "query": "",

            "status_code": 422

        }

    """

    error: bool = Field(
        default=True,
        description="Error flag (always True for error responses)"
    )

    error_type: str = Field(
        ...,
        min_length=1,
        description="Error classification"
    )

    error_message: str = Field(
        ...,
        min_length=1,
        description="Human-readable error description"
    )

    query: Optional[str] = Field(
        default=None,
        description="Original query if available"
    )

    status_code: int = Field(
        ...,
        ge=400,
        le=599,
        description="HTTP status code"
    )

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "error": True,
                    "error_type": "ValidationError",
                    "error_message": "Query cannot be empty or whitespace only",
                    "query": "",
                    "status_code": 422
                },
                {
                    "error": True,
                    "error_type": "RetrievalError",
                    "error_message": "Vector database is currently unavailable",
                    "query": "How do I create a publisher?",
                    "status_code": 503
                }
            ]
        }
    }