File size: 5,027 Bytes
cf52a55 | 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | """
API REST do Modelo Híbrido de LLM.
==================================
FastAPI expondo /process, /chat e /agent. Usa config, pipeline e chat_session.
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
from typing import Any, Dict, Optional
from uuid import uuid4
# Raiz do projeto
ROOT = Path(__file__).resolve().parent
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
try:
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
except ImportError:
FastAPI = None # type: ignore
HTTPException = None # type: ignore
BaseModel = object # type: ignore
# -----------------------------------------------------------------------------
# Modelos de request/response
# -----------------------------------------------------------------------------
class ProcessRequest(BaseModel):
prompt: str
session_id: Optional[str] = None
use_agent: Optional[bool] = None
skip_l5: bool = False
class ChatRequest(BaseModel):
message: str
session_id: Optional[str] = None
class ProcessResponse(BaseModel):
response: str
truth_value: float
state: str
certainty: float
contradiction: float
confidence_label: str
session_id: Optional[str] = None
# -----------------------------------------------------------------------------
# Estado global (sessões de chat, pipeline, config)
# -----------------------------------------------------------------------------
def _load_app_state():
from config_loader import load_config
from pipeline import HybridLLMPipeline
from chat_session import ChatSession
config = load_config()
pipeline = HybridLLMPipeline(config=config, verbose=False)
sessions: Dict[str, ChatSession] = {}
max_turns = config.get("chat", {}).get("max_turns_in_context", 10)
return config, pipeline, sessions, max_turns
if FastAPI is None:
app = None
else:
app = FastAPI(title="Modelo Híbrido de LLM", version="1.0")
_config, _pipeline, _sessions, _max_turns = _load_app_state()
@app.get("/health")
def health():
return {"status": "ok", "model": "hybrid_llm"}
@app.post("/process", response_model=ProcessResponse)
def process(req: ProcessRequest):
session_id = req.session_id or str(uuid4())
session = _sessions.get(session_id)
if session:
session.add_user(req.prompt)
try:
result = _pipeline.process(
req.prompt,
chat_session=session,
use_agent=req.use_agent,
skip_l5=req.skip_l5,
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if session:
session.add_assistant(result.response)
return ProcessResponse(
response=result.response,
truth_value=result.truth_value,
state=result.state,
certainty=result.certainty,
contradiction=result.contradiction,
confidence_label=result.confidence_label,
session_id=session_id,
)
@app.post("/chat", response_model=ProcessResponse)
def chat(req: ChatRequest):
session_id = req.session_id or str(uuid4())
if session_id not in _sessions:
from chat_session import ChatSession
_sessions[session_id] = ChatSession(max_turns=_max_turns)
session = _sessions[session_id]
session.add_user(req.message)
try:
result = _pipeline.process(req.message, chat_session=session, use_agent=None, skip_l5=False)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
session.add_assistant(result.response)
return ProcessResponse(
response=result.response,
truth_value=result.truth_value,
state=result.state,
certainty=result.certainty,
contradiction=result.contradiction,
confidence_label=result.confidence_label,
session_id=session_id,
)
class AgentRequest(BaseModel):
query: str
@app.post("/agent")
def agent_search(req: AgentRequest):
"""Chama apenas o agente de pesquisa (busca local + internet)."""
try:
from agente_busca_web import run_search_for_context
text = run_search_for_context(req.query)
return {"answer": text, "query": req.query}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
def run_api():
if app is None:
print("Instale fastapi e uvicorn: pip install fastapi uvicorn", file=sys.stderr)
sys.exit(1)
import uvicorn
from config_loader import load_config
cfg = load_config()
api_cfg = cfg.get("api", {})
host = api_cfg.get("host", "0.0.0.0")
port = int(api_cfg.get("port", 8000))
uvicorn.run("api:app", host=host, port=port, reload=False)
if __name__ == "__main__":
run_api()
|