Spaces:
Sleeping
Sleeping
| from __future__ import annotations | |
| from typing import Any | |
| from src.config import LANGFUSE_ENABLED | |
| def langfuse_config(user_id: str, session_id: str | None = None) -> dict[str, Any]: | |
| """Build LangChain/LangGraph runtime config with Langfuse tracing.""" | |
| if not LANGFUSE_ENABLED: | |
| return {} | |
| from langfuse.langchain import CallbackHandler | |
| return { | |
| "callbacks": [CallbackHandler()], | |
| "metadata": { | |
| "langfuse_user_id": user_id, | |
| "langfuse_session_id": session_id or user_id, | |
| "langfuse_tags": ["agentic-rag", "langgraph", "gemini"], | |
| }, | |
| "run_name": "agentic-rag-chat", | |
| } | |
| def flush_langfuse() -> None: | |
| """Force pending Langfuse spans to be exported after a request.""" | |
| if not LANGFUSE_ENABLED: | |
| return | |
| try: | |
| from langfuse import get_client | |
| get_client().flush() | |
| except Exception as exc: | |
| print(f"[LANGFUSE] Failed to flush traces: {exc}") | |