A web API exposing a neural network to detect duplicate entities in knowledge graphs. It uses API key authentication and rate limits requests based on client tiers (FREEMIUM, PREMIUM)
Develop an API endpoint within the FastAPI library that receives a JSON payload containing a query, processes it with the ML model, and returns the probability score based on the model’s similarity detection within a knowledge graph. The endpoint should validate input, handle exceptions, has query limit, and format the response as a JSON object with the prediction result.
User Stories
As an API Client, I want to send a JSON query to the service and receive a probability score, so I can determine if two subgraphs are similar.
As a Developer, I want the endpoint to be well-documented and robust, so it handles a variety of valid inputs and gracefully manages errors.
Details
Objective: Create an endpoint /service that takes a JSON query, invokes the ML module for similarity detection, and returns a probability score indicating the likelihood of similarity between subgraphs.
Requirements:
Endpoint: Define a POST endpoint /service under src/app/api/routers/service.py.
Input Validation: Ensure the JSON payload includes required fields for the ML model (e.g., subgraph1 and subgraph2 data).
Output: Return a JSON response with the probability score, formatted as:
{
"probability": <float>
}
Error Handling:
If input validation fails, return a 400 Bad Request with a relevant error message.
If the ML module fails, return a 500 Internal Server Error.
If the query limit has been reached, return a 429 To Many Request
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from src.ml.{{ module_name }}.modeling.predict import predict_similarity
router = APIRouter()
class SimilarityRequest(BaseModel):
subgraph: list(dict)
@router.post("/service")
async def predict_similarity_endpoint(request: SimilarityRequest):
try:
# Logic for valid API key and rate limit
# Invoke ML model prediction
probability = predict_similarity(request.subgraph)
# Return JSON response with the probability score
return {
"probability": probability,
}
except ValueError as e:
# Handle invalid input
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
# Handle general errors
raise HTTPException(status_code=500, detail="Internal server error.")
Edge Cases
Invalid JSON Format: Return a 400 Bad Request if the JSON payload is malformed.
Missing Required Fields: Return a 400 Bad Request if subgraph is missing.
ML Model Errors: Return a 500 Internal Server Error if there’s an issue with the ML model, such as a missing model file or processing error.
Description
Develop an API endpoint within the
FastAPI
library that receives a JSON payload containing a query, processes it with the ML model, and returns the probability score based on the model’s similarity detection within a knowledge graph. The endpoint should validate input, handle exceptions, has query limit, and format the response as a JSON object with the prediction result.User Stories
Details
/service
that takes a JSON query, invokes the ML module for similarity detection, and returns a probability score indicating the likelihood of similarity between subgraphs./service
undersrc/app/api/routers/service.py
.subgraph1
andsubgraph2
data).400 Bad Request
with a relevant error message.500 Internal Server Error
.429 To Many Request
Example Usage and Response
Request (JSON):
Response (JSON):
Error Response (JSON):
Mockup of Endpoint Implementation
Edge Cases
400 Bad Request
if the JSON payload is malformed.400 Bad Request
ifsubgraph
is missing.500 Internal Server Error
if there’s an issue with the ML model, such as a missing model file or processing error.