posthoganalytics 6.7.3__py3-none-any.whl → 6.7.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- posthoganalytics/ai/gemini/gemini.py +7 -7
- posthoganalytics/ai/gemini/gemini_converter.py +56 -0
- posthoganalytics/ai/openai/openai_converter.py +3 -4
- posthoganalytics/ai/utils.py +18 -11
- posthoganalytics/version.py +1 -1
- {posthoganalytics-6.7.3.dist-info → posthoganalytics-6.7.4.dist-info}/METADATA +1 -1
- {posthoganalytics-6.7.3.dist-info → posthoganalytics-6.7.4.dist-info}/RECORD +10 -10
- {posthoganalytics-6.7.3.dist-info → posthoganalytics-6.7.4.dist-info}/WHEEL +0 -0
- {posthoganalytics-6.7.3.dist-info → posthoganalytics-6.7.4.dist-info}/licenses/LICENSE +0 -0
- {posthoganalytics-6.7.3.dist-info → posthoganalytics-6.7.4.dist-info}/top_level.txt +0 -0
|
@@ -3,7 +3,8 @@ import time
|
|
|
3
3
|
import uuid
|
|
4
4
|
from typing import Any, Dict, Optional
|
|
5
5
|
|
|
6
|
-
from posthoganalytics.ai.types import TokenUsage
|
|
6
|
+
from posthoganalytics.ai.types import TokenUsage, StreamingEventData
|
|
7
|
+
from posthoganalytics.ai.utils import merge_system_prompt
|
|
7
8
|
|
|
8
9
|
try:
|
|
9
10
|
from google import genai
|
|
@@ -19,7 +20,6 @@ from posthoganalytics.ai.utils import (
|
|
|
19
20
|
merge_usage_stats,
|
|
20
21
|
)
|
|
21
22
|
from posthoganalytics.ai.gemini.gemini_converter import (
|
|
22
|
-
format_gemini_input,
|
|
23
23
|
extract_gemini_usage_from_chunk,
|
|
24
24
|
extract_gemini_content_from_chunk,
|
|
25
25
|
format_gemini_streaming_output,
|
|
@@ -356,10 +356,8 @@ class Models:
|
|
|
356
356
|
latency: float,
|
|
357
357
|
output: Any,
|
|
358
358
|
):
|
|
359
|
-
from posthoganalytics.ai.types import StreamingEventData
|
|
360
|
-
|
|
361
359
|
# Prepare standardized event data
|
|
362
|
-
formatted_input = self._format_input(contents)
|
|
360
|
+
formatted_input = self._format_input(contents, **kwargs)
|
|
363
361
|
sanitized_input = sanitize_gemini(formatted_input)
|
|
364
362
|
|
|
365
363
|
event_data = StreamingEventData(
|
|
@@ -381,10 +379,12 @@ class Models:
|
|
|
381
379
|
# Use the common capture function
|
|
382
380
|
capture_streaming_event(self._ph_client, event_data)
|
|
383
381
|
|
|
384
|
-
def _format_input(self, contents):
|
|
382
|
+
def _format_input(self, contents, **kwargs):
|
|
385
383
|
"""Format input contents for PostHog tracking"""
|
|
386
384
|
|
|
387
|
-
|
|
385
|
+
# Create kwargs dict with contents for merge_system_prompt
|
|
386
|
+
input_kwargs = {"contents": contents, **kwargs}
|
|
387
|
+
return merge_system_prompt(input_kwargs, "gemini")
|
|
388
388
|
|
|
389
389
|
def generate_content_stream(
|
|
390
390
|
self,
|
|
@@ -220,6 +220,30 @@ def format_gemini_response(response: Any) -> List[FormattedMessage]:
|
|
|
220
220
|
return output
|
|
221
221
|
|
|
222
222
|
|
|
223
|
+
def extract_gemini_system_instruction(config: Any) -> Optional[str]:
|
|
224
|
+
"""
|
|
225
|
+
Extract system instruction from Gemini config parameter.
|
|
226
|
+
|
|
227
|
+
Args:
|
|
228
|
+
config: Config object or dict that may contain system instruction
|
|
229
|
+
|
|
230
|
+
Returns:
|
|
231
|
+
System instruction string if present, None otherwise
|
|
232
|
+
"""
|
|
233
|
+
if config is None:
|
|
234
|
+
return None
|
|
235
|
+
|
|
236
|
+
# Handle different config formats
|
|
237
|
+
if hasattr(config, "system_instruction"):
|
|
238
|
+
return config.system_instruction
|
|
239
|
+
elif isinstance(config, dict) and "system_instruction" in config:
|
|
240
|
+
return config["system_instruction"]
|
|
241
|
+
elif isinstance(config, dict) and "systemInstruction" in config:
|
|
242
|
+
return config["systemInstruction"]
|
|
243
|
+
|
|
244
|
+
return None
|
|
245
|
+
|
|
246
|
+
|
|
223
247
|
def extract_gemini_tools(kwargs: Dict[str, Any]) -> Optional[Any]:
|
|
224
248
|
"""
|
|
225
249
|
Extract tool definitions from Gemini API kwargs.
|
|
@@ -237,6 +261,38 @@ def extract_gemini_tools(kwargs: Dict[str, Any]) -> Optional[Any]:
|
|
|
237
261
|
return None
|
|
238
262
|
|
|
239
263
|
|
|
264
|
+
def format_gemini_input_with_system(
|
|
265
|
+
contents: Any, config: Any = None
|
|
266
|
+
) -> List[FormattedMessage]:
|
|
267
|
+
"""
|
|
268
|
+
Format Gemini input contents into standardized message format, including system instruction handling.
|
|
269
|
+
|
|
270
|
+
Args:
|
|
271
|
+
contents: Input contents in various possible formats
|
|
272
|
+
config: Config object or dict that may contain system instruction
|
|
273
|
+
|
|
274
|
+
Returns:
|
|
275
|
+
List of formatted messages with role and content fields, with system message prepended if needed
|
|
276
|
+
"""
|
|
277
|
+
formatted_messages = format_gemini_input(contents)
|
|
278
|
+
|
|
279
|
+
# Check if system instruction is provided in config parameter
|
|
280
|
+
system_instruction = extract_gemini_system_instruction(config)
|
|
281
|
+
|
|
282
|
+
if system_instruction is not None:
|
|
283
|
+
has_system = any(msg.get("role") == "system" for msg in formatted_messages)
|
|
284
|
+
if not has_system:
|
|
285
|
+
from posthoganalytics.ai.types import FormattedMessage
|
|
286
|
+
|
|
287
|
+
system_message: FormattedMessage = {
|
|
288
|
+
"role": "system",
|
|
289
|
+
"content": system_instruction,
|
|
290
|
+
}
|
|
291
|
+
formatted_messages = [system_message] + list(formatted_messages)
|
|
292
|
+
|
|
293
|
+
return formatted_messages
|
|
294
|
+
|
|
295
|
+
|
|
240
296
|
def format_gemini_input(contents: Any) -> List[FormattedMessage]:
|
|
241
297
|
"""
|
|
242
298
|
Format Gemini input contents into standardized message format for PostHog tracking.
|
|
@@ -606,7 +606,6 @@ def format_openai_streaming_input(
|
|
|
606
606
|
Returns:
|
|
607
607
|
Formatted input ready for PostHog tracking
|
|
608
608
|
"""
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
return kwargs.get("input")
|
|
609
|
+
from posthoganalytics.ai.utils import merge_system_prompt
|
|
610
|
+
|
|
611
|
+
return merge_system_prompt(kwargs, "openai")
|
posthoganalytics/ai/utils.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import time
|
|
2
2
|
import uuid
|
|
3
|
-
from typing import Any, Callable, Dict, Optional
|
|
3
|
+
from typing import Any, Callable, Dict, List, Optional, cast
|
|
4
4
|
|
|
5
5
|
from posthoganalytics.client import Client as PostHogClient
|
|
6
|
-
from posthoganalytics.ai.types import StreamingEventData, TokenUsage
|
|
6
|
+
from posthoganalytics.ai.types import FormattedMessage, StreamingEventData, TokenUsage
|
|
7
7
|
from posthoganalytics.ai.sanitization import (
|
|
8
8
|
sanitize_openai,
|
|
9
9
|
sanitize_anthropic,
|
|
@@ -158,7 +158,9 @@ def extract_available_tool_calls(provider: str, kwargs: Dict[str, Any]):
|
|
|
158
158
|
return None
|
|
159
159
|
|
|
160
160
|
|
|
161
|
-
def merge_system_prompt(
|
|
161
|
+
def merge_system_prompt(
|
|
162
|
+
kwargs: Dict[str, Any], provider: str
|
|
163
|
+
) -> List[FormattedMessage]:
|
|
162
164
|
"""
|
|
163
165
|
Merge system prompts and format messages for the given provider.
|
|
164
166
|
"""
|
|
@@ -169,10 +171,11 @@ def merge_system_prompt(kwargs: Dict[str, Any], provider: str):
|
|
|
169
171
|
system = kwargs.get("system")
|
|
170
172
|
return format_anthropic_input(messages, system)
|
|
171
173
|
elif provider == "gemini":
|
|
172
|
-
from posthoganalytics.ai.gemini.gemini_converter import
|
|
174
|
+
from posthoganalytics.ai.gemini.gemini_converter import format_gemini_input_with_system
|
|
173
175
|
|
|
174
176
|
contents = kwargs.get("contents", [])
|
|
175
|
-
|
|
177
|
+
config = kwargs.get("config")
|
|
178
|
+
return format_gemini_input_with_system(contents, config)
|
|
176
179
|
elif provider == "openai":
|
|
177
180
|
from posthoganalytics.ai.openai.openai_converter import format_openai_input
|
|
178
181
|
|
|
@@ -187,9 +190,11 @@ def merge_system_prompt(kwargs: Dict[str, Any], provider: str):
|
|
|
187
190
|
if kwargs.get("system") is not None:
|
|
188
191
|
has_system = any(msg.get("role") == "system" for msg in messages)
|
|
189
192
|
if not has_system:
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
+
system_msg = cast(
|
|
194
|
+
FormattedMessage,
|
|
195
|
+
{"role": "system", "content": kwargs.get("system")},
|
|
196
|
+
)
|
|
197
|
+
messages = [system_msg] + messages
|
|
193
198
|
|
|
194
199
|
# For Responses API, add instructions to the system prompt if provided
|
|
195
200
|
if kwargs.get("instructions") is not None:
|
|
@@ -207,9 +212,11 @@ def merge_system_prompt(kwargs: Dict[str, Any], provider: str):
|
|
|
207
212
|
)
|
|
208
213
|
else:
|
|
209
214
|
# Create a new system message with instructions
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
215
|
+
instruction_msg = cast(
|
|
216
|
+
FormattedMessage,
|
|
217
|
+
{"role": "system", "content": kwargs.get("instructions")},
|
|
218
|
+
)
|
|
219
|
+
messages = [instruction_msg] + messages
|
|
213
220
|
|
|
214
221
|
return messages
|
|
215
222
|
|
posthoganalytics/version.py
CHANGED
|
@@ -11,25 +11,25 @@ posthoganalytics/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
|
11
11
|
posthoganalytics/request.py,sha256=Bsl2c5WwONKPQzwWMmKPX5VgOlwSiIcSNfhXgoz62Y8,6186
|
|
12
12
|
posthoganalytics/types.py,sha256=Dl3aFGX9XUR0wMmK12r2s5Hjan9jL4HpQ9GHpVcEq5U,10207
|
|
13
13
|
posthoganalytics/utils.py,sha256=-0w-OLcCaoldkbBebPzQyBzLJSo9G9yBOg8NDVz7La8,16088
|
|
14
|
-
posthoganalytics/version.py,sha256=
|
|
14
|
+
posthoganalytics/version.py,sha256=RJbegcgNmUJvUzvz3PhJ7kXrYNUcMXmEqH9X-ctDffs,87
|
|
15
15
|
posthoganalytics/ai/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
16
16
|
posthoganalytics/ai/sanitization.py,sha256=owipZ4eJYtd4JTI-CM_klatclXaeaIec3XJBOUfsOnQ,5770
|
|
17
17
|
posthoganalytics/ai/types.py,sha256=ceubs4K9xf8vQx7wokq1NL9hPtxyS7D7sUOuT7Lx1lM,3237
|
|
18
|
-
posthoganalytics/ai/utils.py,sha256=
|
|
18
|
+
posthoganalytics/ai/utils.py,sha256=WPeb-K_c4wwLJ2fr6s6OAq9YuxwvFhyGqQdIwaRSUQw,20364
|
|
19
19
|
posthoganalytics/ai/anthropic/__init__.py,sha256=8nTvETZzkfW-P3zBMmp06GOHs0N-xyOGu7Oa4di_lno,669
|
|
20
20
|
posthoganalytics/ai/anthropic/anthropic.py,sha256=njOoVb9vkCdnPWAQuVF0XB0BnT2y1ScIryrCGyt5ur8,8750
|
|
21
21
|
posthoganalytics/ai/anthropic/anthropic_async.py,sha256=nM3oFcNLw6meEtV6RfrvhFcuxD4aS-CXDuepRHycUjM,10169
|
|
22
22
|
posthoganalytics/ai/anthropic/anthropic_converter.py,sha256=LWIQ1kyK3vV3rLBmQIcd-98fet7isK3uhTRmBqBN0lk,11776
|
|
23
23
|
posthoganalytics/ai/anthropic/anthropic_providers.py,sha256=y1_qc8Lbip-YDmpimPGg3DfTm5g-WZk5FrRCXzwF_Ow,2139
|
|
24
24
|
posthoganalytics/ai/gemini/__init__.py,sha256=JV_9-gBR87leHgZW4XAYZP7LSl4YaXeuhqDUpA8HygA,383
|
|
25
|
-
posthoganalytics/ai/gemini/gemini.py,sha256=
|
|
26
|
-
posthoganalytics/ai/gemini/gemini_converter.py,sha256=
|
|
25
|
+
posthoganalytics/ai/gemini/gemini.py,sha256=A2acjT_m8ru2YwgIk15aN21CRVEl2jh8pbqjmHplMC8,15035
|
|
26
|
+
posthoganalytics/ai/gemini/gemini_converter.py,sha256=WzRsid-FjXRyhAI5wQ9-tjTapYVCTRKuMPcZFYKUdIo,16027
|
|
27
27
|
posthoganalytics/ai/langchain/__init__.py,sha256=9CqAwLynTGj3ASAR80C3PmdTdrYGmu99tz0JL-HPFgI,70
|
|
28
28
|
posthoganalytics/ai/langchain/callbacks.py,sha256=Otha0a6YLBwETfKjDDbdLzNi-RHRgKFJB69GwWCv9lg,29527
|
|
29
29
|
posthoganalytics/ai/openai/__init__.py,sha256=u4OuUT7k1NgFj0TrxjuyegOg7a_UA8nAU6a-Hszr0OM,490
|
|
30
30
|
posthoganalytics/ai/openai/openai.py,sha256=I05NruE9grWezM_EgOZBiG5Ej_gABsDcYKN0pRQWvzU,20235
|
|
31
31
|
posthoganalytics/ai/openai/openai_async.py,sha256=k6bo3LfJ_CAPBZCxAzyM2uLz4BpW2YWEFhNuzVcpJlM,21811
|
|
32
|
-
posthoganalytics/ai/openai/openai_converter.py,sha256=
|
|
32
|
+
posthoganalytics/ai/openai/openai_converter.py,sha256=VBaAGdXPSVNgfvCnSAojslWkTRO2luUxpjafR-WMEbs,20469
|
|
33
33
|
posthoganalytics/ai/openai/openai_providers.py,sha256=RPVmj2V0_lAdno_ax5Ul2kwhBA9_rRgAdl_sCqrQc6M,4004
|
|
34
34
|
posthoganalytics/integrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
35
35
|
posthoganalytics/integrations/django.py,sha256=KYtBr7CkiZQynRc2TCWWYHe-J3ie8iSUa42WPshYZdc,6795
|
|
@@ -47,8 +47,8 @@ posthoganalytics/test/test_request.py,sha256=Zc0VbkjpVmj8mKokQm9rzdgTr0b1U44vvMY
|
|
|
47
47
|
posthoganalytics/test/test_size_limited_dict.py,sha256=-5IQjIEr_-Dql24M0HusdR_XroOMrtgiT0v6ZQCRvzo,774
|
|
48
48
|
posthoganalytics/test/test_types.py,sha256=bRPHdwVpP7hu7emsplU8UVyzSQptv6PaG5lAoOD_BtM,7595
|
|
49
49
|
posthoganalytics/test/test_utils.py,sha256=sqUTbfweVcxxFRd3WDMFXqPMyU6DvzOBeAOc68Py9aw,9620
|
|
50
|
-
posthoganalytics-6.7.
|
|
51
|
-
posthoganalytics-6.7.
|
|
52
|
-
posthoganalytics-6.7.
|
|
53
|
-
posthoganalytics-6.7.
|
|
54
|
-
posthoganalytics-6.7.
|
|
50
|
+
posthoganalytics-6.7.4.dist-info/licenses/LICENSE,sha256=wGf9JBotDkSygFj43m49oiKlFnpMnn97keiZKF-40vE,2450
|
|
51
|
+
posthoganalytics-6.7.4.dist-info/METADATA,sha256=krO8eciyydNJ2LzkNv59_maYsYxnnT4XpvZ_n6FCi44,6024
|
|
52
|
+
posthoganalytics-6.7.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
53
|
+
posthoganalytics-6.7.4.dist-info/top_level.txt,sha256=8QsNIqIkBh1p2TXvKp0Em9ZLZKwe3uIqCETyW4s1GOE,17
|
|
54
|
+
posthoganalytics-6.7.4.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|