Spaces:
Running
Running
Añade funciones de previsualización de texto y manejo de historial en `app.py`. Se implementan las funciones `_preview_text` y `_history_preview` para limitar la longitud del texto mostrado y mejorar la visualización del historial de interacciones. Además, se integra la instrumentación opcional de RunTree para el seguimiento de sesiones de chat, permitiendo registrar la cantidad de mensajes y manejar errores de manera más efectiva.
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import gradio as gr
|
|
| 5 |
from openai import OpenAI
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from langsmith import traceable
|
|
|
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
|
|
@@ -91,6 +92,28 @@ def _extract_text_and_files(message):
|
|
| 91 |
return text_combined, files
|
| 92 |
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
@traceable
|
| 95 |
def respond(message, history: list[tuple[str, str]]):
|
| 96 |
"""Stream assistant reply via Gemini using OpenAI-compatible API.
|
|
@@ -153,7 +176,59 @@ def respond(message, history: list[tuple[str, str]]):
|
|
| 153 |
|
| 154 |
messages.append({"role": "user", "content": user_content})
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
stream = _client.chat.completions.create(
|
| 158 |
model=GEMINI_MODEL,
|
| 159 |
messages=messages,
|
|
@@ -180,7 +255,32 @@ def respond(message, history: list[tuple[str, str]]):
|
|
| 180 |
|
| 181 |
if not accumulated:
|
| 182 |
yield "(Sin contenido de respuesta)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
yield f"Ocurrió un error al llamar a Gemini: {e}"
|
| 185 |
|
| 186 |
|
|
|
|
| 5 |
from openai import OpenAI
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from langsmith import traceable
|
| 8 |
+
from langsmith.run_trees import RunTree
|
| 9 |
|
| 10 |
load_dotenv()
|
| 11 |
|
|
|
|
| 92 |
return text_combined, files
|
| 93 |
|
| 94 |
|
| 95 |
+
def _preview_text(text: str | None, limit: int = 600) -> str:
|
| 96 |
+
if not text:
|
| 97 |
+
return ""
|
| 98 |
+
if len(text) <= limit:
|
| 99 |
+
return text
|
| 100 |
+
return text[:limit] + "…"
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def _history_preview(history: list[tuple[str, str]] | None, max_turns: int = 3, max_chars: int = 1200) -> str:
|
| 104 |
+
if not history:
|
| 105 |
+
return ""
|
| 106 |
+
tail = history[-max_turns:]
|
| 107 |
+
parts: list[str] = []
|
| 108 |
+
for user_turn, assistant_turn in tail:
|
| 109 |
+
if user_turn:
|
| 110 |
+
parts.append(f"U: {_preview_text(user_turn, 300)}")
|
| 111 |
+
if assistant_turn:
|
| 112 |
+
parts.append(f"A: {_preview_text(assistant_turn, 300)}")
|
| 113 |
+
joined = "\n".join(parts)
|
| 114 |
+
return _preview_text(joined, max_chars)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
@traceable
|
| 118 |
def respond(message, history: list[tuple[str, str]]):
|
| 119 |
"""Stream assistant reply via Gemini using OpenAI-compatible API.
|
|
|
|
| 176 |
|
| 177 |
messages.append({"role": "user", "content": user_content})
|
| 178 |
|
| 179 |
+
# Optional RunTree instrumentation (does not require LANGSMITH_TRACING)
|
| 180 |
+
_ls_api_key = os.getenv("LANGSMITH_API_KEY")
|
| 181 |
+
pipeline = None
|
| 182 |
+
child_build = None
|
| 183 |
+
child_llm = None
|
| 184 |
+
if _ls_api_key:
|
| 185 |
+
try:
|
| 186 |
+
pipeline = RunTree(
|
| 187 |
+
name="Chat Session",
|
| 188 |
+
run_type="chain",
|
| 189 |
+
inputs={
|
| 190 |
+
"user_text": _preview_text(user_text, 600),
|
| 191 |
+
"has_images": bool(image_parts),
|
| 192 |
+
"history_preview": _history_preview(history),
|
| 193 |
+
},
|
| 194 |
+
)
|
| 195 |
+
pipeline.post()
|
| 196 |
+
|
| 197 |
+
child_build = pipeline.create_child(
|
| 198 |
+
name="BuildMessages",
|
| 199 |
+
run_type="chain",
|
| 200 |
+
inputs={
|
| 201 |
+
"system_prompt_preview": _preview_text(system_prompt, 400),
|
| 202 |
+
"user_content_type": "multimodal" if image_parts else "text",
|
| 203 |
+
"history_turns": len(history or []),
|
| 204 |
+
},
|
| 205 |
+
)
|
| 206 |
+
child_build.post()
|
| 207 |
+
child_build.end(
|
| 208 |
+
outputs={
|
| 209 |
+
"messages_count": len(messages),
|
| 210 |
+
}
|
| 211 |
+
)
|
| 212 |
+
child_build.patch()
|
| 213 |
+
except Exception:
|
| 214 |
+
pipeline = None
|
| 215 |
+
|
| 216 |
try:
|
| 217 |
+
if pipeline:
|
| 218 |
+
try:
|
| 219 |
+
child_llm = pipeline.create_child(
|
| 220 |
+
name="LLMCall",
|
| 221 |
+
run_type="llm",
|
| 222 |
+
inputs={
|
| 223 |
+
"model": GEMINI_MODEL,
|
| 224 |
+
"provider": "gemini-openai",
|
| 225 |
+
"messages_preview": _preview_text(str(messages[-1]), 600),
|
| 226 |
+
},
|
| 227 |
+
)
|
| 228 |
+
child_llm.post()
|
| 229 |
+
except Exception:
|
| 230 |
+
child_llm = None
|
| 231 |
+
|
| 232 |
stream = _client.chat.completions.create(
|
| 233 |
model=GEMINI_MODEL,
|
| 234 |
messages=messages,
|
|
|
|
| 255 |
|
| 256 |
if not accumulated:
|
| 257 |
yield "(Sin contenido de respuesta)"
|
| 258 |
+
|
| 259 |
+
if child_llm:
|
| 260 |
+
try:
|
| 261 |
+
child_llm.end(outputs={"content": _preview_text(accumulated, 5000)})
|
| 262 |
+
child_llm.patch()
|
| 263 |
+
except Exception:
|
| 264 |
+
pass
|
| 265 |
+
if pipeline:
|
| 266 |
+
try:
|
| 267 |
+
pipeline.end(outputs={"answer": _preview_text(accumulated, 5000)})
|
| 268 |
+
pipeline.patch()
|
| 269 |
+
except Exception:
|
| 270 |
+
pass
|
| 271 |
except Exception as e:
|
| 272 |
+
if child_llm:
|
| 273 |
+
try:
|
| 274 |
+
child_llm.end(outputs={"error": str(e)})
|
| 275 |
+
child_llm.patch()
|
| 276 |
+
except Exception:
|
| 277 |
+
pass
|
| 278 |
+
if pipeline:
|
| 279 |
+
try:
|
| 280 |
+
pipeline.end(outputs={"error": str(e)})
|
| 281 |
+
pipeline.patch()
|
| 282 |
+
except Exception:
|
| 283 |
+
pass
|
| 284 |
yield f"Ocurrió un error al llamar a Gemini: {e}"
|
| 285 |
|
| 286 |
|