File size: 3,202 Bytes
c6c178c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
FastAPI REST API and Interactive Dashboard for Cognitive Agent Evaluator.
"""

from typing import List, Dict, Any
from fastapi import FastAPI, HTTPException, status
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware

from src.schemas import EvaluationRequest, EvaluationResult
from src.agent import CognitiveEvaluationAgent
from src.biases import BIAS_DEFINITIONS
from src.ui import HTML_DASHBOARD

agent = CognitiveEvaluationAgent()

app = FastAPI(
    title="Cognitive Agent Evaluator & Behavioral Telemetry API",
    description="Autonomous Agent for evaluating cognitive biases, emotional valence, and psychometric alignment in human & LLM reasoning. Engineered by Fabio Torres (neurodeveloper11).",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.get("/", response_class=HTMLResponse, tags=["Dashboard UI"])
async def root_ui():
    """
    Serves the modern, intuitive interactive dashboard for testing the agent live.
    """
    return HTMLResponse(content=HTML_DASHBOARD)


@app.get("/api/v1/info", tags=["General"])
async def api_info():
    """
    Returns JSON metadata about the project and endpoints.
    """
    return {
        "project": "Cognitive Agent Evaluator & Behavioral Telemetry Engine",
        "author": "Fabio Torres (neurodeveloper11)",
        "docs": "/docs",
        "health": "/health",
        "status": "operational"
    }


@app.get("/health", tags=["Monitoring"])
async def health_check():
    return {
        "status": "healthy",
        "agent": agent.agent_name,
        "supported_biases": list(BIAS_DEFINITIONS.keys())
    }


@app.post(
    "/api/v1/evaluate",
    response_model=EvaluationResult,
    status_code=status.HTTP_200_OK,
    tags=["Agent Evaluation"]
)
async def evaluate_text(payload: EvaluationRequest):
    """
    Submits a text trace or LLM output for cognitive bias detection,
    psychometric telemetry scoring, and alignment mitigation.
    """
    try:
        return await agent.evaluate(payload)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.get("/api/v1/taxonomy/biases", tags=["Taxonomy"])
async def get_bias_taxonomy() -> Dict[str, Any]:
    """
    Returns the supported cognitive bias taxonomy, heuristics, and severity levels.
    """
    return {
        bias: {
            "severity": meta["severity"],
            "explanation": meta["explanation"]
        }
        for bias, meta in BIAS_DEFINITIONS.items()
    }


@app.post(
    "/api/v1/benchmark/batch-eval",
    response_model=List[EvaluationResult],
    tags=["Agent Evaluation"]
)
async def batch_evaluate(requests: List[EvaluationRequest]):
    """
    Batch processes multiple text traces for benchmark evaluation.
    """
    if len(requests) > 50:
        raise HTTPException(status_code=400, detail="Maximum batch size is 50 requests.")

    results: List[EvaluationResult] = []
    for req in requests:
        res = await agent.evaluate(req)
        results.append(res)
    return results