from fastapi import FastAPI, HTTPException from pydantic import BaseModel import uvicorn from typing import Optional import logging # Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Initialize FastAPI app = FastAPI( title="OpenHealth Agent API", description="Medical Literature Assistant Backend", version="0.1.0" ) # Pydantic models class QueryRequest(BaseModel): question: str max_papers: Optional[int] = 10 min_year: Optional[int] = 2019 class HealthResponse(BaseModel): status: str timestamp: str version: str # Root endpoint @app.get("/") async def root(): return { "message": "OpenHealth Agent API", "endpoints": { "health": "/health", "search": "/search", "analyze": "/analyze" } } # Health check @app.get("/health", response_model=HealthResponse) async def health_check(): import datetime return { "status": "healthy", "timestamp": datetime.datetime.now().isoformat(), "version": "0.1.0" } # Search endpoint (placeholder) @app.post("/search") async def search_papers(request: QueryRequest): logger.info(f"Received query: {request.question}") # For now, return mock data return { "status": "success", "query": request.question, "results": [ { "title": "Sample Study on Diabetes Treatment", "authors": ["Smith J", "Johnson A"], "year": 2023, "journal": "New England Journal of Medicine", "abstract": "A randomized controlled trial comparing SGLT2 inhibitors to standard care." } ], "count": 1 } # Run the app if __name__ == "__main__": uvicorn.run( "api:app", host="0.0.0.0", port=8000, reload=True # Auto-reload on changes )