feat: add Logfire tracking/tracing for observability
Browse files- logfire.configure() with send_to_logfire="if-token-present" so it's
a safe no-op locally without a token, and instrument_httpx() to
auto-capture every Mistral API call (latency, status, errors).
- Wrap get_dm_response in a span and log exceptions instead of only
returning them as a chat string, since friends using the Space
won't otherwise surface failures anywhere visible.
- Document LOGFIRE_TOKEN in .env.example.
Verified locally end-to-end: traces show up in the Logfire project
both with and without a token present.
- .env.example +5 -0
- app.py +33 -22
- requirements.txt +1 -0
.env.example
CHANGED
|
@@ -6,3 +6,8 @@ MISTRAL_API_KEY=your_api_key_here
|
|
| 6 |
# Get your token at: https://huggingface.co/settings/tokens
|
| 7 |
# Required to push to Hugging Face Spaces
|
| 8 |
HF_TOKEN=your_hf_token_here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Get your token at: https://huggingface.co/settings/tokens
|
| 7 |
# Required to push to Hugging Face Spaces
|
| 8 |
HF_TOKEN=your_hf_token_here
|
| 9 |
+
|
| 10 |
+
# Pydantic Logfire write token for tracking/tracing
|
| 11 |
+
# Get your token at: https://logfire.pydantic.dev/ (project settings > write tokens)
|
| 12 |
+
# Optional: without it, logfire.configure(send_to_logfire="if-token-present") is a no-op
|
| 13 |
+
LOGFIRE_TOKEN=your_logfire_token_here
|
app.py
CHANGED
|
@@ -1,8 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
import random
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from mistralai.client import Mistral
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
# Load Mistral API key from environment variable
|
| 7 |
MISTRAL_API_KEY = os.environ.get("MISTRAL_API_KEY")
|
| 8 |
|
|
@@ -34,28 +38,35 @@ def roll_dice(dice_type: str) -> str:
|
|
| 34 |
|
| 35 |
def get_dm_response(message: str, history: list[tuple[str, str]]) -> str:
|
| 36 |
"""Get a response from the Mistral DM."""
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
def respond(message: str, history: list[tuple[str, str]]) -> tuple[str, list[tuple[str, str]]]:
|
|
|
|
| 1 |
import os
|
| 2 |
import random
|
| 3 |
import gradio as gr
|
| 4 |
+
import logfire
|
| 5 |
from mistralai.client import Mistral
|
| 6 |
|
| 7 |
+
logfire.configure(service_name="dnd-dm-assistant", send_to_logfire="if-token-present")
|
| 8 |
+
logfire.instrument_httpx(capture_all=True)
|
| 9 |
+
|
| 10 |
# Load Mistral API key from environment variable
|
| 11 |
MISTRAL_API_KEY = os.environ.get("MISTRAL_API_KEY")
|
| 12 |
|
|
|
|
| 38 |
|
| 39 |
def get_dm_response(message: str, history: list[tuple[str, str]]) -> str:
|
| 40 |
"""Get a response from the Mistral DM."""
|
| 41 |
+
with logfire.span(
|
| 42 |
+
"get_dm_response",
|
| 43 |
+
message_length=len(message),
|
| 44 |
+
history_turns=len(history),
|
| 45 |
+
):
|
| 46 |
+
if client is None:
|
| 47 |
+
logfire.error("Mistral API key not configured")
|
| 48 |
+
return "Error: Mistral API key not configured. Set MISTRAL_API_KEY environment variable."
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
# Build messages including system prompt and conversation history
|
| 52 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 53 |
+
for user_msg, assistant_msg in history:
|
| 54 |
+
messages.append({"role": "user", "content": user_msg})
|
| 55 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 56 |
+
messages.append({"role": "user", "content": message})
|
| 57 |
+
|
| 58 |
+
# Get response from Mistral
|
| 59 |
+
response = client.chat.complete(
|
| 60 |
+
model="mistral-medium-latest",
|
| 61 |
+
messages=messages,
|
| 62 |
+
temperature=0.7,
|
| 63 |
+
max_tokens=150,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
return response.choices[0].message.content
|
| 67 |
+
except Exception as e:
|
| 68 |
+
logfire.exception("Mistral request failed")
|
| 69 |
+
return f"Error: Failed to get response from Mistral. {str(e)}"
|
| 70 |
|
| 71 |
|
| 72 |
def respond(message: str, history: list[tuple[str, str]]) -> tuple[str, list[tuple[str, str]]]:
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
mistralai>=1.0.0
|
| 2 |
python-dotenv>=1.0.0
|
| 3 |
gradio==6.20.0
|
|
|
|
|
|
| 1 |
mistralai>=1.0.0
|
| 2 |
python-dotenv>=1.0.0
|
| 3 |
gradio==6.20.0
|
| 4 |
+
logfire[httpx]
|