|
| 1 | +import time |
| 2 | +from typing import Any, Dict, Optional, Union |
| 3 | + |
| 4 | +import openai |
| 5 | +from posthog.client import Client as PostHogClient |
| 6 | + |
| 7 | + |
| 8 | +def get_model_params(kwargs: Dict[str, Any]) -> Dict[str, Any]: |
| 9 | + """ |
| 10 | + Extracts model parameters from the kwargs dictionary. |
| 11 | + """ |
| 12 | + model_params = {} |
| 13 | + for param in ["temperature", "max_tokens", "top_p", "frequency_penalty", |
| 14 | + "presence_penalty", "n", "stop", "stream"]: |
| 15 | + if param in kwargs: |
| 16 | + model_params[param] = kwargs.get(param) |
| 17 | + return model_params |
| 18 | + |
| 19 | +def get_output(response: openai.types.chat.ChatCompletion) -> Dict[str, Any]: |
| 20 | + output = { |
| 21 | + "choices": [] |
| 22 | + } |
| 23 | + for choice in response.choices: |
| 24 | + if choice.message.content: |
| 25 | + output["choices"].append({ |
| 26 | + "content": choice.message.content, |
| 27 | + "role": choice.message.role, |
| 28 | + }) |
| 29 | + return output |
| 30 | + |
| 31 | + |
| 32 | +class OpenAI: |
| 33 | + """ |
| 34 | + A wrapper around the OpenAI SDK that automatically sends LLM usage events to PostHog. |
| 35 | + """ |
| 36 | + |
| 37 | + def __init__( |
| 38 | + self, |
| 39 | + posthog_client: PostHogClient, |
| 40 | + **openai_config: Any, |
| 41 | + ): |
| 42 | + """ |
| 43 | + Args: |
| 44 | + api_key: OpenAI API key. |
| 45 | + posthog_client: If provided, events will be captured via this client instance instead |
| 46 | + of the global posthog module. |
| 47 | + **openai_config: Any additional keyword args to set on openai (e.g. organization="xxx"). |
| 48 | + """ |
| 49 | + # Initialize OpenAI client instead of setting global config |
| 50 | + self._openai_client = openai.OpenAI(**openai_config) |
| 51 | + self._posthog_client = posthog_client |
| 52 | + |
| 53 | + @property |
| 54 | + def chat(self) -> "ChatNamespace": |
| 55 | + return ChatNamespace(self._posthog_client, self._openai_client) |
| 56 | + |
| 57 | + |
| 58 | +class ChatNamespace: |
| 59 | + def __init__(self, posthog_client: Union[PostHogClient, Any], openai_client: Any): |
| 60 | + self._ph_client = posthog_client |
| 61 | + self._openai_client = openai_client |
| 62 | + |
| 63 | + @property |
| 64 | + def completions(self): |
| 65 | + return ChatCompletions(self._ph_client, self._openai_client) |
| 66 | + |
| 67 | + |
| 68 | +class ChatCompletions: |
| 69 | + |
| 70 | + def __init__(self, posthog_client: Union[PostHogClient, Any], openai_client: Any): |
| 71 | + self._ph_client = posthog_client |
| 72 | + self._openai_client = openai_client |
| 73 | + |
| 74 | + def create( |
| 75 | + self, |
| 76 | + posthog_distinct_id: Optional[str] = None, |
| 77 | + posthog_trace_id: Optional[str] = None, |
| 78 | + posthog_properties: Optional[Dict[str, Any]] = None, |
| 79 | + **kwargs: Any, |
| 80 | + ): |
| 81 | + """ |
| 82 | + Wraps open ai chat completions and captures a $ai_generation event in PostHog. |
| 83 | +
|
| 84 | + PostHog-specific parameters: |
| 85 | + posthog_distinct_id: Ties the resulting event to a user in PostHog. |
| 86 | + posthog_trace_id: For grouping multiple calls into a single trace. |
| 87 | + posthog_properties: Additional custom properties to include on the PostHog event. |
| 88 | + """ |
| 89 | + start_time = time.time() |
| 90 | + response = None |
| 91 | + error = None |
| 92 | + http_status = 200 |
| 93 | + usage: Dict[str, Any] = {} |
| 94 | + |
| 95 | + try: |
| 96 | + response = self._openai_client.chat.completions.create(**kwargs) |
| 97 | + except Exception as exc: |
| 98 | + error = exc |
| 99 | + http_status = getattr(exc, 'status_code', 500) |
| 100 | + finally: |
| 101 | + end_time = time.time() |
| 102 | + latency = end_time - start_time |
| 103 | + |
| 104 | + # Update usage extraction for new response format |
| 105 | + if response and hasattr(response, "usage"): |
| 106 | + usage = response.usage.model_dump() |
| 107 | + |
| 108 | + input_tokens = usage.get("prompt_tokens", 0) |
| 109 | + output_tokens = usage.get("completion_tokens", 0) |
| 110 | + |
| 111 | + # Build PostHog event properties |
| 112 | + event_properties = { |
| 113 | + "$ai_provider": "openai", |
| 114 | + "$ai_model": kwargs.get("model"), |
| 115 | + "$ai_model_parameters": get_model_params(kwargs), |
| 116 | + "$ai_input": kwargs.get("messages"), |
| 117 | + "$ai_output": None, |
| 118 | + "$ai_http_status": http_status, |
| 119 | + "$ai_input_tokens": input_tokens, |
| 120 | + "$ai_output_tokens": output_tokens, |
| 121 | + "$ai_latency": latency, |
| 122 | + "$ai_trace_id": posthog_trace_id, |
| 123 | + } |
| 124 | + |
| 125 | + # If not streaming and no error, try storing some output detail |
| 126 | + # TODO: we need to support streaming responses |
| 127 | + stream = kwargs.get("stream", False) |
| 128 | + if response and not error and not stream: |
| 129 | + event_properties["$ai_output"] = get_output(response) |
| 130 | + |
| 131 | + # Merge in any custom PostHog properties |
| 132 | + if posthog_properties: |
| 133 | + event_properties.update(posthog_properties) |
| 134 | + |
| 135 | + # Capture event in PostHog |
| 136 | + if hasattr(self._ph_client, "capture") and callable(self._ph_client.capture): |
| 137 | + distinct_id = posthog_distinct_id or "anonymous_ai_user" |
| 138 | + self._ph_client.capture( |
| 139 | + distinct_id=distinct_id, |
| 140 | + event="$ai_generation", |
| 141 | + properties=event_properties, |
| 142 | + ) |
| 143 | + |
| 144 | + if error: |
| 145 | + raise error |
| 146 | + |
| 147 | + return response |
0 commit comments