Skip to content

Commit 7bc0521

Browse files
authored
feat: Add an example with observability (#15)
1 parent e05e465 commit 7bc0521

3 files changed

Lines changed: 127 additions & 0 deletions

File tree

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ This repository includes examples for `OpenAI`, `Bedrock`, and `LangChain` for m
3030
1. Set the environment variable `OPENAI_API_KEY` to your OpenAI key.
3131
1. On the command line, run `poetry run openai-example`.
3232

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).
36+
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"
43+
```
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+
3352
#### Bedrock setup (single provider)
3453

3554
1. Install the required dependencies with `poetry install -E bedrock` or `poetry install --all-extras`.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import os
2+
import asyncio
3+
import logging
4+
import ldclient
5+
from ldclient import Context
6+
from ldclient.config import Config
7+
from ldai import LDAIClient, AICompletionConfigDefault
8+
from ldobserve import ObservabilityConfig, ObservabilityPlugin
9+
10+
logging.getLogger('ldclient').setLevel(logging.WARNING)
11+
12+
# Set sdk_key to your LaunchDarkly SDK key.
13+
sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY')
14+
15+
# Set config_key to the AI Config key you want to evaluate.
16+
ai_config_key = os.getenv('LAUNCHDARKLY_AI_CONFIG_KEY', 'sample-ai-config')
17+
18+
# Service configuration for observability
19+
service_name = os.getenv('SERVICE_NAME', 'hello-python-ai-observability')
20+
service_version = os.getenv('SERVICE_VERSION', '1.0.0')
21+
22+
23+
async def async_main():
24+
if not sdk_key:
25+
print("*** Please set the LAUNCHDARKLY_SDK_KEY env first")
26+
exit()
27+
28+
29+
# Initialize LaunchDarkly SDK with observability plugin
30+
ldclient.set_config(Config(
31+
sdk_key,
32+
plugins=[
33+
ObservabilityPlugin(
34+
ObservabilityConfig(
35+
service_name=service_name,
36+
service_version=service_version,
37+
)
38+
)
39+
]
40+
))
41+
42+
if not ldclient.get().is_initialized():
43+
print("\n*** SDK failed to initialize. Please check your internet connection and SDK credential for any typo.")
44+
exit()
45+
46+
aiclient = LDAIClient(ldclient.get())
47+
print("*** SDK successfully initialized")
48+
49+
# Set up the evaluation context with custom attributes for filtering
50+
context = (
51+
Context
52+
.builder('example-user-key')
53+
.kind('user')
54+
.name('Sandy')
55+
.set('environment', 'observability-demo')
56+
.set('tier', 'premium')
57+
.build()
58+
)
59+
60+
try:
61+
# Create a chat instance with custom variables
62+
default_value = AICompletionConfigDefault(enabled=False)
63+
64+
chat = await aiclient.create_chat(
65+
ai_config_key,
66+
context,
67+
default_value,
68+
{
69+
'example_type': 'observability_demo',
70+
'session_id': 'demo-session-123',
71+
'feature': 'ai_chat'
72+
}
73+
)
74+
75+
if not chat:
76+
print(f"*** AI chat configuration is not enabled for key: {ai_config_key}")
77+
return
78+
79+
user_input_1 = "What is feature flagging in 2 sentences?"
80+
print("User Input:", user_input_1)
81+
82+
response_1 = await chat.invoke(user_input_1)
83+
print("Chat Response:", response_1.message.content)
84+
85+
user_input_2 = "Give me a specific use case example."
86+
print("\nUser Input:", user_input_2)
87+
88+
response_2 = await chat.invoke(user_input_2)
89+
print("Chat Response:", response_2.message.content)
90+
91+
print("\nSuccess.")
92+
93+
except Exception as err:
94+
print("Error:", err)
95+
finally:
96+
ldclient.get().close()
97+
98+
99+
def main():
100+
"""Synchronous entry point for Poetry script."""
101+
asyncio.run(async_main())
102+
103+
104+
if __name__ == "__main__":
105+
main()

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ langchain-example = 'examples.langchain_example:main'
1515
langgraph-agent-example = 'examples.langgraph_agent_example:main'
1616
langgraph-multi-agent-example = 'examples.langgraph_multi_agent_example:main'
1717
chat-judge-example = 'examples.chat_judge_example:main'
18+
chat-observability-example = 'examples.chat_observability_example:main'
1819
direct-judge-example = 'examples.direct_judge_example:main'
1920

2021
[tool.poetry.dependencies]
2122
python = "^3.10"
2223
launchdarkly-server-sdk-ai = "^0.14.0"
2324
launchdarkly-server-sdk-ai-langchain = "^0.3.0"
2425
launchdarkly-server-sdk-ai-openai = "^0.1.0"
26+
launchdarkly-observability = { version = ">=0.1.0", optional = true }
2527

2628
boto3 = { version = ">=0.2.0", optional = true }
2729
openai = { version = ">=0.2.0", optional = true }
@@ -39,6 +41,7 @@ openai = ["openai"]
3941
gemini = ["google-genai"]
4042
langchain = ["langchain", "langchain-core", "langchain-openai", "langchain-google-genai", "langchain-aws"]
4143
langgraph = ["langgraph", "typing-extensions"]
44+
observability = ["launchdarkly-observability", "openai"]
4245

4346
[build-system]
4447
requires = ["poetry-core"]

0 commit comments

Comments
 (0)