-
Notifications
You must be signed in to change notification settings - Fork 66
feat(examples): add Dedalus Labs and Cloudflare AI Gateway examples #508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8f15aa2
feat(examples): add Dedalus Labs and Cloudflare AI Gateway examples
richardsolomou 9c5fab0
docs: include POSTHOG_API_KEY in Cloudflare example README
richardsolomou b54d0fc
fix(examples): use cf-aig-authorization header for Cloudflare AI Gate…
richardsolomou d9c8e7e
fix(examples): update Cloudflare .env.example to use CF_AIG env vars
richardsolomou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| POSTHOG_API_KEY=phc_your_project_api_key | ||
| POSTHOG_HOST=https://us.i.posthog.com | ||
| OPENAI_API_KEY=your_openai_api_key | ||
| CF_AIG_TOKEN=your_cloudflare_ai_gateway_token | ||
| CF_AIG_ACCOUNT_ID=your_cloudflare_account_id | ||
| CF_AIG_GATEWAY_ID=your_gateway_id |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Cloudflare AI Gateway + PostHog AI Examples | ||
|
|
||
| Track Cloudflare AI Gateway API calls with PostHog via the OpenAI-compatible unified endpoint. | ||
|
|
||
| ## Setup | ||
|
|
||
| ```bash | ||
| cp .env.example .env | ||
| # Fill in your API keys in .env | ||
| # Install uv if you haven't already: https://docs.astral.sh/uv/getting-started/installation/ | ||
| uv sync | ||
| ``` | ||
|
|
||
| `POSTHOG_API_KEY`, `OPENAI_API_KEY`, `CF_AIG_TOKEN`, `CF_AIG_ACCOUNT_ID`, and `CF_AIG_GATEWAY_ID` are required. `CF_AIG_TOKEN` is your Cloudflare AI Gateway API token, passed via the `cf-aig-authorization` header. | ||
|
|
||
| ## Examples | ||
|
|
||
| - **chat.py** - Chat completions via Cloudflare AI Gateway | ||
|
|
||
| ## Run | ||
|
|
||
| ```bash | ||
| source .env | ||
| uv run python chat.py | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| """Cloudflare AI Gateway chat completions via OpenAI-compatible API, tracked by PostHog via OpenTelemetry.""" | ||
|
|
||
| import os | ||
| from opentelemetry import trace | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.resources import Resource, SERVICE_NAME | ||
| from posthog.ai.otel import PostHogSpanProcessor | ||
| from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor | ||
|
|
||
| resource = Resource( | ||
| attributes={ | ||
| SERVICE_NAME: "example-cloudflare-ai-gateway-app", | ||
| "posthog.distinct_id": "example-user", | ||
| "foo": "bar", | ||
| "conversation_id": "abc-123", | ||
| } | ||
| ) | ||
| provider = TracerProvider(resource=resource) | ||
| provider.add_span_processor( | ||
| PostHogSpanProcessor( | ||
| api_key=os.environ["POSTHOG_API_KEY"], | ||
| host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"), | ||
| ) | ||
| ) | ||
| trace.set_tracer_provider(provider) | ||
|
|
||
| OpenAIInstrumentor().instrument() | ||
|
|
||
| import openai # noqa: E402 | ||
|
|
||
| client = openai.OpenAI( | ||
| api_key=os.environ["OPENAI_API_KEY"], | ||
| default_headers={ | ||
| "cf-aig-authorization": f"Bearer {os.environ['CF_AIG_TOKEN']}", | ||
| }, | ||
| base_url=f"https://gateway.ai.cloudflare.com/v1/{os.environ['CF_AIG_ACCOUNT_ID']}/{os.environ['CF_AIG_GATEWAY_ID']}/compat", | ||
| ) | ||
|
|
||
| response = client.chat.completions.create( | ||
| model="openai/gpt-5-mini", | ||
| max_completion_tokens=1024, | ||
| messages=[ | ||
| {"role": "user", "content": "Tell me a fun fact about hedgehogs."}, | ||
| ], | ||
| ) | ||
|
|
||
| print(response.choices[0].message.content) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [project] | ||
| name = "example-ai-cloudflare-ai-gateway" | ||
| version = "0.1.0" | ||
| requires-python = ">=3.10" | ||
| dependencies = [ | ||
| "openai>=1.0.0", | ||
| "opentelemetry-instrumentation-openai-v2>=2.0b0", | ||
| "opentelemetry-sdk>=1.30.0", | ||
| "posthog[otel]>=7.11.0", | ||
| ] | ||
|
|
||
| [tool.uv.sources] | ||
| posthog = { path = "../.." } |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| exclude-newer = "7 days" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| DEDALUS_API_KEY=your_api_key |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Dedalus Labs + PostHog AI Examples | ||
|
|
||
| Track Dedalus Labs API calls with PostHog via the OpenAI-compatible API. | ||
|
|
||
| ## Setup | ||
|
|
||
| ```bash | ||
| cp .env.example .env | ||
| # Fill in your API keys in .env | ||
| # Install uv if you haven't already: https://docs.astral.sh/uv/getting-started/installation/ | ||
| uv sync | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| - **chat.py** - Chat completions via Dedalus Labs | ||
|
|
||
| ## Run | ||
|
|
||
| ```bash | ||
| source .env | ||
| uv run python chat.py | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| """Dedalus Labs chat completions via OpenAI-compatible API, tracked by PostHog via OpenTelemetry.""" | ||
|
|
||
| import os | ||
| from opentelemetry import trace | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.resources import Resource, SERVICE_NAME | ||
| from posthog.ai.otel import PostHogSpanProcessor | ||
| from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor | ||
|
|
||
| resource = Resource( | ||
| attributes={ | ||
| SERVICE_NAME: "example-dedalus-app", | ||
| "posthog.distinct_id": "example-user", | ||
| "foo": "bar", | ||
| "conversation_id": "abc-123", | ||
| } | ||
| ) | ||
| provider = TracerProvider(resource=resource) | ||
| provider.add_span_processor( | ||
| PostHogSpanProcessor( | ||
| api_key=os.environ["POSTHOG_API_KEY"], | ||
| host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"), | ||
| ) | ||
| ) | ||
| trace.set_tracer_provider(provider) | ||
|
|
||
| OpenAIInstrumentor().instrument() | ||
|
|
||
| import openai # noqa: E402 | ||
|
|
||
| client = openai.OpenAI( | ||
| base_url="https://api.dedaluslabs.ai/v1", | ||
| api_key=os.environ["DEDALUS_API_KEY"], | ||
| ) | ||
|
|
||
| response = client.chat.completions.create( | ||
| model="openai/gpt-5-mini", | ||
| max_completion_tokens=1024, | ||
| messages=[ | ||
| {"role": "user", "content": "Tell me a fun fact about hedgehogs."}, | ||
| ], | ||
| ) | ||
|
|
||
| print(response.choices[0].message.content) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [project] | ||
| name = "example-ai-dedalus" | ||
| version = "0.1.0" | ||
| requires-python = ">=3.10" | ||
| dependencies = [ | ||
| "openai>=1.0.0", | ||
| "opentelemetry-instrumentation-openai-v2>=2.0b0", | ||
| "opentelemetry-sdk>=1.30.0", | ||
| "posthog[otel]>=7.11.0", | ||
| ] | ||
|
|
||
| [tool.uv.sources] | ||
| posthog = { path = "../.." } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.