Skip to main content

LaunchDarkly AI SDK OpenAI Provider

Project description

LaunchDarkly AI SDK OpenAI Provider

Actions Status

PyPI PyPI

[!CAUTION] This package is in pre-release and not subject to backwards compatibility guarantees. The API may change based on feedback.

Pin to a specific minor version and review the changelog before upgrading.

This package provides an OpenAI integration for the LaunchDarkly AI SDK.

Installation

pip install launchdarkly-server-sdk-ai-openai

Quick Start

import asyncio
from ldclient import LDClient, Config, Context
from ldai import init
from ldai.models import AICompletionConfigDefault, ModelConfig, ProviderConfig

# Initialize LaunchDarkly client
ld_client = LDClient(Config("your-sdk-key"))
ai_client = init(ld_client)

context = Context.builder("user-123").build()

async def main():
    # Create a ManagedModel backed by the OpenAI provider
    model = await ai_client.create_model(
        "ai-config-key",
        context,
        AICompletionConfigDefault(
            enabled=True,
            model=ModelConfig("gpt-4"),
            provider=ProviderConfig("openai"),
        ),
    )

    if model:
        result = await model.run("Hello, how are you?")
        print(result.content)

asyncio.run(main())

Usage

Using create_model (recommended)

The recommended entry point is LDAIClient.create_model, which evaluates a LaunchDarkly AI config flag, selects the OpenAI runner automatically, and returns a ManagedModel that wraps the runner:

model = await ai_client.create_model("ai-config-key", context)

if model:
    result = await model.run("What is feature flagging?")
    print(result.content)

Using the runner directly

If you need to construct a runner manually (e.g. for testing), you can use OpenAIRunnerFactory from the ldai_openai package:

from ldai_openai import OpenAIRunnerFactory

factory = OpenAIRunnerFactory()  # uses OPENAI_API_KEY from environment
runner = factory.create_model(ai_config)

result = await runner.run("Hello!")
print(result.content)

Structured Output

Pass a JSON schema dict as output_type to request structured output:

response_structure = {
    "type": "object",
    "properties": {
        "sentiment": {"type": "string", "enum": ["positive", "negative", "neutral"]},
        "confidence": {"type": "number"},
    },
    "required": ["sentiment", "confidence"],
}

result = await runner.run(messages, output_type=response_structure)
print(result.parsed)  # {"sentiment": "positive", "confidence": 0.95}

Tracking Metrics

ManagedModel.run() automatically tracks metrics via the associated LDAIConfigTracker. For manual tracking, use the tracker directly:

model = await ai_client.create_model("ai-config-key", context)

if model:
    result = await model.run("Explain feature flags.")
    # Metrics are tracked automatically; access them via result.metrics
    print(result.metrics.tokens)

Static Utility Methods

The ldai_openai helper module provides several utility functions:

Converting Messages

from ldai.models import LDMessage
from ldai_openai import convert_messages_to_openai

messages = [
    LDMessage(role="system", content="You are helpful."),
    LDMessage(role="user", content="Hello!"),
]

openai_messages = convert_messages_to_openai(messages)

Extracting Metrics

from ldai_openai import get_ai_metrics_from_response

# After getting a response from OpenAI
metrics = get_ai_metrics_from_response(response)
print(f"Success: {metrics.success}")
print(f"Tokens used: {metrics.tokens.total if metrics.tokens else 'N/A'}")

Documentation

For full documentation, please refer to the LaunchDarkly AI SDK documentation.

Contributing

See CONTRIBUTING.md in the repository root.

License

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

launchdarkly_server_sdk_ai_openai-0.6.0.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file launchdarkly_server_sdk_ai_openai-0.6.0.tar.gz.

File metadata

File hashes

Hashes for launchdarkly_server_sdk_ai_openai-0.6.0.tar.gz
Algorithm Hash digest
SHA256 ccb6df727e2c479399d855375e0cbe02228a54af7f3591a11158eb10c24a2365
MD5 dc485d545938dad7bd55604459fcc4e5
BLAKE2b-256 6c22705da5f8d842957e9abefbcf8db32a8ce1b102353476111d02a6ac98035d

See more details on using hashes here.

File details

Details for the file launchdarkly_server_sdk_ai_openai-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for launchdarkly_server_sdk_ai_openai-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e872328b29c25b136967a4d3522288fb502d925d36788930877e4b150910cd5
MD5 06f793956e64ed8d733d197b98c59cba
BLAKE2b-256 79cd8cc2bc95aa30126957dbd7fac31bd2fbcfbef46226c40fc30c247589ad7d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page