|
6 | 6 | try: |
7 | 7 | from google import genai |
8 | 8 | except ImportError: |
9 | | - raise ModuleNotFoundError("Please install the Google Gemini SDK to use this feature: 'pip install google-genai'") |
10 | | - |
11 | | -from posthog.ai.utils import call_llm_and_track_usage, get_model_params, with_privacy_mode |
| 9 | + raise ModuleNotFoundError( |
| 10 | + "Please install the Google Gemini SDK to use this feature: 'pip install google-genai'" |
| 11 | + ) |
| 12 | + |
| 13 | +from posthog.ai.utils import ( |
| 14 | + call_llm_and_track_usage, |
| 15 | + get_model_params, |
| 16 | + with_privacy_mode, |
| 17 | +) |
12 | 18 | from posthog.client import Client as PostHogClient |
13 | 19 |
|
14 | 20 |
|
@@ -124,8 +130,16 @@ def _merge_posthog_params( |
124 | 130 | ): |
125 | 131 | """Merge call-level PostHog parameters with client defaults.""" |
126 | 132 | # Use call-level values if provided, otherwise fall back to defaults |
127 | | - distinct_id = call_distinct_id if call_distinct_id is not None else self._default_distinct_id |
128 | | - privacy_mode = call_privacy_mode if call_privacy_mode is not None else self._default_privacy_mode |
| 133 | + distinct_id = ( |
| 134 | + call_distinct_id |
| 135 | + if call_distinct_id is not None |
| 136 | + else self._default_distinct_id |
| 137 | + ) |
| 138 | + privacy_mode = ( |
| 139 | + call_privacy_mode |
| 140 | + if call_privacy_mode is not None |
| 141 | + else self._default_privacy_mode |
| 142 | + ) |
129 | 143 | groups = call_groups if call_groups is not None else self._default_groups |
130 | 144 |
|
131 | 145 | # Merge properties: default properties + call properties (call properties override) |
@@ -166,8 +180,14 @@ def generate_content( |
166 | 180 | **kwargs: Arguments passed to Gemini's generate_content |
167 | 181 | """ |
168 | 182 | # Merge PostHog parameters |
169 | | - distinct_id, trace_id, properties, privacy_mode, groups = self._merge_posthog_params( |
170 | | - posthog_distinct_id, posthog_trace_id, posthog_properties, posthog_privacy_mode, posthog_groups |
| 183 | + distinct_id, trace_id, properties, privacy_mode, groups = ( |
| 184 | + self._merge_posthog_params( |
| 185 | + posthog_distinct_id, |
| 186 | + posthog_trace_id, |
| 187 | + posthog_properties, |
| 188 | + posthog_privacy_mode, |
| 189 | + posthog_groups, |
| 190 | + ) |
171 | 191 | ) |
172 | 192 |
|
173 | 193 | kwargs_with_contents = {"model": model, "contents": contents, **kwargs} |
@@ -210,8 +230,12 @@ def generator(): |
210 | 230 | for chunk in response: |
211 | 231 | if hasattr(chunk, "usage_metadata") and chunk.usage_metadata: |
212 | 232 | usage_stats = { |
213 | | - "input_tokens": getattr(chunk.usage_metadata, "prompt_token_count", 0), |
214 | | - "output_tokens": getattr(chunk.usage_metadata, "candidates_token_count", 0), |
| 233 | + "input_tokens": getattr( |
| 234 | + chunk.usage_metadata, "prompt_token_count", 0 |
| 235 | + ), |
| 236 | + "output_tokens": getattr( |
| 237 | + chunk.usage_metadata, "candidates_token_count", 0 |
| 238 | + ), |
215 | 239 | } |
216 | 240 |
|
217 | 241 | if hasattr(chunk, "text") and chunk.text: |
@@ -320,8 +344,14 @@ def generate_content_stream( |
320 | 344 | **kwargs: Any, |
321 | 345 | ): |
322 | 346 | # Merge PostHog parameters |
323 | | - distinct_id, trace_id, properties, privacy_mode, groups = self._merge_posthog_params( |
324 | | - posthog_distinct_id, posthog_trace_id, posthog_properties, posthog_privacy_mode, posthog_groups |
| 347 | + distinct_id, trace_id, properties, privacy_mode, groups = ( |
| 348 | + self._merge_posthog_params( |
| 349 | + posthog_distinct_id, |
| 350 | + posthog_trace_id, |
| 351 | + posthog_properties, |
| 352 | + posthog_privacy_mode, |
| 353 | + posthog_groups, |
| 354 | + ) |
325 | 355 | ) |
326 | 356 |
|
327 | 357 | return self._generate_content_streaming( |
|
0 commit comments