Spaces:
Running
Running
Update modules/api.py
Browse files- modules/api.py +14 -7
modules/api.py
CHANGED
|
@@ -52,7 +52,7 @@ class LLMManager:
|
|
| 52 |
self.config = config_instance
|
| 53 |
self.mistral_client: Optional[Mistral] = None
|
| 54 |
self.gemini_model: Optional[genai.GenerativeModel] = None
|
| 55 |
-
self.hermes_llm = self._import_hermes()
|
| 56 |
self._setup_providers()
|
| 57 |
|
| 58 |
# PRIORIDADE: HERMES LOCAL → MISTRAL → GEMINI
|
|
@@ -71,7 +71,7 @@ class LLMManager:
|
|
| 71 |
try:
|
| 72 |
if HermesLLM.is_available():
|
| 73 |
logger.info("Hermes 7B local carregado com sucesso! (INSTÂNCIA PRONTA)")
|
| 74 |
-
return HermesLLM
|
| 75 |
else:
|
| 76 |
logger.warning("HermesLLM existe mas não está disponível")
|
| 77 |
return None
|
|
@@ -121,18 +121,25 @@ class LLMManager:
|
|
| 121 |
messages.append({"role": "user", "content": user_prompt})
|
| 122 |
|
| 123 |
for provider in self.providers:
|
| 124 |
-
# 1. HERMES LOCAL
|
| 125 |
if provider == 'hermes' and self.hermes_llm and self.hermes_llm.is_available():
|
| 126 |
try:
|
| 127 |
-
logger.info(f"[HERMES] Gerando com max_tokens={self.config.MAX_TOKENS}
|
|
|
|
| 128 |
text = self.hermes_llm.generate(
|
| 129 |
user_prompt,
|
| 130 |
-
max_tokens=self.config.MAX_TOKENS
|
| 131 |
-
temperature=self.config.TOP_P
|
| 132 |
)
|
| 133 |
if text and text.strip():
|
| 134 |
logger.info("Hermes 7B local respondeu com sucesso")
|
| 135 |
return text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
except Exception as e:
|
| 137 |
logger.warning(f"Hermes local falhou: {e}")
|
| 138 |
|
|
@@ -158,7 +165,7 @@ class LLMManager:
|
|
| 158 |
gemini_hist = []
|
| 159 |
for msg in messages[1:]:
|
| 160 |
role = "user" if msg["role"] == "user" else "model"
|
| 161 |
-
gemini_hist.append({"role": role, "parts": [{"text": msg["content"
|
| 162 |
resp = self.gemini_model.generate_content(
|
| 163 |
gemini_hist,
|
| 164 |
generation_config=genai.GenerationConfig(
|
|
|
|
| 52 |
self.config = config_instance
|
| 53 |
self.mistral_client: Optional[Mistral] = None
|
| 54 |
self.gemini_model: Optional[genai.GenerativeModel] = None
|
| 55 |
+
self.hermes_llm = self._import_hermes()
|
| 56 |
self._setup_providers()
|
| 57 |
|
| 58 |
# PRIORIDADE: HERMES LOCAL → MISTRAL → GEMINI
|
|
|
|
| 71 |
try:
|
| 72 |
if HermesLLM.is_available():
|
| 73 |
logger.info("Hermes 7B local carregado com sucesso! (INSTÂNCIA PRONTA)")
|
| 74 |
+
return HermesLLM
|
| 75 |
else:
|
| 76 |
logger.warning("HermesLLM existe mas não está disponível")
|
| 77 |
return None
|
|
|
|
| 121 |
messages.append({"role": "user", "content": user_prompt})
|
| 122 |
|
| 123 |
for provider in self.providers:
|
| 124 |
+
# 1. HERMES LOCAL (PRIORIDADE MÁXIMA)
|
| 125 |
if provider == 'hermes' and self.hermes_llm and self.hermes_llm.is_available():
|
| 126 |
try:
|
| 127 |
+
logger.info(f"[HERMES] Gerando com max_tokens={self.config.MAX_TOKENS}")
|
| 128 |
+
# llama.cpp NÃO aceita 'temperature' → só max_tokens
|
| 129 |
text = self.hermes_llm.generate(
|
| 130 |
user_prompt,
|
| 131 |
+
max_tokens=self.config.MAX_TOKENS
|
|
|
|
| 132 |
)
|
| 133 |
if text and text.strip():
|
| 134 |
logger.info("Hermes 7B local respondeu com sucesso")
|
| 135 |
return text.strip()
|
| 136 |
+
except TypeError as e:
|
| 137 |
+
if "temperature" in str(e):
|
| 138 |
+
logger.info("Tentando sem temperature...")
|
| 139 |
+
text = self.hermes_llm.generate(user_prompt, max_tokens=self.config.MAX_TOKENS)
|
| 140 |
+
if text:
|
| 141 |
+
logger.info("Hermes respondeu sem temperature")
|
| 142 |
+
return text.strip()
|
| 143 |
except Exception as e:
|
| 144 |
logger.warning(f"Hermes local falhou: {e}")
|
| 145 |
|
|
|
|
| 165 |
gemini_hist = []
|
| 166 |
for msg in messages[1:]:
|
| 167 |
role = "user" if msg["role"] == "user" else "model"
|
| 168 |
+
gemini_hist.append({"role": role, "parts": [{"text": msg["content"}]})
|
| 169 |
resp = self.gemini_model.generate_content(
|
| 170 |
gemini_hist,
|
| 171 |
generation_config=genai.GenerationConfig(
|