Skip to content

Commit 916a13e

Browse files
tracisiebeldevin-ai-integration[bot]claude
authored
docs: add per-example READMEs with .env support (#25)
## Summary Restructures the repository so each example is self-contained in its own directory with a dedicated README, `pyproject.toml`, and source files. Adds `.env` file support via `python-dotenv`. ## Review & Testing Checklist for Human - [ ] `cd` into an example directory (e.g. `examples/openai`) and run `poetry install && poetry run openai-example` to confirm the self-contained setup works - [ ] Verify each example README has correct env vars, install, and run commands - [ ] Confirm main README table links resolve to the correct example READMEs ### Notes - Root `pyproject.toml` deleted; CI updated to loop through each example directory with `poetry -C "$dir" install` - Bedrock example now reads `AWS_DEFAULT_REGION` from env with `us-east-1` fallback - `.env` added to `.gitignore` Link to Devin session: https://app.devin.ai/sessions/26e40d921a3d4e42a01faa3068908c23 Requested by: @tracisiebel --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c24a683 commit 916a13e

29 files changed

Lines changed: 584 additions & 140 deletions

.github/workflows/ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ jobs:
3131
uses: abatilo/actions-poetry@7b6d33e44b4f08d7021a1dee3c044e9c253d6439
3232

3333
- name: Install dependencies
34-
run: poetry install --all-extras
34+
run: |
35+
for dir in examples/*/; do
36+
echo "Installing $dir"
37+
poetry -C "$dir" install
38+
done

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.python-version
22
poetry.lock
33
__pycache__/
4+
.env

README.md

Lines changed: 15 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -8,106 +8,31 @@ This demo requires Python 3.10 or higher.
88

99
## Build Instructions
1010

11-
This repository includes examples for `OpenAI`, `Bedrock`, and `LangChain` for multi-provider support. Depending on your preferred provider, you may have to take some additional steps.
11+
This repository includes examples for `OpenAI`, `Bedrock`, `Gemini`, `LangChain`, `LangGraph`, `Judge`, and `Observability`. Depending on your preferred provider, you may have to take some additional steps.
1212

1313
### General setup
1414

1515
1. [Create an AI Config](https://launchdarkly.com/docs/home/ai-configs/create) using the key specified in each example, or copy the key of existing AI Config in your LaunchDarkly project that you want to evaluate.
16-
1. Set the environment variable `LAUNCHDARKLY_SDK_KEY` to your LaunchDarkly SDK key and `LAUNCHDARKLY_AI_CONFIG_KEY` to the AI Config key; otherwise, an AI Config of `sample-ai-config` or `sample-ai-agent-config` will be assumed for most examples.
17-
18-
```bash
19-
export LAUNCHDARKLY_SDK_KEY="1234567890abcdef"
20-
export LAUNCHDARKLY_AI_CONFIG_KEY="sample-ai-config"
21-
```
2216

2317
1. Ensure you have [Poetry](https://python-poetry.org/) installed.
2418

25-
### Provider-Specific Setup
26-
27-
#### OpenAI setup (single provider)
28-
29-
1. Install the required dependencies with `poetry install -E openai` or `poetry install --all-extras`.
30-
1. Set the environment variable `OPENAI_API_KEY` to your OpenAI key.
31-
1. On the command line, run `poetry run openai-example`.
32-
33-
#### Chat with observability (observability plugin example)
34-
35-
This example demonstrates how to use the LaunchDarkly observability SDK plugin to monitor AI chat operations. For more details, see the [Python SDK observability reference](https://launchdarkly.com/docs/sdk/observability/python).
19+
1. Create a `.env` file in the repository root with at least your LaunchDarkly SDK key:
3620

37-
1. Install the required dependencies with `poetry install -E observability` or `poetry install --all-extras`.
38-
1. Set the environment variable for your AI provider (e.g., `OPENAI_API_KEY`), or configure your AI Config to use a different provider.
39-
1. Optionally, set service identification:
40-
```bash
41-
export SERVICE_NAME="my-ai-service"
42-
export SERVICE_VERSION="1.0.0"
4321
```
44-
1. On the command line, run `poetry run chat-observability-example`.
45-
46-
The observability plugin automatically captures and sends data to LaunchDarkly:
47-
- **Observability tab**: SDK operations, flag evaluations, error monitoring, logging, and distributed tracing
48-
- **AI Config Monitoring tab**: Token usage, duration, success/error rates, and custom metadata for filtering and analysis
49-
50-
View your data in the LaunchDarkly dashboard under **Observability** tabs.
51-
52-
#### Bedrock setup (single provider)
53-
54-
1. Install the required dependencies with `poetry install -E bedrock` or `poetry install --all-extras`.
55-
1. Ensure the required AWS credentials can be [auto-detected by the `boto3` library](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html). Examples might include environment variables, role providers, or shared credential files.
56-
1. On the command line, run `poetry run bedrock-example`.
57-
58-
#### Gemini setup (single provider)
59-
60-
1. Install the required dependencies with `poetry install -E gemini` or `poetry install --all-extras`.
61-
1. Set the environment variable `GOOGLE_API_KEY` to your Google API key.
62-
1. On the command line, run `poetry run gemini-example`.
63-
64-
#### LangChain setup (multiple providers)
65-
66-
This example uses `OpenAI`, `Bedrock`, and `Gemini` LangChain provider packages. You can add additional LangChain providers using the `poetry add` command.
67-
68-
1. Install all dependencies with `poetry install -E langchain` or `poetry install --all-extras`.
69-
1. Set up API keys for the providers you want to use.
70-
1. On the command line, run `poetry run langchain-example`
71-
72-
#### LangGraph setup (multiple providers, single agent)
73-
74-
1. Install all dependencies with `poetry install -E langgraph` or `poetry install --all-extras`.
75-
1. Set up API keys for the providers you want to use.
76-
1. Optionally set this environment variable to use a different agent config:
77-
```bash
78-
export LAUNCHDARKLY_AGENT_CONFIG_KEY="sample-ai-agent-config"
22+
LAUNCHDARKLY_SDK_KEY=your-launchdarkly-sdk-key
7923
```
80-
1. On the command line, run `poetry run langgraph-agent-example`.
81-
82-
#### LangGraph setup (multiple providers, multiple agents)
8324

84-
1. Install all dependencies with `poetry install -E langgraph` or `poetry install --all-extras`.
85-
1. Set up API keys for the providers you want to use.
86-
1. [Create an AI Config (Agent-based)](https://launchdarkly.com/docs/home/ai-configs/agents) using the keys below. Write a goal for each config and enable it with targeting rules.
87-
1. Optionally set these environment variables to use different agent configs:
88-
```bash
89-
export LAUNCHDARKLY_ANALYZER_CONFIG_KEY="code-review-analyzer"
90-
export LAUNCHDARKLY_DOCUMENTATION_CONFIG_KEY="code-review-documentation"
91-
```
92-
1. On the command line, run `poetry run langgraph-multi-agent-example`.
93-
94-
#### Judge setup (judge evaluation)
95-
96-
These examples demonstrate how to use LaunchDarkly's judge functionality to evaluate AI responses for accuracy, relevance, and other metrics.
97-
98-
1. Install dependencies with `poetry install -E langchain` or `poetry install --all-extras`.
99-
1. Set up API keys for the provider you want to use (OpenAI, Bedrock, or Gemini).
100-
1. [Create an AI Config](https://launchdarkly.com/docs/home/ai-configs/create) for chat functionality.
101-
1. [Create a Judge Config](https://launchdarkly.com/docs/home/ai-configs/judges) for evaluation.
102-
1. Set the required environment variables:
103-
```bash
104-
export LAUNCHDARKLY_SDK_KEY="your-sdk-key"
105-
export LAUNCHDARKLY_AI_CONFIG_KEY="sample-ai-config"
106-
export LAUNCHDARKLY_AI_JUDGE_KEY="sample-ai-judge-accuracy"
107-
```
108-
Note: The default values are `sample-ai-config` for AI Config and `sample-ai-judge-accuracy` for Judge Config if not specified.
25+
Each example README describes the full set of environment variables needed. The `.env` file is loaded automatically when running any example.
10926

110-
##### Available judge examples:
27+
### Examples
11128

112-
- **Chat with automatic judge evaluation** (`poetry run chat-judge-example`): Uses the chat functionality which automatically evaluates responses with any judges defined in the AI config.
113-
- **Direct judge evaluation** (`poetry run direct-judge-example`): Evaluates specific input/output pairs using a judge configuration directly.
29+
| Example | Description | README |
30+
| --- | --- | --- |
31+
| **OpenAI** | Single provider using OpenAI | [examples/openai](examples/openai/README.md) |
32+
| **Bedrock** | Single provider using AWS Bedrock | [examples/bedrock](examples/bedrock/README.md) |
33+
| **Gemini** | Single provider using Google Gemini | [examples/gemini](examples/gemini/README.md) |
34+
| **LangChain** | Multiple providers via LangChain | [examples/langchain](examples/langchain/README.md) |
35+
| **LangGraph Agent** | Single agent using LangGraph | [examples/langgraph_agent](examples/langgraph_agent/README.md) |
36+
| **LangGraph Multi-Agent** | Multiple agents using LangGraph | [examples/langgraph_multi_agent](examples/langgraph_multi_agent/README.md) |
37+
| **Judge** | Judge evaluation of AI responses | [examples/judge](examples/judge/README.md) |
38+
| **Chat with Observability** | Observability plugin for AI chat monitoring | [examples/chat_observability](examples/chat_observability/README.md) |

examples/bedrock/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Bedrock Example (Single Provider)
2+
3+
This example demonstrates how to use LaunchDarkly's AI Config with the AWS Bedrock provider.
4+
5+
## Prerequisites
6+
7+
- Python 3.10 or higher
8+
- [Poetry](https://python-poetry.org/) installed
9+
- A LaunchDarkly account with an [AI Config](https://launchdarkly.com/docs/home/ai-configs/create) created
10+
- AWS credentials configured for Bedrock access
11+
12+
## Setup
13+
14+
1. Create a `.env` file in this directory with the following variables:
15+
16+
```
17+
LAUNCHDARKLY_SDK_KEY=your-launchdarkly-sdk-key
18+
LAUNCHDARKLY_AI_CONFIG_KEY=sample-ai-config
19+
```
20+
21+
> `LAUNCHDARKLY_AI_CONFIG_KEY` defaults to `sample-ai-config` if not set.
22+
23+
2. Ensure your AWS credentials can be [auto-detected by the `boto3` library](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html). You can set them in your `.env` file:
24+
25+
```
26+
AWS_ACCESS_KEY_ID=your-access-key-id
27+
AWS_SECRET_ACCESS_KEY=your-secret-access-key
28+
AWS_DEFAULT_REGION=us-east-1
29+
```
30+
31+
Other options include role providers or shared credential files.
32+
33+
3. Install the required dependencies:
34+
35+
```bash
36+
poetry install
37+
```
38+
39+
## Run
40+
41+
```bash
42+
poetry run bedrock-example
43+
```
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import os
2+
from dotenv import load_dotenv
23
import ldclient
34
from ldclient import Context
45
from ldclient.config import Config
56
from ldai import LDAIClient
67
import boto3
78

8-
client = boto3.client("bedrock-runtime", region_name="us-east-1")
9+
load_dotenv()
10+
11+
client = boto3.client("bedrock-runtime", region_name=os.getenv('AWS_DEFAULT_REGION', 'us-east-1'))
912

1013
# Set sdk_key to your LaunchDarkly SDK key.
1114
sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY')

examples/bedrock/pyproject.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[tool.poetry]
2+
name = "hello-python-ai-bedrock"
3+
version = "0.1.0"
4+
description = "Hello LaunchDarkly for Python AI - Bedrock"
5+
authors = ["LaunchDarkly <dev@launchdarkly.com>"]
6+
license = "Apache-2.0"
7+
readme = "README.md"
8+
packages = [{include = "bedrock_example.py"}]
9+
10+
[tool.poetry.scripts]
11+
bedrock-example = "bedrock_example:main"
12+
13+
[tool.poetry.dependencies]
14+
python = "^3.10"
15+
python-dotenv = ">=1.0.0"
16+
launchdarkly-server-sdk-ai = "^0.17.0"
17+
boto3 = ">=0.2.0"
18+
19+
[build-system]
20+
requires = ["poetry-core"]
21+
build-backend = "poetry.core.masonry.api"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Chat with Observability (Observability Plugin Example)
2+
3+
This example demonstrates how to use the LaunchDarkly observability SDK plugin to monitor AI chat operations. For more details, see the [Python SDK observability reference](https://launchdarkly.com/docs/sdk/observability/python).
4+
5+
The observability plugin automatically captures and sends data to LaunchDarkly:
6+
7+
- **Observability tab**: SDK operations, flag evaluations, error monitoring, logging, and distributed tracing
8+
- **AI Config Monitoring tab**: Token usage, duration, success/error rates, and custom metadata for filtering and analysis
9+
10+
View your data in the LaunchDarkly dashboard under **Observability** tabs.
11+
12+
## Prerequisites
13+
14+
- Python 3.10 or higher
15+
- [Poetry](https://python-poetry.org/) installed
16+
- A LaunchDarkly account with an [AI Config](https://launchdarkly.com/docs/home/ai-configs/create) created
17+
- An API key for your AI provider (e.g., OpenAI)
18+
19+
## Setup
20+
21+
1. Create a `.env` file in this directory with the following variables:
22+
23+
```
24+
LAUNCHDARKLY_SDK_KEY=your-launchdarkly-sdk-key
25+
LAUNCHDARKLY_AI_CONFIG_KEY=sample-ai-config
26+
OPENAI_API_KEY=your-openai-api-key
27+
```
28+
29+
> `LAUNCHDARKLY_AI_CONFIG_KEY` defaults to `sample-ai-config` if not set.
30+
31+
Optionally, set service identification:
32+
33+
```
34+
SERVICE_NAME=my-ai-service
35+
SERVICE_VERSION=1.0.0
36+
```
37+
38+
2. Install the required dependencies:
39+
40+
```bash
41+
poetry install
42+
```
43+
44+
## Run
45+
46+
```bash
47+
poetry run chat-observability-example
48+
```

examples/chat_observability_example.py renamed to examples/chat_observability/chat_observability_example.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from dotenv import load_dotenv
23
import asyncio
34
import logging
45
import ldclient
@@ -7,6 +8,8 @@
78
from ldai import LDAIClient, AICompletionConfigDefault
89
from ldobserve import ObservabilityConfig, ObservabilityPlugin
910

11+
load_dotenv()
12+
1013
logging.getLogger('ldclient').setLevel(logging.WARNING)
1114

1215
# Set sdk_key to your LaunchDarkly SDK key.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[tool.poetry]
2+
name = "hello-python-ai-chat-observability"
3+
version = "0.1.0"
4+
description = "Hello LaunchDarkly for Python AI - Chat with Observability"
5+
authors = ["LaunchDarkly <dev@launchdarkly.com>"]
6+
license = "Apache-2.0"
7+
readme = "README.md"
8+
packages = [{include = "chat_observability_example.py"}]
9+
10+
[tool.poetry.scripts]
11+
chat-observability-example = "chat_observability_example:main"
12+
13+
[tool.poetry.dependencies]
14+
python = "^3.10"
15+
python-dotenv = ">=1.0.0"
16+
launchdarkly-server-sdk-ai = "^0.17.0"
17+
launchdarkly-observability = ">=0.1.0"
18+
openai = ">=0.2.0"
19+
20+
[build-system]
21+
requires = ["poetry-core"]
22+
build-backend = "poetry.core.masonry.api"

examples/gemini/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Gemini Example (Single Provider)
2+
3+
This example demonstrates how to use LaunchDarkly's AI Config with the Google Gemini provider.
4+
5+
## Prerequisites
6+
7+
- Python 3.10 or higher
8+
- [Poetry](https://python-poetry.org/) installed
9+
- A LaunchDarkly account with an [AI Config](https://launchdarkly.com/docs/home/ai-configs/create) created
10+
- A [Google API key](https://aistudio.google.com/apikey)
11+
12+
## Setup
13+
14+
1. Create a `.env` file in this directory with the following variables:
15+
16+
```
17+
LAUNCHDARKLY_SDK_KEY=your-launchdarkly-sdk-key
18+
LAUNCHDARKLY_AI_CONFIG_KEY=sample-ai-config
19+
GOOGLE_API_KEY=your-google-api-key
20+
```
21+
22+
> `LAUNCHDARKLY_AI_CONFIG_KEY` defaults to `sample-ai-config` if not set.
23+
24+
2. Install the required dependencies:
25+
26+
```bash
27+
poetry install
28+
```
29+
30+
## Run
31+
32+
```bash
33+
poetry run gemini-example
34+
```

0 commit comments

Comments
 (0)