LaunchDarkly Server-Side AI SDK for Python
This package contains the LaunchDarkly Server-Side AI SDK for Python (launchdarkly-server-sdk-ai).
LaunchDarkly overview
LaunchDarkly is a feature management platform that serves over 100 billion feature flags daily to help teams build better software, faster. Get started using LaunchDarkly today!
Quick Setup
This assumes that you have already installed the LaunchDarkly Python (server-side) SDK.
- Install this package with
pip:
pip install launchdarkly-server-sdk-ai
- Create an AI SDK instance:
from ldclient import LDClient, Config, Context
from ldai import LDAIClient
# The ld_client instance should be created based on the instructions in the relevant SDK.
ld_client = LDClient(Config("your-sdk-key"))
ai_client = LDAIClient(ld_client)
Setting Default AI Configurations
When retrieving AI configurations, you need to provide default values that will be used if the configuration is not available from LaunchDarkly:
Fully Configured Default
from ldai import AICompletionConfigDefault, ModelConfig, LDMessage
default_config = AICompletionConfigDefault(
enabled=True,
model=ModelConfig(
name='gpt-4',
parameters={'temperature': 0.7, 'maxTokens': 1000}
),
messages=[
LDMessage(role='system', content='You are a helpful assistant.')
]
)
Disabled Default
from ldai import AICompletionConfigDefault
default_config = AICompletionConfigDefault(
enabled=False
)
Retrieving AI Configurations
The completion_config method retrieves AI configurations from LaunchDarkly with support for dynamic variables and fallback values:
from ldclient import Context
from ldai import LDAIClient, AICompletionConfigDefault, ModelConfig
context = Context.create("user-123")
ai_config = ai_client.completion_config(
ai_config_key,
context,
default_config,
variables={'myVariable': 'My User Defined Variable'} # Variables for template interpolation
)
# Ensure configuration is enabled
if ai_config.enabled:
messages = ai_config.messages
model = ai_config.model
tracker = ai_config.create_tracker()
# Use with your AI provider
ManagedModel for Conversational AI
ManagedModel provides a high-level interface for conversational AI with automatic conversation management and metrics tracking:
- Automatically configures models based on AI configuration
- Maintains conversation history across multiple interactions
- Automatically tracks token usage, latency, and success rates
- Works with any supported AI provider (see AI Providers for available packages)
Using ManagedModel
import asyncio
from ldclient import Context
from ldai import LDAIClient, AICompletionConfigDefault, ModelConfig, LDMessage
# Use the same default_config from the retrieval section above
async def main():
context = Context.create("user-123")
model = ai_client.create_model(
'customer-support-chat',
context,
default_config,
variables={'customerName': 'John'}
)
if model:
# Simple conversation flow - metrics are automatically tracked by run()
response1 = await model.run('I need help with my order')
print(response1.content)
response2 = await model.run("What's the status?")
print(response2.content)
asyncio.run(main())
Advanced Usage with Providers
For more control, you can use the configuration directly with AI providers. We recommend using LaunchDarkly AI Provider packages when available:
Using AI Provider Packages
import asyncio
from ldai import LDAIClient, AICompletionConfigDefault, ModelConfig
from ldai_langchain import create_langchain_model, get_ai_metrics_from_response
async def main():
ai_config = ai_client.completion_config(ai_config_key, context, default)
# Create LangChain model from configuration
llm = create_langchain_model(ai_config)
# Use with tracking (sync invoke). Mint a tracker once per AI run.
tracker = ai_config.create_tracker()
response = tracker.track_metrics_of(
get_ai_metrics_from_response,
lambda: llm.invoke(messages),
)
print('AI Response:', response.content)
asyncio.run(main())
Using Custom Providers
import asyncio
from ldai import LDAIClient, AICompletionConfigDefault, ModelConfig
from ldai.providers import LDAIMetrics
from ldai.tracker import TokenUsage
async def main():
ai_config = ai_client.completion_config(ai_config_key, context, default)
# Define custom metrics mapping for your provider
def map_custom_provider_metrics(response):
return LDAIMetrics(
success=True,
tokens=TokenUsage(
total=response.usage.get('total_tokens', 0) if response.usage else 0,
input=response.usage.get('prompt_tokens', 0) if response.usage else 0,
output=response.usage.get('completion_tokens', 0) if response.usage else 0,
)
)
# Use with custom provider and tracking
async def call_custom_provider():
return await custom_provider.generate(
messages=ai_config.messages or [],
model=ai_config.model.name if ai_config.model else 'custom-model',
temperature=ai_config.model.get_parameter('temperature') if ai_config.model else 0.5,
)
# Mint a tracker once per AI run.
tracker = ai_config.create_tracker()
result = await tracker.track_metrics_of_async(
map_custom_provider_metrics,
call_custom_provider,
)
print('AI Response:', result.content)
asyncio.run(main())
Documentation
For full documentation, please refer to the LaunchDarkly AI SDK documentation.
License
Apache-2.0
Metadata
Release files for launchdarkly-server-sdk-ai 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| launchdarkly_server_sdk_ai-1.2.0.tar.gz | 91.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| launchdarkly_server_sdk_ai-1.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 128.8 kB
Release files / launchdarkly_server_sdk_ai-1.2.0.tar.gz
| Download URL | launchdarkly_server_sdk_ai-1.2.0.tar.gz |
|---|---|
| Size | 91.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e9c7e5069c53e70bd4daa83cc8e02efbf7cf020f8451c83f8ff46420c0e4af10
|
|
BLAKE2b-256 checksum How to use checksums |
48b5a323f590868c5dc07be79218ced974c4098b7101fe708f3f5a29f995e6a2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / launchdarkly_server_sdk_ai-1.2.0-py3-none-any.whl
| Download URL | launchdarkly_server_sdk_ai-1.2.0-py3-none-any.whl |
|---|---|
| Size | 37.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3712ff313c8e65d39ac6c00222fc4d9fa7e364bb4766c8c6382f5ccaab407cc5
|
|
BLAKE2b-256 checksum How to use checksums |
45d24670b8377cc2b313f3fc3828608e6c60f49d6fcc6fd816dee0de6e8152a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|