Skip to main content

LaunchDarkly AI SDK OpenAI Provider

Project description

LaunchDarkly AI SDK OpenAI Provider

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-dev

Quick Start

import asyncio
from ldai import AIClient
from ldai_openai import OpenAIProvider

async def main():
    # Initialize the AI client
    ai_client = AIClient(ld_client)

    # Get AI config. Pass a default for improved resiliency when the flag is unavailable or
    # LaunchDarkly is unreachable; omit for a disabled default. Example:
    #   from ldai.models import AICompletionConfigDefault, LDMessage, ModelConfig, ProviderConfig
    #   default = AICompletionConfigDefault(
    #       enabled=True,
    #       model=ModelConfig("gpt-4"),
    #       provider=ProviderConfig("openai"),
    #       messages=[LDMessage(role="system", content="You are a helpful assistant.")]
    #   )
    #   ai_config = ai_client.config("my-ai-config-key", context, default)
    ai_config = ai_client.config("my-ai-config-key", context)
    
    # Create an OpenAI provider from the config
    provider = await OpenAIProvider.create(ai_config)
    
    # Invoke the model
    response = await provider.invoke_model(ai_config.messages)
    print(response.message.content)

asyncio.run(main())

Features

  • Full integration with OpenAI's chat completions API
  • Automatic token usage tracking
  • Support for structured output (JSON schema)
  • Static utility methods for custom integrations

API Reference

OpenAIProvider

Constructor

OpenAIProvider(client: OpenAI, model_name: str, parameters: Dict[str, Any], logger: Optional[Any] = None)

Static Methods

  • create(ai_config: AIConfigKind, logger: Optional[Any] = None) -> OpenAIProvider - Factory method to create a provider from an AI config
  • get_ai_metrics_from_response(response: Any) -> LDAIMetrics - Extract metrics from an OpenAI response

Instance Methods

  • invoke_model(messages: List[LDMessage]) -> ChatResponse - Invoke the model with messages
  • invoke_structured_model(messages: List[LDMessage], response_structure: Dict[str, Any]) -> StructuredResponse - Invoke the model with structured output
  • get_client() -> OpenAI - Get the underlying OpenAI client

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.3.0.tar.gz (18.0 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.3.0.tar.gz.

File metadata

File hashes

Hashes for launchdarkly_server_sdk_ai_openai-0.3.0.tar.gz
Algorithm Hash digest
SHA256 46be3f7b6f472c4d5f2aeb23d93c5b930ababbab49e65d6945afe4a2869ba5bd
MD5 968503ba9391993b86461a80d679f2b4
BLAKE2b-256 36f0e98128661dae22ab8a5efd6dbafde60c76b0c9b379acd0020de8de0468db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for launchdarkly_server_sdk_ai_openai-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fbf6ea8dac29d656f3d865d74a22a8f3c4426112e708050cbdf0c9fd5b6a7847
MD5 d00d3631c15100eff4c7e74d8bdd121b
BLAKE2b-256 be68a1bd8bf1ffc7cbd364cd28bdade271cd39035fc7a536b9ac7034707e605b

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