Update app.py
Browse files
app.py
CHANGED
|
@@ -136,10 +136,9 @@ def call_model_with_messages(client: InferenceClient, messages: List[Dict[str, s
|
|
| 136 |
|
| 137 |
Estratégia (ordem):
|
| 138 |
1) client.completions.create(messages=...)
|
| 139 |
-
2) client.
|
| 140 |
-
3) client.
|
| 141 |
-
4)
|
| 142 |
-
5) tentar chamar diretamente funções encontradas que contenham 'create'/'generate'/'complet'
|
| 143 |
|
| 144 |
Retorna o objeto cru retornado pela biblioteca ou lança RuntimeError com info de debug.
|
| 145 |
"""
|
|
@@ -156,22 +155,43 @@ def call_model_with_messages(client: InferenceClient, messages: List[Dict[str, s
|
|
| 156 |
except Exception as e:
|
| 157 |
logger.debug("completions.create falhou: %s", e)
|
| 158 |
|
| 159 |
-
# 2)
|
| 160 |
-
|
|
|
|
| 161 |
try:
|
| 162 |
-
|
| 163 |
-
if
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
| 171 |
except Exception as e:
|
| 172 |
-
logger.debug("%s falhou: %s",
|
| 173 |
|
| 174 |
-
# 3) tentar
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
prompt = _messages_to_prompt(messages)
|
| 176 |
try:
|
| 177 |
if hasattr(client, "text_generation"):
|
|
@@ -184,7 +204,7 @@ def call_model_with_messages(client: InferenceClient, messages: List[Dict[str, s
|
|
| 184 |
except Exception as e:
|
| 185 |
logger.debug("text_generation/generate falhou: %s", e)
|
| 186 |
|
| 187 |
-
#
|
| 188 |
candidate_methods = [m for m in dir(client) if any(k in m for k in ("create", "generate", "complete", "run"))]
|
| 189 |
for name in candidate_methods:
|
| 190 |
try:
|
|
|
|
| 136 |
|
| 137 |
Estratégia (ordem):
|
| 138 |
1) client.completions.create(messages=...)
|
| 139 |
+
2) client.chat.create / client.chat(...) / client.chat_completion.create / client.chat_completion(...)
|
| 140 |
+
3) client.text_generation(prompt=...)
|
| 141 |
+
4) tentar chamar diretamente funções encontradas que contenham 'create'/'generate'/'complet'
|
|
|
|
| 142 |
|
| 143 |
Retorna o objeto cru retornado pela biblioteca ou lança RuntimeError com info de debug.
|
| 144 |
"""
|
|
|
|
| 155 |
except Exception as e:
|
| 156 |
logger.debug("completions.create falhou: %s", e)
|
| 157 |
|
| 158 |
+
# 2) tentar chat / chat_completion namespaces (há no runtime mostrado)
|
| 159 |
+
# suporte: client.chat.create, client.chat(...), client.chat_completion.create, client.chat_completion(...)
|
| 160 |
+
for chat_ns in ("chat", "chat_completion", "chat_completions"):
|
| 161 |
try:
|
| 162 |
+
ns = getattr(client, chat_ns, None)
|
| 163 |
+
if ns is None:
|
| 164 |
+
continue
|
| 165 |
+
# ns pode ser um objeto com .create ou chamável diretamente
|
| 166 |
+
if hasattr(ns, "create"):
|
| 167 |
+
logger.info(f"Chamando {chat_ns}.create(messages=...)")
|
| 168 |
+
return ns.create(messages=messages, max_new_tokens=max_new_tokens, temperature=temperature)
|
| 169 |
+
if callable(ns):
|
| 170 |
+
logger.info(f"Chamando {chat_ns}(messages=...)")
|
| 171 |
+
return ns(messages=messages, max_new_tokens=max_new_tokens, temperature=temperature)
|
| 172 |
except Exception as e:
|
| 173 |
+
logger.debug("%s falhou: %s", chat_ns, e)
|
| 174 |
|
| 175 |
+
# 3) tentar diretamente client.chat (que pelo debug pode existir como atributo com métodos internos)
|
| 176 |
+
try:
|
| 177 |
+
if hasattr(client, "chat"):
|
| 178 |
+
chat_obj = getattr(client, "chat")
|
| 179 |
+
# se chat_obj tem create
|
| 180 |
+
if hasattr(chat_obj, "create"):
|
| 181 |
+
logger.info("Chamando client.chat.create(messages=...)")
|
| 182 |
+
return chat_obj.create(messages=messages, max_new_tokens=max_new_tokens, temperature=temperature)
|
| 183 |
+
# se chat_obj tem chat_completion
|
| 184 |
+
if hasattr(chat_obj, "chat_completion") and hasattr(chat_obj.chat_completion, "create"):
|
| 185 |
+
logger.info("Chamando client.chat.chat_completion.create(messages=...)")
|
| 186 |
+
return chat_obj.chat_completion.create(messages=messages, max_new_tokens=max_new_tokens, temperature=temperature)
|
| 187 |
+
# se chat_obj é chamável
|
| 188 |
+
if callable(chat_obj):
|
| 189 |
+
logger.info("Chamando client.chat(messages=...)")
|
| 190 |
+
return chat_obj(messages=messages, max_new_tokens=max_new_tokens, temperature=temperature)
|
| 191 |
+
except Exception as e:
|
| 192 |
+
logger.debug("client.chat path falhou: %s", e)
|
| 193 |
+
|
| 194 |
+
# 4) gerar prompt concatenado e usar text_generation
|
| 195 |
prompt = _messages_to_prompt(messages)
|
| 196 |
try:
|
| 197 |
if hasattr(client, "text_generation"):
|
|
|
|
| 204 |
except Exception as e:
|
| 205 |
logger.debug("text_generation/generate falhou: %s", e)
|
| 206 |
|
| 207 |
+
# 5) última tentativa: procurar métodos nomeados úteis
|
| 208 |
candidate_methods = [m for m in dir(client) if any(k in m for k in ("create", "generate", "complete", "run"))]
|
| 209 |
for name in candidate_methods:
|
| 210 |
try:
|