GabrielEValenzuela / chatML

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)
MIT License
0 stars 0 forks source link

Create `service` endpoint #7

Open GabrielEValenzuela opened 4 weeks ago

GabrielEValenzuela commented 4 weeks ago

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


Example Usage and Response


Mockup of Endpoint Implementation

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