Create api/router_hallucination.py
Browse files
api/router_hallucination.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
|
|
| 1 |
from fastapi import APIRouter, Depends
|
| 2 |
import numpy as np
|
| 3 |
-
from typing import List
|
| 4 |
from consensus.topo import TopologicalConsciousness
|
| 5 |
-
from .schemas import HallucinationRequest, HallucinationResponse
|
| 6 |
from adapters.base import BaseModelAdapter
|
| 7 |
|
| 8 |
router = APIRouter(prefix="/hallucination", tags=["safety"])
|
|
@@ -13,11 +13,9 @@ def get_model() -> BaseModelAdapter:
|
|
| 13 |
|
| 14 |
@router.post("/check", response_model=HallucinationResponse)
|
| 15 |
def check(req: HallucinationRequest, model: BaseModelAdapter = Depends(get_model)):
|
| 16 |
-
# Hidden-state proxy: embed prompt and output, stack
|
| 17 |
out = model.generate(req.prompt)
|
| 18 |
hid = np.array([model.embed_text(req.prompt), model.embed_text(out)], dtype=float)
|
| 19 |
tc = TopologicalConsciousness(n_anyons=1, central_charge=627)
|
| 20 |
energy = tc.cardy_boundary_energy(hid)
|
| 21 |
-
# Normalize with a heuristic scale
|
| 22 |
risk = float(energy / (energy + 1.0))
|
| 23 |
return HallucinationResponse(energy=energy, hallucination_risk=risk, grounded=(risk < 0.3))
|
|
|
|
| 1 |
+
# api/router_hallucination.py
|
| 2 |
from fastapi import APIRouter, Depends
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from consensus.topo import TopologicalConsciousness
|
| 5 |
+
from api.schemas import HallucinationRequest, HallucinationResponse
|
| 6 |
from adapters.base import BaseModelAdapter
|
| 7 |
|
| 8 |
router = APIRouter(prefix="/hallucination", tags=["safety"])
|
|
|
|
| 13 |
|
| 14 |
@router.post("/check", response_model=HallucinationResponse)
|
| 15 |
def check(req: HallucinationRequest, model: BaseModelAdapter = Depends(get_model)):
|
|
|
|
| 16 |
out = model.generate(req.prompt)
|
| 17 |
hid = np.array([model.embed_text(req.prompt), model.embed_text(out)], dtype=float)
|
| 18 |
tc = TopologicalConsciousness(n_anyons=1, central_charge=627)
|
| 19 |
energy = tc.cardy_boundary_energy(hid)
|
|
|
|
| 20 |
risk = float(energy / (energy + 1.0))
|
| 21 |
return HallucinationResponse(energy=energy, hallucination_risk=risk, grounded=(risk < 0.3))
|