File size: 13,752 Bytes
55a76a3 | 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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | # API_SPECIFICATION.md — API Specification
## RAG-Based Technical Documentation Assistant
**Version:** 1.0.0
**Base URL:** `http://localhost:8000`
**OpenAPI Docs:** `http://localhost:8000/docs`
**ReDoc:** `http://localhost:8000/redoc`
---
## Global Response Conventions
All responses return `Content-Type: application/json`.
### Standard Error Response
```json
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Human-readable description",
"details": {}
},
"request_id": "req_abc123"
}
```
### HTTP Status Code Reference
| Code | Meaning |
|------|---------|
| 200 | Success |
| 201 | Created (new resource ingested) |
| 400 | Bad Request (invalid input) |
| 404 | Not Found |
| 422 | Unprocessable Entity (Pydantic validation failure) |
| 429 | Rate Limited |
| 503 | Service Unavailable (LLM or vector store down) |
| 500 | Internal Server Error |
---
## Endpoints
---
### POST /query
**Purpose:** Submit a natural language question and receive a grounded, cited answer from the documentation corpus.
#### Request Schema
```json
{
"question": "string", // required, 1-2000 chars
"session_id": "string", // optional, UUID for conversation memory
"top_k": 5, // optional, 1-20, default 5
"max_retries": 2 // optional, 0-5, default from config
}
```
#### Pydantic Model
```python
class QueryRequest(BaseModel):
question: str = Field(..., min_length=1, max_length=2000,
description="Natural language question")
session_id: Optional[str] = Field(None, description="Session ID for conversation memory")
top_k: Optional[int] = Field(5, ge=1, le=20, description="Number of chunks to retrieve")
max_retries: Optional[int] = Field(None, ge=0, le=5,
description="Override max retry count")
```
#### Response Schema
```json
{
"answer": "string", // generated answer text with inline citations
"sources": [
{
"source_file": "string",
"document_id": "string",
"chunk_index": 0,
"excerpt": "string" // first 100 chars of chunk
}
],
"query_type": "string", // conceptual | how-to | troubleshooting | api-reference
"rewritten_query": "string", // the query actually used for retrieval
"retry_count": 0, // number of query rewrites performed
"is_fallback": false, // true if insufficient context was found
"response_time_ms": 1234,
"session_id": "string" // echoed back if provided
}
```
#### Validation Rules
- `question` must be 1-2000 characters
- `question` must not be empty or whitespace-only
- `top_k` must be between 1 and 20 (inclusive)
- `session_id` if provided must match UUID format: `^[0-9a-f-]{36}$`
#### Error Responses
| Code | Error Code | Condition |
|------|-----------|-----------|
| 422 | `VALIDATION_ERROR` | question exceeds 2000 chars or is empty |
| 503 | `LLM_UNAVAILABLE` | LLM provider API is down |
| 503 | `VECTOR_STORE_ERROR` | ChromaDB unavailable |
| 500 | `WORKFLOW_ERROR` | Unexpected error in LangGraph |
#### Example Request
```bash
curl -X POST http://localhost:8000/query \
-H "Content-Type: application/json" \
-d '{
"question": "How do I create a custom tool in LangChain?",
"top_k": 5
}'
```
#### Example Response (Success)
```json
{
"answer": "To create a custom tool in LangChain, you can use the @tool decorator or inherit from the BaseTool class. [Source: langchain_docs.md]\n\nUsing the decorator approach:\n```python\nfrom langchain.tools import tool\n\n@tool\ndef my_tool(input: str) -> str:\n \"\"\"Describe what the tool does.\"\"\"\n return f\"Result: {input}\"\n```\n[Source: langchain_docs.md, chunk 12]",
"sources": [
{
"source_file": "langchain_docs.md",
"document_id": "doc_001",
"chunk_index": 12,
"excerpt": "To create a custom tool, use the @tool decorator. The function's docstring..."
}
],
"query_type": "how-to",
"rewritten_query": "LangChain custom tool creation @tool decorator BaseTool class",
"retry_count": 0,
"is_fallback": false,
"response_time_ms": 2341,
"session_id": null
}
```
#### Example Response (Fallback)
```json
{
"answer": "I was unable to find relevant information in the documentation corpus to answer your question. Please check that your question is related to the indexed documents, or consider rephrasing it.",
"sources": [],
"query_type": "conceptual",
"rewritten_query": "React hooks useEffect tutorial",
"retry_count": 2,
"is_fallback": true,
"response_time_ms": 5876,
"session_id": null
}
```
---
### POST /ingest
**Purpose:** Ingest a new document into the corpus. Accepts a file upload or a URL. The document is chunked, embedded, and stored in the vector store.
#### Request Schema (multipart/form-data)
```
file: File (optional) — .md, .txt, .html, .pdf, max 10MB
url: string (optional) — publicly accessible URL to fetch
```
At least one of `file` or `url` must be provided.
#### Validation Rules
- Exactly one of `file` or `url` must be provided (not both, not neither)
- Allowed file extensions: `.md`, `.txt`, `.html`, `.pdf`
- Maximum file size: 10MB
- URL must be a valid `http://` or `https://` URL
- File content must not be empty after loading
#### Response Schema
```json
{
"document_id": "string", // assigned document ID
"filename": "string", // original filename or URL-derived name
"chunks_indexed": 42, // number of chunks created and indexed
"file_size_bytes": 12345,
"status": "indexed", // indexed | failed
"message": "string", // human-readable summary
"duplicate": false // true if document was already indexed (dedup check)
}
```
#### Error Responses
| Code | Error Code | Condition |
|------|-----------|-----------|
| 400 | `MISSING_SOURCE` | Neither file nor url provided |
| 400 | `BOTH_SOURCES` | Both file and url provided |
| 400 | `INVALID_FILE_TYPE` | File extension not allowed |
| 400 | `FILE_TOO_LARGE` | File exceeds 10MB |
| 400 | `EMPTY_DOCUMENT` | File has no extractable text content |
| 400 | `INVALID_URL` | URL is malformed |
| 400 | `URL_FETCH_FAILED` | URL could not be fetched (404, timeout, etc.) |
| 503 | `VECTOR_STORE_ERROR` | ChromaDB unavailable |
#### Example Request (File Upload)
```bash
curl -X POST http://localhost:8000/ingest \
-F "file=@langchain_docs.md"
```
#### Example Request (URL)
```bash
curl -X POST http://localhost:8000/ingest \
-F "url=https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md"
```
#### Example Response
```json
{
"document_id": "doc_007",
"filename": "langchain_docs.md",
"chunks_indexed": 47,
"file_size_bytes": 23041,
"status": "indexed",
"message": "Document 'langchain_docs.md' successfully indexed with 47 chunks.",
"duplicate": false
}
```
#### Example Response (Duplicate)
```json
{
"document_id": "doc_001",
"filename": "langchain_docs.md",
"chunks_indexed": 0,
"file_size_bytes": 23041,
"status": "indexed",
"message": "Document 'langchain_docs.md' was already indexed. Skipping.",
"duplicate": true
}
```
---
### GET /documents
**Purpose:** List all documents that have been ingested into the corpus.
#### Query Parameters
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `status` | string | all | Filter by status: `indexed`, `failed`, `all` |
| `limit` | int | 50 | Max records to return (1-100) |
| `offset` | int | 0 | Pagination offset |
#### Response Schema
```json
{
"documents": [
{
"document_id": "string",
"filename": "string",
"source_url": "string",
"chunk_count": 47,
"file_type": "md",
"status": "indexed",
"ingestion_timestamp": "2025-06-11T10:00:00Z",
"file_size_bytes": 23041
}
],
"total": 5,
"limit": 50,
"offset": 0
}
```
#### Error Responses
| Code | Error Code | Condition |
|------|-----------|-----------|
| 422 | `VALIDATION_ERROR` | Invalid query parameter value |
| 503 | `REGISTRY_ERROR` | Document registry unavailable |
#### Example Request
```bash
curl http://localhost:8000/documents
curl "http://localhost:8000/documents?status=indexed&limit=10"
```
#### Example Response
```json
{
"documents": [
{
"document_id": "doc_001",
"filename": "langchain_docs.md",
"source_url": "",
"chunk_count": 47,
"file_type": "md",
"status": "indexed",
"ingestion_timestamp": "2025-06-11T10:00:00Z",
"file_size_bytes": 23041
},
{
"document_id": "doc_002",
"filename": "fastapi_tutorial.md",
"source_url": "https://fastapi.tiangolo.com/tutorial/",
"chunk_count": 31,
"file_type": "md",
"status": "indexed",
"ingestion_timestamp": "2025-06-11T10:05:00Z",
"file_size_bytes": 15600
}
],
"total": 2,
"limit": 50,
"offset": 0
}
```
---
### POST /feedback
**Purpose:** Submit user feedback (thumbs up/down) on a generated answer. Used for offline quality monitoring.
#### Request Schema
```json
{
"query": "string", // required, the question that was asked
"answer": "string", // required, the answer that was given
"rating": "thumbs_up", // required: "thumbs_up" | "thumbs_down"
"comment": "string", // optional, free text comment
"session_id": "string", // optional, for correlation
"response_time_ms": 1234 // optional, from query response
}
```
#### Pydantic Model
```python
class FeedbackRequest(BaseModel):
query: str = Field(..., min_length=1, max_length=2000)
answer: str = Field(..., min_length=1, max_length=10000)
rating: Literal["thumbs_up", "thumbs_down"]
comment: Optional[str] = Field(None, max_length=1000)
session_id: Optional[str] = None
response_time_ms: Optional[int] = Field(None, ge=0)
```
#### Response Schema
```json
{
"feedback_id": "string", // UUID assigned to this feedback record
"status": "recorded",
"message": "Thank you for your feedback."
}
```
#### Error Responses
| Code | Error Code | Condition |
|------|-----------|-----------|
| 422 | `VALIDATION_ERROR` | rating not one of allowed values |
| 422 | `VALIDATION_ERROR` | query or answer empty |
| 503 | `STORAGE_ERROR` | Feedback database unavailable |
#### Example Request
```bash
curl -X POST http://localhost:8000/feedback \
-H "Content-Type: application/json" \
-d '{
"query": "How do I create a custom tool in LangChain?",
"answer": "To create a custom tool, use the @tool decorator...",
"rating": "thumbs_up",
"comment": "Very helpful, exactly what I needed"
}'
```
#### Example Response
```json
{
"feedback_id": "fb_d4e5f6a7-b8c9-0d1e-2f3a-4b5c6d7e8f9a",
"status": "recorded",
"message": "Thank you for your feedback."
}
```
---
### GET /health
**Purpose:** Health check endpoint for load balancers and monitoring systems.
#### Response Schema
```json
{
"status": "healthy", // healthy | degraded | unhealthy
"version": "1.0.0",
"components": {
"vector_store": "ok", // ok | error
"document_registry": "ok",
"llm_provider": "ok",
"embedding_model": "ok"
},
"corpus_size": 5, // number of indexed documents
"timestamp": "2025-06-11T10:00:00Z"
}
```
#### Example Request
```bash
curl http://localhost:8000/health
```
---
### DELETE /documents/{document_id}
**Purpose:** Remove a document and all its chunks from the vector store and registry.
#### Path Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `document_id` | string | The document ID from GET /documents |
#### Response Schema
```json
{
"document_id": "string",
"chunks_removed": 47,
"status": "deleted",
"message": "Document 'doc_001' and 47 chunks have been removed."
}
```
#### Error Responses
| Code | Error Code | Condition |
|------|-----------|-----------|
| 404 | `DOCUMENT_NOT_FOUND` | document_id does not exist |
| 503 | `VECTOR_STORE_ERROR` | ChromaDB unavailable |
#### Example Request
```bash
curl -X DELETE http://localhost:8000/documents/doc_001
```
---
### GET /feedback
**Purpose:** Retrieve stored feedback records for analysis (admin use).
#### Query Parameters
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `rating` | string | all | Filter: `thumbs_up`, `thumbs_down`, `all` |
| `limit` | int | 50 | Max records (1-100) |
| `offset` | int | 0 | Pagination offset |
#### Example Request
```bash
curl "http://localhost:8000/feedback?rating=thumbs_down&limit=20"
```
#### Example Response
```json
{
"feedback": [
{
"feedback_id": "fb_abc123",
"query": "How do I install FastAPI?",
"rating": "thumbs_down",
"comment": "Answer was about the wrong version",
"created_at": "2025-06-11T11:00:00Z"
}
],
"total": 1,
"limit": 50,
"offset": 0
}
```
---
## FastAPI Application Setup
```python
# app/main.py
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api.routes import query, ingest, documents, feedback, health
app = FastAPI(
title="RAG Technical Documentation Assistant",
description="Self-corrective RAG system powered by LangGraph",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # restrict in production
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(query.router)
app.include_router(ingest.router)
app.include_router(documents.router)
app.include_router(feedback.router)
app.include_router(health.router)
``` |