-
Notifications
You must be signed in to change notification settings - Fork 10
Add Langfuse observability to Unified API #457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4f0cf35
4231e2b
ebf50d1
8318707
01ad61e
483ca18
8abe029
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,10 +1,12 @@ | ||||||||||||||||
| import uuid | ||||||||||||||||
| import logging | ||||||||||||||||
| from typing import Any, Dict, Optional | ||||||||||||||||
| from typing import Any, Callable, Dict, Optional | ||||||||||||||||
| from functools import wraps | ||||||||||||||||
|
|
||||||||||||||||
| from asgi_correlation_id import correlation_id | ||||||||||||||||
| from langfuse import Langfuse | ||||||||||||||||
| from langfuse.client import StatefulGenerationClient, StatefulTraceClient | ||||||||||||||||
| from app.models.llm import CompletionConfig, QueryParams, LLMCallResponse | ||||||||||||||||
|
|
||||||||||||||||
| logger = logging.getLogger(__name__) | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -107,3 +109,102 @@ def log_error(self, error_message: str, response_id: Optional[str] = None): | |||||||||||||||
|
|
||||||||||||||||
| def flush(self): | ||||||||||||||||
| self.langfuse.flush() | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| def observe_llm_execution( | ||||||||||||||||
| session_id: str | None = None, | ||||||||||||||||
| credentials: dict | None = None, | ||||||||||||||||
| ): | ||||||||||||||||
| """Decorator to add Langfuse observability to LLM provider execute methods. | ||||||||||||||||
|
|
||||||||||||||||
| Args: | ||||||||||||||||
| credentials: Langfuse credentials with public_key, secret_key, and host | ||||||||||||||||
| session_id: Session ID for grouping traces (conversation_id) | ||||||||||||||||
|
|
||||||||||||||||
| Usage: | ||||||||||||||||
| decorated_execute = observe_llm_execution( | ||||||||||||||||
| credentials=langfuse_creds, | ||||||||||||||||
| session_id=conversation_id | ||||||||||||||||
| )(provider_instance.execute) | ||||||||||||||||
| """ | ||||||||||||||||
|
|
||||||||||||||||
| def decorator(func: Callable) -> Callable: | ||||||||||||||||
| @wraps(func) | ||||||||||||||||
| def wrapper(completion_config: CompletionConfig, query: QueryParams, **kwargs): | ||||||||||||||||
avirajsingh7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
| # Skip observability if no credentials provided | ||||||||||||||||
| if not credentials: | ||||||||||||||||
| logger.info("[Langfuse] No credentials - skipping observability") | ||||||||||||||||
| return func(completion_config, query, **kwargs) | ||||||||||||||||
|
|
||||||||||||||||
| try: | ||||||||||||||||
| langfuse = Langfuse( | ||||||||||||||||
| public_key=credentials.get("public_key"), | ||||||||||||||||
| secret_key=credentials.get("secret_key"), | ||||||||||||||||
| host=credentials.get("host"), | ||||||||||||||||
| ) | ||||||||||||||||
| except Exception as e: | ||||||||||||||||
| logger.warning(f"[Langfuse] Failed to initialize client: {e}") | ||||||||||||||||
| return func(completion_config, query, **kwargs) | ||||||||||||||||
|
|
||||||||||||||||
| trace = langfuse.trace( | ||||||||||||||||
|
Comment on lines
+147
to
+149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix Black formatting: remove extra blank line. The pipeline failure indicates Black formatting failed. There's an extra blank line between line 149 and 152. Remove one blank line to fix the formatting. except Exception as e:
logger.warning(f"[Langfuse] Failed to initialize client: {e}")
return func(completion_config, query, **kwargs)
-
trace = langfuse.trace(📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||
| name="unified-llm-call", | ||||||||||||||||
| input=query.input, | ||||||||||||||||
| tags=[completion_config.provider], | ||||||||||||||||
avirajsingh7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
| ) | ||||||||||||||||
avirajsingh7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
|
||||||||||||||||
| generation = trace.generation( | ||||||||||||||||
| name=f"{completion_config.provider}-completion", | ||||||||||||||||
| input=query.input, | ||||||||||||||||
| model=completion_config.params.get("model"), | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| try: | ||||||||||||||||
| # Execute the actual LLM call | ||||||||||||||||
| response: LLMCallResponse | None | ||||||||||||||||
| error: str | None | ||||||||||||||||
| response, error = func(completion_config, query, **kwargs) | ||||||||||||||||
|
|
||||||||||||||||
| if response: | ||||||||||||||||
| generation.end( | ||||||||||||||||
| output={ | ||||||||||||||||
| "status": "success", | ||||||||||||||||
| "output": response.response.output.text, | ||||||||||||||||
| }, | ||||||||||||||||
| usage_details={ | ||||||||||||||||
| "input": response.usage.input_tokens, | ||||||||||||||||
| "output": response.usage.output_tokens, | ||||||||||||||||
| }, | ||||||||||||||||
| model=response.response.model, | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| trace.update( | ||||||||||||||||
| output={ | ||||||||||||||||
| "status": "success", | ||||||||||||||||
| "output": response.response.output.text, | ||||||||||||||||
| }, | ||||||||||||||||
| session_id=session_id or response.response.conversation_id, | ||||||||||||||||
| ) | ||||||||||||||||
| else: | ||||||||||||||||
| error_msg = error or "Unknown error" | ||||||||||||||||
| generation.end(output={"error": error_msg}) | ||||||||||||||||
| trace.update( | ||||||||||||||||
| output={"status": "failure", "error": error_msg}, | ||||||||||||||||
| session_id=session_id, | ||||||||||||||||
avirajsingh7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| langfuse.flush() | ||||||||||||||||
avirajsingh7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
| return response, error | ||||||||||||||||
|
|
||||||||||||||||
| except Exception as e: | ||||||||||||||||
| error_msg = str(e) | ||||||||||||||||
| generation.end(output={"error": error_msg}) | ||||||||||||||||
| trace.update( | ||||||||||||||||
| output={"status": "failure", "error": error_msg}, | ||||||||||||||||
| session_id=session_id, | ||||||||||||||||
| ) | ||||||||||||||||
| langfuse.flush() | ||||||||||||||||
| raise | ||||||||||||||||
|
|
||||||||||||||||
| return wrapper | ||||||||||||||||
|
|
||||||||||||||||
| return decorator | ||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.