Skip to content

Commit 84ab551

Browse files
committed
fix(llma): anthropic streaming
1 parent 1bcec76 commit 84ab551

File tree

1 file changed

+30
-67
lines changed

1 file changed

+30
-67
lines changed

posthog/ai/anthropic/anthropic_async.py

Lines changed: 30 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,9 @@
1414
from posthog.ai.types import StreamingContentBlock, TokenUsage, ToolInProgress
1515
from posthog.ai.utils import (
1616
call_llm_and_track_usage_async,
17-
extract_available_tool_calls,
18-
get_model_params,
19-
merge_system_prompt,
2017
merge_usage_stats,
21-
with_privacy_mode,
2218
)
2319
from posthog.ai.anthropic.anthropic_converter import (
24-
format_anthropic_streaming_content,
2520
extract_anthropic_usage_from_event,
2621
handle_anthropic_content_block_start,
2722
handle_anthropic_text_delta,
@@ -220,66 +215,34 @@ async def _capture_streaming_event(
220215
content_blocks: List[StreamingContentBlock],
221216
accumulated_content: str,
222217
):
223-
if posthog_trace_id is None:
224-
posthog_trace_id = str(uuid.uuid4())
225-
226-
# Format output using converter
227-
formatted_content = format_anthropic_streaming_content(content_blocks)
228-
formatted_output = []
229-
230-
if formatted_content:
231-
formatted_output = [{"role": "assistant", "content": formatted_content}]
232-
else:
233-
# Fallback to accumulated content if no blocks
234-
formatted_output = [
235-
{
236-
"role": "assistant",
237-
"content": [{"type": "text", "text": accumulated_content}],
238-
}
239-
]
240-
241-
event_properties = {
242-
"$ai_provider": "anthropic",
243-
"$ai_model": kwargs.get("model"),
244-
"$ai_model_parameters": get_model_params(kwargs),
245-
"$ai_input": with_privacy_mode(
246-
self._client._ph_client,
247-
posthog_privacy_mode,
248-
sanitize_anthropic(merge_system_prompt(kwargs, "anthropic")),
249-
),
250-
"$ai_output_choices": with_privacy_mode(
251-
self._client._ph_client,
252-
posthog_privacy_mode,
253-
formatted_output,
254-
),
255-
"$ai_http_status": 200,
256-
"$ai_input_tokens": usage_stats.get("input_tokens", 0),
257-
"$ai_output_tokens": usage_stats.get("output_tokens", 0),
258-
"$ai_cache_read_input_tokens": usage_stats.get(
259-
"cache_read_input_tokens", 0
260-
),
261-
"$ai_cache_creation_input_tokens": usage_stats.get(
262-
"cache_creation_input_tokens", 0
218+
from posthog.ai.types import StreamingEventData
219+
from posthog.ai.anthropic.anthropic_converter import (
220+
format_anthropic_streaming_input,
221+
format_anthropic_streaming_output_complete,
222+
)
223+
from posthog.ai.utils import capture_streaming_event
224+
225+
# Prepare standardized event data
226+
formatted_input = format_anthropic_streaming_input(kwargs)
227+
sanitized_input = sanitize_anthropic(formatted_input)
228+
229+
event_data = StreamingEventData(
230+
provider="anthropic",
231+
model=kwargs.get("model", "unknown"),
232+
base_url=str(self._client.base_url),
233+
kwargs=kwargs,
234+
formatted_input=sanitized_input,
235+
formatted_output=format_anthropic_streaming_output_complete(
236+
content_blocks, accumulated_content
263237
),
264-
"$ai_latency": latency,
265-
"$ai_trace_id": posthog_trace_id,
266-
"$ai_base_url": str(self._client.base_url),
267-
**(posthog_properties or {}),
268-
}
269-
270-
# Add tools if available
271-
available_tools = extract_available_tool_calls("anthropic", kwargs)
272-
273-
if available_tools:
274-
event_properties["$ai_tools"] = available_tools
275-
276-
if posthog_distinct_id is None:
277-
event_properties["$process_person_profile"] = False
278-
279-
if hasattr(self._client._ph_client, "capture"):
280-
self._client._ph_client.capture(
281-
distinct_id=posthog_distinct_id or posthog_trace_id,
282-
event="$ai_generation",
283-
properties=event_properties,
284-
groups=posthog_groups,
285-
)
238+
usage_stats=usage_stats,
239+
latency=latency,
240+
distinct_id=posthog_distinct_id,
241+
trace_id=posthog_trace_id,
242+
properties=posthog_properties,
243+
privacy_mode=posthog_privacy_mode,
244+
groups=posthog_groups,
245+
)
246+
247+
# Use the common capture function
248+
capture_streaming_event(self._client._ph_client, event_data)

0 commit comments

Comments
 (0)