Spaces:
Running
Running
| """ | |
| LangSmith + LangChain tracing configuration dla GrantForge AI. | |
| FAZA 6: LLMOps — monitorowanie halucynacji, faithfulness, latencji. | |
| Użycie: | |
| from core.langsmith_config import configure_langsmith | |
| configure_langsmith() # wywołaj raz w startup/lifespan | |
| Wymagane zmienne środowiskowe: | |
| LANGCHAIN_API_KEY — klucz z https://smith.langchain.com | |
| LANGCHAIN_PROJECT — nazwa projektu (np. "grantforge-prod") | |
| LANGCHAIN_TRACING_V2 — "true" / "false" | |
| ENVIRONMENT — "development" / "production" | |
| """ | |
| import os | |
| import logging | |
| logger = logging.getLogger(__name__) | |
| def configure_langsmith() -> bool: | |
| """ | |
| Konfiguruje LangSmith tracing. | |
| Zwraca True jeśli tracing aktywny, False jeśli brak klucza. | |
| """ | |
| api_key = os.environ.get("LANGCHAIN_API_KEY") or os.environ.get("LANGSMITH_API_KEY") | |
| project = os.environ.get("LANGCHAIN_PROJECT", "grantforge-dev") | |
| env = os.environ.get("ENVIRONMENT", "development") | |
| if not api_key: | |
| logger.warning( | |
| "[LangSmith] Brak LANGCHAIN_API_KEY — tracing wyłączony. " | |
| "Ustaw klucz z https://smith.langchain.com dla monitoringu LLM." | |
| ) | |
| return False | |
| # Ustawiamy zmienne środowiskowe wymagane przez LangChain | |
| os.environ["LANGCHAIN_TRACING_V2"] = "true" | |
| os.environ["LANGCHAIN_API_KEY"] = api_key | |
| os.environ["LANGCHAIN_PROJECT"] = project | |
| os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com" | |
| # Tag środowiska — widoczny w dashboardzie | |
| os.environ["LANGCHAIN_TAGS"] = f"env:{env},version:beta-1.0" | |
| logger.info( | |
| f"[LangSmith] Tracing AKTYWNY → projekt: '{project}' | " | |
| f"env: {env} | endpoint: {os.environ['LANGCHAIN_ENDPOINT']}" | |
| ) | |
| return True | |
| def get_langsmith_run_url(run_id: str) -> str: | |
| """Generuje URL do konkretnego runu w LangSmith.""" | |
| project = os.environ.get("LANGCHAIN_PROJECT", "grantforge-dev") | |
| return f"https://smith.langchain.com/o/default/projects/p/{project}/r/{run_id}" | |