Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/example-ai-autogen/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POSTHOG_API_KEY=phc_your_project_api_key
POSTHOG_HOST=https://us.i.posthog.com
OPENAI_API_KEY=sk-your_api_key
22 changes: 22 additions & 0 deletions examples/example-ai-autogen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# AutoGen + PostHog AI Examples

Track AutoGen agent LLM calls with PostHog.

## Setup

```bash
cp .env.example .env
# Fill in your API keys in .env
uv sync
```

## Examples

- **agent.py** - AutoGen agent with PostHog tracking via OpenAI wrapper

## Run

```bash
source .env
uv run python agent.py
```
31 changes: 31 additions & 0 deletions examples/example-ai-autogen/agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""AutoGen with PostHog tracking via OpenAI wrapper."""

import os
import asyncio
from posthog import Posthog
from posthog.ai.openai import OpenAI
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient

posthog = Posthog(
os.environ["POSTHOG_API_KEY"],
host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"),
)
openai_client = OpenAI(api_key=os.environ["OPENAI_API_KEY"], posthog_client=posthog)

model_client = OpenAIChatCompletionClient(
model="gpt-4o-mini",
openai_client=openai_client,
)

agent = AssistantAgent("assistant", model_client=model_client)


async def main():
result = await agent.run(task="Tell me a fun fact about hedgehogs.")
print(result)
await model_client.close()


asyncio.run(main())
posthog.shutdown()
9 changes: 9 additions & 0 deletions examples/example-ai-autogen/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
name = "example-ai-autogen"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = [
"posthog==7.9.12",
"autogen-agentchat>=0.4.0",
"autogen-ext[openai]>=0.4.0",
]
993 changes: 993 additions & 0 deletions examples/example-ai-autogen/uv.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/example-ai-autogen/uv.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exclude-newer = "7 days"
5 changes: 5 additions & 0 deletions examples/example-ai-aws-bedrock/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
POSTHOG_API_KEY=phc_your_project_api_key
POSTHOG_HOST=https://us.i.posthog.com
AWS_REGION=us-east-1
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
22 changes: 22 additions & 0 deletions examples/example-ai-aws-bedrock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# AWS Bedrock + PostHog AI Examples

Track AWS Bedrock LLM calls with PostHog via OpenTelemetry instrumentation.

## Setup

```bash
cp .env.example .env
# Fill in your API keys in .env
uv sync
```

## Examples

- **chat.py** - Bedrock Converse API with OpenTelemetry tracing to PostHog

## Run

```bash
source .env
uv run python chat.py
```
43 changes: 43 additions & 0 deletions examples/example-ai-aws-bedrock/chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""AWS Bedrock chat with OpenTelemetry instrumentation, tracked by PostHog."""

import os
import boto3
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.sdk.resources import Resource, SERVICE_NAME
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.botocore import BotocoreInstrumentor

resource = Resource(attributes={SERVICE_NAME: "example-bedrock-app"})

exporter = OTLPSpanExporter(
endpoint=f"{os.environ.get('POSTHOG_HOST', 'https://us.i.posthog.com')}/i/v0/ai/otel",
headers={"Authorization": f"Bearer {os.environ['POSTHOG_API_KEY']}"},
)

provider = TracerProvider(resource=resource)
provider.add_span_processor(BatchSpanProcessor(exporter))
trace.set_tracer_provider(provider)

BotocoreInstrumentor().instrument()

client = boto3.client(
"bedrock-runtime",
region_name=os.environ.get("AWS_REGION", "us-east-1"),
)

response = client.converse(
modelId="openai.gpt-oss-20b-1:0",
messages=[
{
"role": "user",
"content": [{"text": "Tell me a fun fact about hedgehogs."}],
}
],
)

for block in response["output"]["message"]["content"]:
if "text" in block:
print(block["text"])
break
11 changes: 11 additions & 0 deletions examples/example-ai-aws-bedrock/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]
name = "example-ai-aws-bedrock"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = [
"posthog==7.9.12",
"boto3>=1.35.0",
"opentelemetry-instrumentation-botocore>=0.49b0",
"opentelemetry-sdk>=1.30.0",
"opentelemetry-exporter-otlp-proto-http>=1.30.0",
]
539 changes: 539 additions & 0 deletions examples/example-ai-aws-bedrock/uv.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/example-ai-aws-bedrock/uv.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exclude-newer = "7 days"
4 changes: 4 additions & 0 deletions examples/example-ai-azure-openai/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POSTHOG_API_KEY=phc_your_project_api_key
POSTHOG_HOST=https://us.i.posthog.com
AZURE_OPENAI_API_KEY=your_azure_api_key
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com
22 changes: 22 additions & 0 deletions examples/example-ai-azure-openai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Azure OpenAI + PostHog AI Examples

Track Azure OpenAI API calls with PostHog.

## Setup

```bash
cp .env.example .env
# Fill in your API keys in .env
uv sync
```

## Examples

- **chat.py** - Chat completions via Azure OpenAI

## Run

```bash
source .env
uv run python chat.py
```
28 changes: 28 additions & 0 deletions examples/example-ai-azure-openai/chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Azure OpenAI chat completions, tracked by PostHog."""

import os
from posthog import Posthog
from posthog.ai.openai import AzureOpenAI

posthog = Posthog(
os.environ["POSTHOG_API_KEY"],
host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"),
)
client = AzureOpenAI(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
api_version="2024-10-21",
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
posthog_client=posthog,
)

response = client.chat.completions.create(
model="gpt-4o",
max_completion_tokens=1024,
posthog_distinct_id="example-user",
messages=[
{"role": "user", "content": "Tell me a fun fact about hedgehogs."},
],
)

print(response.choices[0].message.content)
posthog.shutdown()
8 changes: 8 additions & 0 deletions examples/example-ai-azure-openai/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
name = "example-ai-azure-openai"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = [
"posthog==7.9.12",
"openai==2.29.0",
]
Loading
Loading