Skip to content

Commit b964323

Browse files
style: apply ruff formatting to AI examples
1 parent cf5cd6f commit b964323

22 files changed

Lines changed: 134 additions & 34 deletions

File tree

examples/example-ai-anthropic/chat.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
from posthog import Posthog
77
from posthog.ai.anthropic import Anthropic
88

9-
posthog = Posthog(os.environ["POSTHOG_API_KEY"], host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"))
9+
posthog = Posthog(
10+
os.environ["POSTHOG_API_KEY"],
11+
host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"),
12+
)
1013
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"], posthog_client=posthog)
1114

1215
tools = [

examples/example-ai-anthropic/extended_thinking.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@
77
from posthog import Posthog
88
from posthog.ai.anthropic import Anthropic
99

10-
posthog = Posthog(os.environ["POSTHOG_API_KEY"], host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"))
10+
posthog = Posthog(
11+
os.environ["POSTHOG_API_KEY"],
12+
host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"),
13+
)
1114
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"], posthog_client=posthog)
1215

1316
message = client.messages.create(
1417
model="claude-sonnet-4-5-20250929",
1518
max_tokens=16000,
1619
posthog_distinct_id="example-user",
1720
thinking={"type": "enabled", "budget_tokens": 10000},
18-
messages=[{"role": "user", "content": "What is the probability of rolling at least one six in four rolls of a fair die?"}],
21+
messages=[
22+
{
23+
"role": "user",
24+
"content": "What is the probability of rolling at least one six in four rolls of a fair die?",
25+
}
26+
],
1927
)
2028

2129
for block in message.content:

examples/example-ai-anthropic/streaming.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
from posthog import Posthog
55
from posthog.ai.anthropic import Anthropic
66

7-
posthog = Posthog(os.environ["POSTHOG_API_KEY"], host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"))
7+
posthog = Posthog(
8+
os.environ["POSTHOG_API_KEY"],
9+
host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"),
10+
)
811
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"], posthog_client=posthog)
912

1013
stream = client.messages.create(

examples/example-ai-gemini/chat.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
from posthog import Posthog
88
from posthog.ai.gemini import Client
99

10-
posthog = Posthog(os.environ["POSTHOG_API_KEY"], host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"))
10+
posthog = Posthog(
11+
os.environ["POSTHOG_API_KEY"],
12+
host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"),
13+
)
1114
client = Client(api_key=os.environ["GEMINI_API_KEY"], posthog_client=posthog)
1215

1316
tool_declarations = [

examples/example-ai-gemini/image_generation.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
from posthog import Posthog
55
from posthog.ai.gemini import Client
66

7-
posthog = Posthog(os.environ["POSTHOG_API_KEY"], host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"))
7+
posthog = Posthog(
8+
os.environ["POSTHOG_API_KEY"],
9+
host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"),
10+
)
811
client = Client(api_key=os.environ["GEMINI_API_KEY"], posthog_client=posthog)
912

1013
response = client.models.generate_content(
@@ -16,7 +19,9 @@
1619
for candidate in response.candidates:
1720
for part in candidate.content.parts:
1821
if hasattr(part, "inline_data") and part.inline_data:
19-
print(f"Generated image: {part.inline_data.mime_type}, {len(part.inline_data.data)} bytes")
22+
print(
23+
f"Generated image: {part.inline_data.mime_type}, {len(part.inline_data.data)} bytes"
24+
)
2025
elif hasattr(part, "text"):
2126
print(part.text)
2227

examples/example-ai-gemini/streaming.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44
from posthog import Posthog
55
from posthog.ai.gemini import Client
66

7-
posthog = Posthog(os.environ["POSTHOG_API_KEY"], host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"))
7+
posthog = Posthog(
8+
os.environ["POSTHOG_API_KEY"],
9+
host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"),
10+
)
811
client = Client(api_key=os.environ["GEMINI_API_KEY"], posthog_client=posthog)
912

1013
stream = client.models.generate_content_stream(
1114
model="gemini-2.5-flash",
1215
posthog_distinct_id="example-user",
13-
contents=[{"role": "user", "parts": [{"text": "Explain product analytics in three sentences."}]}],
16+
contents=[
17+
{
18+
"role": "user",
19+
"parts": [{"text": "Explain product analytics in three sentences."}],
20+
}
21+
],
1422
)
1523

1624
for chunk in stream:

examples/example-ai-langchain/callback_handler.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
import urllib.request
66
from langchain_openai import ChatOpenAI
77
from langchain_core.tools import tool
8-
from langchain_core.messages import HumanMessage, ToolMessage
8+
from langchain_core.messages import HumanMessage
99
from posthog import Posthog
1010
from posthog.ai.langchain import CallbackHandler
1111

12-
posthog = Posthog(os.environ["POSTHOG_API_KEY"], host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"))
12+
posthog = Posthog(
13+
os.environ["POSTHOG_API_KEY"],
14+
host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"),
15+
)
1316
callback_handler = CallbackHandler(client=posthog)
1417

1518

examples/example-ai-langchain/otel.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Bearer {posthog_api_key}"
1818

1919
tracer_provider = TracerProvider(
20-
resource=Resource.create({"service.name": "langchain-example", "user.id": "example-user"})
20+
resource=Resource.create(
21+
{"service.name": "langchain-example", "user.id": "example-user"}
22+
)
2123
)
2224
tracer_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
2325
trace.set_tracer_provider(tracer_provider)

examples/example-ai-litellm/chat.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
# Enable PostHog callbacks — LiteLLM has built-in PostHog support
99
os.environ["POSTHOG_API_KEY"] = os.environ.get("POSTHOG_API_KEY", "")
10-
os.environ["POSTHOG_API_URL"] = os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com")
10+
os.environ["POSTHOG_API_URL"] = os.environ.get(
11+
"POSTHOG_HOST", "https://us.i.posthog.com"
12+
)
1113
litellm.success_callback = ["posthog"]
1214
litellm.failure_callback = ["posthog"]
1315

@@ -43,7 +45,10 @@ def get_weather(latitude: float, longitude: float, location_name: str) -> str:
4345
response = litellm.completion(
4446
model="gpt-4o-mini",
4547
messages=[
46-
{"role": "system", "content": "You are a helpful assistant with access to weather data."},
48+
{
49+
"role": "system",
50+
"content": "You are a helpful assistant with access to weather data.",
51+
},
4752
{"role": "user", "content": "What's the weather in Paris?"},
4853
],
4954
tools=tools,

examples/example-ai-litellm/streaming.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import litellm
55

66
os.environ["POSTHOG_API_KEY"] = os.environ.get("POSTHOG_API_KEY", "")
7-
os.environ["POSTHOG_API_URL"] = os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com")
7+
os.environ["POSTHOG_API_URL"] = os.environ.get(
8+
"POSTHOG_HOST", "https://us.i.posthog.com"
9+
)
810
litellm.success_callback = ["posthog"]
911
litellm.failure_callback = ["posthog"]
1012

0 commit comments

Comments
 (0)