Upload 2 files
Browse files- app.py +33 -28
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -103,8 +103,8 @@ from langdetect import detect, LangDetectException
|
|
| 103 |
from prometheus_fastapi_instrumentator import Instrumentator
|
| 104 |
from prometheus_client import Histogram
|
| 105 |
|
| 106 |
-
# A Conexão com o Oráculo
|
| 107 |
-
from
|
| 108 |
|
| 109 |
# ==============================================================================
|
| 110 |
# CONFIGURAÇÕES GERAIS E LOGGING
|
|
@@ -123,25 +123,30 @@ UMAP_N_NEIGHBORS = 30
|
|
| 123 |
cache: Dict[str, Any] = {}
|
| 124 |
|
| 125 |
# Definição de Métricas Customizadas do Prometheus
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
"
|
| 129 |
-
"Tempo de resposta da API externa Groq (LLM Generation)",
|
| 130 |
buckets=[0.1, 0.5, 1.0, 2.0, 5.0, 10.0, 20.0]
|
| 131 |
)
|
| 132 |
|
| 133 |
-
#
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
try:
|
| 136 |
-
if not
|
| 137 |
-
logging.warning("
|
| 138 |
-
|
| 139 |
else:
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
| 142 |
except Exception as e:
|
| 143 |
-
logging.error(f"FALHA AO INICIALIZAR
|
| 144 |
-
|
| 145 |
|
| 146 |
# Inicialização do Cliente Tavily (Web Search)
|
| 147 |
TAVILY_API_KEY = os.environ.get("TAVILY_API_KEY")
|
|
@@ -935,9 +940,9 @@ async def search_api(query: str = Form(...), job_id: str = Form(...)):
|
|
| 935 |
"citation_id": rank + 1
|
| 936 |
})
|
| 937 |
|
| 938 |
-
# FASE 3: Geração (
|
| 939 |
summary = ""
|
| 940 |
-
if
|
| 941 |
context_str = "\n".join(context_parts)
|
| 942 |
rag_prompt = (
|
| 943 |
"INSTRUÇÃO DE SISTEMA:\n"
|
|
@@ -954,18 +959,18 @@ async def search_api(query: str = Form(...), job_id: str = Form(...)):
|
|
| 954 |
|
| 955 |
try:
|
| 956 |
# --- INÍCIO DA MEDIÇÃO DA API EXTERNA ---
|
| 957 |
-
|
| 958 |
|
| 959 |
-
chat_completion =
|
| 960 |
messages=[{"role": "user", "content": rag_prompt}],
|
| 961 |
-
model=
|
| 962 |
temperature=0.1,
|
| 963 |
max_tokens=1024
|
| 964 |
)
|
| 965 |
|
| 966 |
# Registra o tempo gasto apenas na chamada da API
|
| 967 |
-
duration = time.time() -
|
| 968 |
-
|
| 969 |
# --- FIM DA MEDIÇÃO ---
|
| 970 |
|
| 971 |
summary = chat_completion.choices[0].message.content.strip()
|
|
@@ -983,7 +988,7 @@ async def search_api(query: str = Form(...), job_id: str = Form(...)):
|
|
| 983 |
@app.post("/describe_clusters/")
|
| 984 |
async def describe_clusters_api(job_id: str = Form(...)):
|
| 985 |
logging.info(f"Descrevendo clusters para Job: {job_id}")
|
| 986 |
-
if not
|
| 987 |
if job_id not in cache: raise HTTPException(status_code=404, detail="Job não encontrado.")
|
| 988 |
|
| 989 |
try:
|
|
@@ -1018,17 +1023,17 @@ async def describe_clusters_api(job_id: str = Form(...)):
|
|
| 1018 |
)
|
| 1019 |
|
| 1020 |
# --- INÍCIO DA MEDIÇÃO DA API EXTERNA ---
|
| 1021 |
-
|
| 1022 |
|
| 1023 |
-
chat_completion =
|
| 1024 |
messages=[
|
| 1025 |
{"role": "system", "content": "JSON Output Only."},
|
| 1026 |
{"role": "user", "content": master_prompt},
|
| 1027 |
-
], model=
|
| 1028 |
)
|
| 1029 |
|
| 1030 |
-
duration = time.time() -
|
| 1031 |
-
|
| 1032 |
# --- FIM DA MEDIÇÃO ---
|
| 1033 |
|
| 1034 |
response_content = chat_completion.choices[0].message.content
|
|
|
|
| 103 |
from prometheus_fastapi_instrumentator import Instrumentator
|
| 104 |
from prometheus_client import Histogram
|
| 105 |
|
| 106 |
+
# A Conexão com o Oráculo (OpenRouter - OpenAI Compatible)
|
| 107 |
+
from openai import OpenAI
|
| 108 |
|
| 109 |
# ==============================================================================
|
| 110 |
# CONFIGURAÇÕES GERAIS E LOGGING
|
|
|
|
| 123 |
cache: Dict[str, Any] = {}
|
| 124 |
|
| 125 |
# Definição de Métricas Customizadas do Prometheus
|
| 126 |
+
LLM_LATENCY = Histogram(
|
| 127 |
+
"llm_api_latency_seconds",
|
| 128 |
+
"Tempo de resposta da API externa LLM (OpenRouter)",
|
|
|
|
| 129 |
buckets=[0.1, 0.5, 1.0, 2.0, 5.0, 10.0, 20.0]
|
| 130 |
)
|
| 131 |
|
| 132 |
+
# Modelo LLM (OpenRouter)
|
| 133 |
+
LLM_MODEL = os.environ.get("LLM_MODEL", "google/gemini-2.0-flash-exp:free")
|
| 134 |
+
|
| 135 |
+
# Inicialização do Cliente OpenRouter (OpenAI Compatible)
|
| 136 |
+
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
| 137 |
try:
|
| 138 |
+
if not OPENROUTER_API_KEY:
|
| 139 |
+
logging.warning("OPENROUTER_API_KEY não encontrada. Funcionalidades de LLM estarão indisponíveis.")
|
| 140 |
+
llm_client = None
|
| 141 |
else:
|
| 142 |
+
llm_client = OpenAI(
|
| 143 |
+
base_url="https://openrouter.ai/api/v1",
|
| 144 |
+
api_key=OPENROUTER_API_KEY
|
| 145 |
+
)
|
| 146 |
+
logging.info(f"Cliente OpenRouter inicializado com modelo: {LLM_MODEL}")
|
| 147 |
except Exception as e:
|
| 148 |
+
logging.error(f"FALHA AO INICIALIZAR OPENROUTER: {e}")
|
| 149 |
+
llm_client = None
|
| 150 |
|
| 151 |
# Inicialização do Cliente Tavily (Web Search)
|
| 152 |
TAVILY_API_KEY = os.environ.get("TAVILY_API_KEY")
|
|
|
|
| 940 |
"citation_id": rank + 1
|
| 941 |
})
|
| 942 |
|
| 943 |
+
# FASE 3: Geração (OpenRouter) com TELEMETRIA
|
| 944 |
summary = ""
|
| 945 |
+
if llm_client:
|
| 946 |
context_str = "\n".join(context_parts)
|
| 947 |
rag_prompt = (
|
| 948 |
"INSTRUÇÃO DE SISTEMA:\n"
|
|
|
|
| 959 |
|
| 960 |
try:
|
| 961 |
# --- INÍCIO DA MEDIÇÃO DA API EXTERNA ---
|
| 962 |
+
start_time_llm = time.time()
|
| 963 |
|
| 964 |
+
chat_completion = llm_client.chat.completions.create(
|
| 965 |
messages=[{"role": "user", "content": rag_prompt}],
|
| 966 |
+
model=LLM_MODEL,
|
| 967 |
temperature=0.1,
|
| 968 |
max_tokens=1024
|
| 969 |
)
|
| 970 |
|
| 971 |
# Registra o tempo gasto apenas na chamada da API
|
| 972 |
+
duration = time.time() - start_time_llm
|
| 973 |
+
LLM_LATENCY.observe(duration)
|
| 974 |
# --- FIM DA MEDIÇÃO ---
|
| 975 |
|
| 976 |
summary = chat_completion.choices[0].message.content.strip()
|
|
|
|
| 988 |
@app.post("/describe_clusters/")
|
| 989 |
async def describe_clusters_api(job_id: str = Form(...)):
|
| 990 |
logging.info(f"Descrevendo clusters para Job: {job_id}")
|
| 991 |
+
if not llm_client: raise HTTPException(status_code=503, detail="LLM indisponível.")
|
| 992 |
if job_id not in cache: raise HTTPException(status_code=404, detail="Job não encontrado.")
|
| 993 |
|
| 994 |
try:
|
|
|
|
| 1023 |
)
|
| 1024 |
|
| 1025 |
# --- INÍCIO DA MEDIÇÃO DA API EXTERNA ---
|
| 1026 |
+
start_time_llm = time.time()
|
| 1027 |
|
| 1028 |
+
chat_completion = llm_client.chat.completions.create(
|
| 1029 |
messages=[
|
| 1030 |
{"role": "system", "content": "JSON Output Only."},
|
| 1031 |
{"role": "user", "content": master_prompt},
|
| 1032 |
+
], model=LLM_MODEL, temperature=0.2,
|
| 1033 |
)
|
| 1034 |
|
| 1035 |
+
duration = time.time() - start_time_llm
|
| 1036 |
+
LLM_LATENCY.observe(duration)
|
| 1037 |
# --- FIM DA MEDIÇÃO ---
|
| 1038 |
|
| 1039 |
response_content = chat_completion.choices[0].message.content
|
requirements.txt
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
fastapi
|
| 3 |
uvicorn[standard]
|
| 4 |
python-multipart
|
| 5 |
-
|
| 6 |
prometheus-fastapi-instrumentator
|
| 7 |
prometheus-client
|
| 8 |
tavily-python
|
|
|
|
| 2 |
fastapi
|
| 3 |
uvicorn[standard]
|
| 4 |
python-multipart
|
| 5 |
+
openai
|
| 6 |
prometheus-fastapi-instrumentator
|
| 7 |
prometheus-client
|
| 8 |
tavily-python
|