A FastAPI-based REST API for searching through indexed files using AI-powered semantic search.
-
Install dependencies:
pip install -r requirement.txt
-
Start the API server:
python start_api.py
-
Test the API:
python test_api.py
Search through indexed files using semantic search.
Parameters:
q(string, required): Search query textlimit(integer, optional): Maximum number of results (default: 5, max: 50)
Example Request:
GET http://127.0.0.1:8000/search?q=ID card financials&limit=3
Example Response:
[
{
"file_path": "C:/Users/athar/Desktop/back.jpg",
"type": "image",
"tags": ["ID CARD", "BACK COVER"],
"user_caption": "ID CARD BACK COVER",
"similarity": 0.85
},
{
"file_path": "C:/Users/athar/Downloads/report.pdf_page_3",
"type": "pdf_page",
"original_pdf_path": "C:/Users/athar/Downloads/report.pdf",
"page_num": 3,
"tags": ["Samsung", "Q3 2025"],
"user_caption": "Financials",
"similarity": 0.78
}
]Manually index a file from any location.
Request Body (JSON):
{
"file_path": "C:/path/to/some/other/file.pdf",
"user_caption": "This is an optional note from the user."
}Example Request:
curl -X POST "http://127.0.0.1:8000/index-file" \
-H "Content-Type: application/json" \
-d '{
"file_path": "C:/Users/athar/Desktop/document.pdf",
"user_caption": "Important contract document"
}'Example Response (202 Accepted):
{
"status": "File accepted for processing",
"message": "File 'document.pdf' is being indexed in the background."
}Supported File Types:
- PNG, JPG, JPEG (images)
- PDF (documents)
Remove a file from the database ("Remove from Context").
Request Body (JSON):
{
"file_path": "C:/path/to/the/file_to_delete.jpg"
}Example Request:
curl -X DELETE "http://127.0.0.1:8000/indexed-file" \
-H "Content-Type: application/json" \
-d '{
"file_path": "C:/Users/athar/Desktop/old_document.pdf"
}'Example Response (200 OK):
{
"status": "File removed from Context",
"message": "File 'old_document.pdf' has been removed from the database."
}Health check endpoint.
Response:
{
"status": "healthy",
"service": "context-search-api"
}Root endpoint with API information.
Retrieves all files and entities connected to a specific tag (AI-Generated Graph).
Parameters:
name(string, required): Entity/tag name to generate graph for (e.g., "Samsung", "Q3 2025")
Example Request:
GET http://127.0.0.1:8000/graph/entity?name=Samsung
Example Response:
{
"nodes": [
{
"id": "Samsung",
"label": "Samsung",
"type": "entity"
},
{
"id": "C:/Users/athar/Downloads/report.pdf_page_3",
"label": "report.pdf_page_3",
"type": "file",
"metadata": {
"file_path": "C:/Users/athar/Downloads/report.pdf_page_3",
"tags": ["Samsung", "Q3 2025"]
}
}
],
"edges": [
{
"from": "Samsung",
"to": "C:/Users/athar/Downloads/report.pdf_page_3",
"label": "mentions"
}
]
}Creates a new, empty user-curated map.
Request Body (JSON):
{
"name": "My Thesis Project"
}Example Response (200 OK):
{
"id": 1,
"name": "My Thesis Project"
}Lists all available user-curated maps.
Example Response:
[
{
"id": 1,
"name": "My Thesis Project"
},
{
"id": 2,
"name": "Research Papers"
}
]Gets all nodes and edges for a specific user-curated map.
Example Request:
GET http://127.0.0.1:8000/maps/1
Example Response:
{
"nodes": [
{
"id": 1,
"file_path": "C:/Users/athar/Desktop/document1.pdf",
"position_x": 100,
"position_y": 150
},
{
"id": 2,
"file_path": "C:/Users/athar/Desktop/image1.jpg",
"position_x": 300,
"position_y": 200
}
],
"edges": [
{
"id": 1,
"source_node_id": 1,
"target_node_id": 2,
"label": "references"
}
]
}Adds a file node to a specific user-curated map.
Request Body (JSON):
{
"file_path": "C:/path/to/file.pdf",
"x": 100,
"y": 150
}Example Response (200 OK):
{
"status": "Node added successfully",
"message": "File 'file.pdf' added to map as node 3"
}Creates a labeled connection between two nodes in a user-curated map.
Request Body (JSON):
{
"source_id": 1,
"target_id": 2,
"label": "cites"
}Example Response (200 OK):
{
"status": "Edge created successfully",
"message": "Connection 'cites' created between nodes 1 and 2 (edge 1)"
}Each search result contains:
file_path: Full path to the filetype: File type (image,pdf,pdf_page, orunknown)tags: Array of extracted tags from the fileuser_caption: User-provided caption for the filesimilarity: Similarity score (0.0 to 1.0, higher is more similar)
For PDF pages, additional fields:
original_pdf_path: Path to the original PDF filepage_num: Page number within the PDF
# Using the startup script (recommended)
python start_api.py
# Or directly with uvicorn
python api_server.py# Run the test suite
python test_api.py
# Test specific queries
curl "http://127.0.0.1:8000/search?q=your search query"Once the server is running, visit:
- Interactive docs: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
The API is designed to work seamlessly with a desktop GUI. The frontend should:
- Make GET requests to
/searchwith the user's query - Display results in a user-friendly format
- Handle file opening when users click on results
- Show similarity scores and metadata
Example frontend integration:
async function searchFiles(query) {
const response = await fetch(
`http://127.0.0.1:8000/search?q=${encodeURIComponent(query)}&limit=10`
);
const results = await response.json();
return results;
}The API returns appropriate HTTP status codes:
200: Success422: Validation error (invalid parameters)500: Internal server error
Error responses include a detail field with the error message.