Skip to content
79 changes: 74 additions & 5 deletions api/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from contextlib import asynccontextmanager
import os
import uuid

from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.middleware.base import BaseHTTPMiddleware

from fastapi import FastAPI
from api.routes import templates, forms
from api.db.init_db import init_db
from api.errors.handlers import register_exception_handlers
from fastapi.middleware.cors import CORSMiddleware
from api.routes import forms, templates

@asynccontextmanager
async def lifespan(app: FastAPI):
Expand All @@ -18,7 +23,17 @@ async def lifespan(app: FastAPI):

app = FastAPI(lifespan=lifespan)

register_exception_handlers(app)

class RequestIDMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
request_id = str(uuid.uuid4())
request.state.request_id = request_id
response = await call_next(request)
response.headers["X-Request-ID"] = request_id
return response


app.add_middleware(RequestIDMiddleware)

default_origins = "http://127.0.0.1:5173"
allowed_origins = [
Expand All @@ -35,5 +50,59 @@ async def lifespan(app: FastAPI):
allow_headers=["*"],
)

@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
return JSONResponse(
status_code=exc.status_code,
content={
"error": {
"type": "HTTPException",
"message": exc.detail,
"details": {}
}
},
)


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
formatted_errors = []

for err in exc.errors():
loc = err.get("loc", [])
field = loc[-1] if loc else "unknown"
issue = err.get("msg", "Invalid value")
expected = err.get("type", "")

formatted_errors.append({
"field": field,
"issue": issue,
"expected": expected
})

return JSONResponse(
status_code=422,
content={
"error": {
"type": "ValidationError",
"message": "Invalid request data",
"details": formatted_errors,
}
},
)

@app.exception_handler(Exception)
async def general_exception_handler(request: Request, exc: Exception):
return JSONResponse(
status_code=500,
content={
"error": {
"type": "InternalServerError",
"message": str(exc),
"details": {}
}
},
)

app.include_router(templates.router)
app.include_router(forms.router)
app.include_router(forms.router)
8 changes: 7 additions & 1 deletion api/schemas/forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from pydantic import BaseModel
from pydantic import BaseModel, field_validator

class FormFill(BaseModel):
template_id: int
input_text: str

@field_validator("input_text")
def validate_input_text(cls, value):
if not value or not value.strip():
raise ValueError("Input text cannot be empty")
return value


class FormFillResponse(BaseModel):
id: int
Expand Down
40 changes: 40 additions & 0 deletions api/services/prompt_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def build_extraction_prompt(input_text: str) -> str:
return f"""
You are an AI system that extracts structured information from incident reports.

Extract the following fields:
- name
- location
- date (YYYY-MM-DD if possible)
- incident_type
- description

Return ONLY valid JSON. Do not include any extra text.

Format:
{{
"name": "",
"location": "",
"date": "",
"incident_type": "",
"description": ""
}}

Example:

Input:
Fire reported near Central Park on Jan 5 involving a vehicle.

Output:
{{
"name": "",
"location": "Central Park",
"date": "2024-01-05",
"incident_type": "fire",
"description": "Fire involving a vehicle"
}}

Now extract from:

{input_text}
"""
14 changes: 13 additions & 1 deletion src/llm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import requests
from api.services.prompt_builder import build_extraction_prompt
from requests.exceptions import Timeout, RequestException


Expand Down Expand Up @@ -58,10 +59,21 @@ def main_loop(self):
ollama_host = os.getenv("OLLAMA_HOST", "http://localhost:11434").rstrip("/")
ollama_url = f"{ollama_host}/api/generate"

base_prompt = build_extraction_prompt(self._transcript_text)

prompt = f"""
{base_prompt}

Focus specifically on extracting the value for this field:
{field}

Return only the extracted value as a plain string. Do not return JSON.
"""

payload = {
"model": "mistral",
"prompt": prompt,
"stream": False, # don't really know why --> look into this later.
"stream": False, # streaming disabled; using single response mode
}

json_data = None
Expand Down