Skip to content

Commit 5b287e0

Browse files
committed
fi9x: isort + black
1 parent e0cd9c1 commit 5b287e0

File tree

6 files changed

+34
-22
lines changed

6 files changed

+34
-22
lines changed

posthog/ai/anthropic/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
from .anthropic import Anthropic
22
from .anthropic_async import AsyncAnthropic
3-
from .anthropic_providers import AnthropicBedrock, AsyncAnthropicBedrock, AnthropicVertex, AsyncAnthropicVertex
3+
from .anthropic_providers import AnthropicBedrock, AnthropicVertex, AsyncAnthropicBedrock, AsyncAnthropicVertex
44

5-
__all__ = ["Anthropic", "AsyncAnthropic", "AnthropicBedrock", "AsyncAnthropicBedrock", "AnthropicVertex", "AsyncAnthropicVertex"]
5+
__all__ = [
6+
"Anthropic",
7+
"AsyncAnthropic",
8+
"AnthropicBedrock",
9+
"AsyncAnthropicBedrock",
10+
"AnthropicVertex",
11+
"AsyncAnthropicVertex",
12+
]

posthog/ai/anthropic/anthropic.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
except ImportError:
55
raise ModuleNotFoundError("Please install the Anthropic SDK to use this feature: 'pip install anthropic'")
66

7+
import time
8+
import uuid
9+
from typing import Any, Dict, Optional
10+
711
from posthog.ai.utils import call_llm_and_track_usage, get_model_params, with_privacy_mode
812
from posthog.client import Client as PostHogClient
9-
from typing import Any, Dict, Optional
10-
import uuid
11-
import time
1213

1314

1415
class Anthropic(anthropic.Anthropic):
@@ -43,7 +44,7 @@ def create(
4344
):
4445
"""
4546
Create a message using Anthropic's API while tracking usage in PostHog.
46-
47+
4748
Args:
4849
posthog_distinct_id: Optional ID to associate with the usage event
4950
posthog_trace_id: Optional trace UUID for linking events
@@ -77,7 +78,7 @@ def create(
7778
super().create,
7879
**kwargs,
7980
)
80-
81+
8182
def stream(
8283
self,
8384
posthog_distinct_id: Optional[str] = None,
@@ -89,7 +90,7 @@ def stream(
8990
):
9091
if posthog_trace_id is None:
9192
posthog_trace_id = uuid.uuid4()
92-
93+
9394
return self._create_streaming(
9495
posthog_distinct_id,
9596
posthog_trace_id,
@@ -136,7 +137,7 @@ def generator():
136137
end_time = time.time()
137138
latency = end_time - start_time
138139
output = "".join(accumulated_content)
139-
140+
140141
self._capture_streaming_event(
141142
posthog_distinct_id,
142143
posthog_trace_id,
@@ -195,4 +196,3 @@ def _capture_streaming_event(
195196
properties=event_properties,
196197
groups=posthog_groups,
197198
)
198-

posthog/ai/anthropic/anthropic_async.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
except ImportError:
55
raise ModuleNotFoundError("Please install the Anthropic SDK to use this feature: 'pip install anthropic'")
66

7+
import time
8+
import uuid
9+
from typing import Any, Dict, Optional
10+
711
from posthog.ai.utils import call_llm_and_track_usage_async, get_model_params, with_privacy_mode
812
from posthog.client import Client as PostHogClient
9-
from typing import Any, Dict, Optional
10-
import uuid
11-
import time
1213

1314

1415
class AsyncAnthropic(anthropic.AsyncAnthropic):
@@ -43,7 +44,7 @@ async def create(
4344
):
4445
"""
4546
Create a message using Anthropic's API while tracking usage in PostHog.
46-
47+
4748
Args:
4849
posthog_distinct_id: Optional ID to associate with the usage event
4950
posthog_trace_id: Optional trace UUID for linking events
@@ -77,7 +78,7 @@ async def create(
7778
super().create,
7879
**kwargs,
7980
)
80-
81+
8182
async def stream(
8283
self,
8384
posthog_distinct_id: Optional[str] = None,
@@ -89,7 +90,7 @@ async def stream(
8990
):
9091
if posthog_trace_id is None:
9192
posthog_trace_id = uuid.uuid4()
92-
93+
9394
return await self._create_streaming(
9495
posthog_distinct_id,
9596
posthog_trace_id,
@@ -136,7 +137,7 @@ async def generator():
136137
end_time = time.time()
137138
latency = end_time - start_time
138139
output = "".join(accumulated_content)
139-
140+
140141
await self._capture_streaming_event(
141142
posthog_distinct_id,
142143
posthog_trace_id,
@@ -195,4 +196,3 @@ async def _capture_streaming_event(
195196
properties=event_properties,
196197
groups=posthog_groups,
197198
)
198-

posthog/ai/anthropic/anthropic_providers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from posthog.ai.anthropic.anthropic_async import AsyncWrappedMessages
88
from posthog.client import Client as PostHogClient
99

10+
1011
class AnthropicBedrock(anthropic.AnthropicBedrock):
1112
"""
1213
A wrapper around the Anthropic Bedrock SDK that automatically sends LLM usage events to PostHog.

posthog/ai/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ def call_llm_and_track_usage(
127127
"$ai_model": kwargs.get("model"),
128128
"$ai_model_parameters": get_model_params(kwargs),
129129
"$ai_input": with_privacy_mode(ph_client, posthog_privacy_mode, kwargs.get("messages")),
130-
"$ai_output_choices": with_privacy_mode(ph_client, posthog_privacy_mode, format_response(response, provider)),
130+
"$ai_output_choices": with_privacy_mode(
131+
ph_client, posthog_privacy_mode, format_response(response, provider)
132+
),
131133
"$ai_http_status": http_status,
132134
"$ai_input_tokens": usage.get("input_tokens", 0),
133135
"$ai_output_tokens": usage.get("output_tokens", 0),
@@ -193,7 +195,9 @@ async def call_llm_and_track_usage_async(
193195
"$ai_model": kwargs.get("model"),
194196
"$ai_model_parameters": get_model_params(kwargs),
195197
"$ai_input": with_privacy_mode(ph_client, posthog_privacy_mode, kwargs.get("messages")),
196-
"$ai_output_choices": with_privacy_mode(ph_client, posthog_privacy_mode, format_response(response, provider)),
198+
"$ai_output_choices": with_privacy_mode(
199+
ph_client, posthog_privacy_mode, format_response(response, provider)
200+
),
197201
"$ai_http_status": http_status,
198202
"$ai_input_tokens": usage.get("input_tokens", 0),
199203
"$ai_output_tokens": usage.get("output_tokens", 0),

posthog/test/ai/anthropic/test_anthropic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def test_streaming(mock_client, mock_anthropic_stream):
9898
assert chunks[0].content == "A"
9999
assert chunks[1].content == "B"
100100
assert chunks[2].content == "C"
101-
101+
102102
# Wait a bit to ensure the capture is called
103103
time.sleep(0.1)
104104
assert mock_client.capture.call_count == 1
@@ -134,7 +134,7 @@ def test_streaming_with_stream_endpoint(mock_client, mock_anthropic_stream):
134134
assert chunks[0].content == "A"
135135
assert chunks[1].content == "B"
136136
assert chunks[2].content == "C"
137-
137+
138138
# Wait a bit to ensure the capture is called
139139
time.sleep(0.1)
140140
assert mock_client.capture.call_count == 1

0 commit comments

Comments
 (0)