Spaces:
Runtime error
Runtime error
Update api/processors/processor_llm_base.py
Browse files
api/processors/processor_llm_base.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
"""
|
| 2 |
-
Processor Base com integração
|
| 3 |
-
|
| 4 |
"""
|
| 5 |
-
from typing import Dict, Any, Optional
|
| 6 |
from datetime import datetime
|
| 7 |
import logging
|
| 8 |
from abc import ABC, abstractmethod
|
|
@@ -12,7 +12,7 @@ logger = logging.getLogger(__name__)
|
|
| 12 |
|
| 13 |
class ProcessorLLMBase(ABC):
|
| 14 |
"""
|
| 15 |
-
Processor base que integra com LLM
|
| 16 |
|
| 17 |
Substitui hardcoded por prompts e chamadas reais.
|
| 18 |
"""
|
|
@@ -27,7 +27,7 @@ class ProcessorLLMBase(ABC):
|
|
| 27 |
Args:
|
| 28 |
specialist_id: ID do especialista (1-9)
|
| 29 |
specialist_name: Nome descritivo
|
| 30 |
-
llm_client: Cliente LLM
|
| 31 |
"""
|
| 32 |
self.specialist_id = specialist_id
|
| 33 |
self.specialist_name = specialist_name
|
|
@@ -59,18 +59,20 @@ class ProcessorLLMBase(ABC):
|
|
| 59 |
self,
|
| 60 |
prompt: str,
|
| 61 |
max_tokens: int = 2048,
|
| 62 |
-
temperature: float = 0.3
|
|
|
|
| 63 |
) -> str:
|
| 64 |
"""
|
| 65 |
-
Faz chamada ao LLM real.
|
| 66 |
|
| 67 |
Args:
|
| 68 |
prompt: Prompt a enviar
|
| 69 |
max_tokens: Máximo de tokens
|
| 70 |
temperature: Temperatura (0-1)
|
|
|
|
| 71 |
|
| 72 |
Returns:
|
| 73 |
-
Resposta do LLM
|
| 74 |
"""
|
| 75 |
if not self.llm_client:
|
| 76 |
self.add_error("LLM client não disponível")
|
|
@@ -80,30 +82,36 @@ class ProcessorLLMBase(ABC):
|
|
| 80 |
start_time = datetime.now()
|
| 81 |
|
| 82 |
logger.info(
|
| 83 |
-
f"[{self.specialist_name}] Chamando LLM... "
|
| 84 |
f"(max_tokens={max_tokens}, temp={temperature})"
|
| 85 |
)
|
| 86 |
|
| 87 |
-
# Chamada real ao LLM
|
|
|
|
| 88 |
response = self.llm_client.generate(
|
| 89 |
prompt=prompt,
|
| 90 |
-
|
| 91 |
-
temperature=temperature
|
|
|
|
| 92 |
)
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
elapsed = (datetime.now() - start_time).total_seconds()
|
| 95 |
self.execution_time += elapsed
|
| 96 |
|
| 97 |
logger.info(
|
| 98 |
-
f"[{self.specialist_name}] LLM respondeu em {elapsed:.2f}s "
|
| 99 |
-
f"({len(
|
| 100 |
)
|
| 101 |
|
| 102 |
-
return
|
| 103 |
|
| 104 |
except Exception as e:
|
| 105 |
self.add_error(f"Erro ao chamar LLM: {e}")
|
| 106 |
-
logger.error(f"[{self.specialist_name}] Erro LLM: {e}", exc_info=True)
|
| 107 |
return ""
|
| 108 |
|
| 109 |
def add_error(self, error_msg: str):
|
|
|
|
| 1 |
"""
|
| 2 |
+
Processor Base com integração LLMManager REAL
|
| 3 |
+
Classe abstrata para processors que usam LLM
|
| 4 |
"""
|
| 5 |
+
from typing import Dict, Any, Optional
|
| 6 |
from datetime import datetime
|
| 7 |
import logging
|
| 8 |
from abc import ABC, abstractmethod
|
|
|
|
| 12 |
|
| 13 |
class ProcessorLLMBase(ABC):
|
| 14 |
"""
|
| 15 |
+
Processor base que integra com LLM via client (GroqClient, etc).
|
| 16 |
|
| 17 |
Substitui hardcoded por prompts e chamadas reais.
|
| 18 |
"""
|
|
|
|
| 27 |
Args:
|
| 28 |
specialist_id: ID do especialista (1-9)
|
| 29 |
specialist_name: Nome descritivo
|
| 30 |
+
llm_client: Cliente LLM (GroqClient, OpenAIClient, etc)
|
| 31 |
"""
|
| 32 |
self.specialist_id = specialist_id
|
| 33 |
self.specialist_name = specialist_name
|
|
|
|
| 59 |
self,
|
| 60 |
prompt: str,
|
| 61 |
max_tokens: int = 2048,
|
| 62 |
+
temperature: float = 0.3,
|
| 63 |
+
model: Optional[str] = None
|
| 64 |
) -> str:
|
| 65 |
"""
|
| 66 |
+
Faz chamada ao LLM real via client.
|
| 67 |
|
| 68 |
Args:
|
| 69 |
prompt: Prompt a enviar
|
| 70 |
max_tokens: Máximo de tokens
|
| 71 |
temperature: Temperatura (0-1)
|
| 72 |
+
model: Modelo específico (opcional)
|
| 73 |
|
| 74 |
Returns:
|
| 75 |
+
Resposta do LLM (texto)
|
| 76 |
"""
|
| 77 |
if not self.llm_client:
|
| 78 |
self.add_error("LLM client não disponível")
|
|
|
|
| 82 |
start_time = datetime.now()
|
| 83 |
|
| 84 |
logger.info(
|
| 85 |
+
f"[{self.specialist_name}] 🤖 Chamando LLM... "
|
| 86 |
f"(max_tokens={max_tokens}, temp={temperature})"
|
| 87 |
)
|
| 88 |
|
| 89 |
+
# Chamada real ao LLM via client
|
| 90 |
+
# GroqClient.generate() retorna LLMResponse com .content
|
| 91 |
response = self.llm_client.generate(
|
| 92 |
prompt=prompt,
|
| 93 |
+
model=model, # Opcional, usa default se None
|
| 94 |
+
temperature=temperature,
|
| 95 |
+
max_tokens=max_tokens
|
| 96 |
)
|
| 97 |
|
| 98 |
+
# Extrair conteúdo da resposta
|
| 99 |
+
# LLMResponse tem atributo .content
|
| 100 |
+
content = response.content if hasattr(response, 'content') else str(response)
|
| 101 |
+
|
| 102 |
elapsed = (datetime.now() - start_time).total_seconds()
|
| 103 |
self.execution_time += elapsed
|
| 104 |
|
| 105 |
logger.info(
|
| 106 |
+
f"[{self.specialist_name}] ✅ LLM respondeu em {elapsed:.2f}s "
|
| 107 |
+
f"({len(content)} chars)"
|
| 108 |
)
|
| 109 |
|
| 110 |
+
return content
|
| 111 |
|
| 112 |
except Exception as e:
|
| 113 |
self.add_error(f"Erro ao chamar LLM: {e}")
|
| 114 |
+
logger.error(f"[{self.specialist_name}] ❌ Erro LLM: {e}", exc_info=True)
|
| 115 |
return ""
|
| 116 |
|
| 117 |
def add_error(self, error_msg: str):
|