Skip to main content

LaunchDarkly AI SDK LangChain Provider

Project description

LaunchDarkly AI SDK - LangChain 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 LangChain integration for the LaunchDarkly Server-Side AI SDK, allowing you to use LangChain models and chains with LaunchDarkly's tracking and configuration capabilities.

Installation

pip install launchdarkly-server-sdk-ai-langchain

You'll also need to install the LangChain provider packages for the models you want to use:

# For OpenAI
pip install langchain-openai

# For Anthropic
pip install langchain-anthropic

# For Google
pip install langchain-google-genai

Quick Start

import asyncio
from ldclient import LDClient, Config, Context
from ldai import init
from ldai_langchain import LangChainProvider

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

# Get AI configuration. 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.")]
#   )
#   config = ai_client.config("ai-config-key", context, default)
context = Context.builder("user-123").build()
config = ai_client.config("ai-config-key", context)

async def main():
    # Create a LangChain provider from the AI configuration
    provider = await LangChainProvider.create(config)

    # Use the provider to invoke the model
    from ldai.models import LDMessage
    messages = [
        LDMessage(role="system", content="You are a helpful assistant."),
        LDMessage(role="user", content="Hello, how are you?"),
    ]
    
    response = await provider.invoke_model(messages)
    print(response.message.content)

asyncio.run(main())

Usage

Using LangChainProvider with the Create Factory

The simplest way to use the LangChain provider is with the static create factory method, which automatically creates the appropriate LangChain model based on your LaunchDarkly AI configuration:

from ldai_langchain import LangChainProvider

# Create provider from AI configuration
provider = await LangChainProvider.create(ai_config)

# Invoke the model
response = await provider.invoke_model(messages)

Using an Existing LangChain Model

If you already have a LangChain model configured, you can use it directly:

from langchain_openai import ChatOpenAI
from ldai_langchain import LangChainProvider

# Create your own LangChain model
llm = ChatOpenAI(model="gpt-4", temperature=0.7)

# Wrap it with LangChainProvider
provider = LangChainProvider(llm)

# Use with LaunchDarkly tracking
response = await provider.invoke_model(messages)

Structured Output

The provider supports structured output using LangChain's with_structured_output:

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

result = await provider.invoke_structured_model(messages, response_structure)
print(result.data)  # {"sentiment": "positive", "confidence": 0.95}

Tracking Metrics

Use the provider with LaunchDarkly's tracking capabilities:

# Get the AI config with tracker
config = ai_client.config("ai-config-key", context)

# Create provider
provider = await LangChainProvider.create(config)

# Track metrics automatically
async def invoke():
    return await provider.invoke_model(messages)

response = await config.tracker.track_metrics_of_async(
    invoke,
    lambda r: r.metrics
)

Static Utility Methods

The LangChainProvider class provides several utility methods:

Converting Messages

from ldai.models import LDMessage
from ldai_langchain import LangChainProvider

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

# Convert to LangChain messages
langchain_messages = LangChainProvider.convert_messages_to_langchain(messages)

Extracting Metrics

from ldai_langchain import LangChainProvider

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

Provider Name Mapping

# Map LaunchDarkly provider names to LangChain provider names
langchain_provider = LangChainProvider.map_provider("gemini")  # Returns "google-genai"

API Reference

LangChainProvider

Constructor

LangChainProvider(llm: BaseChatModel, logger: Optional[Any] = None)

Static Methods

  • create(ai_config: AIConfigKind, logger: Optional[Any] = None) -> LangChainProvider - Factory method to create a provider from AI configuration
  • convert_messages_to_langchain(messages: List[LDMessage]) -> List[BaseMessage] - Convert LaunchDarkly messages to LangChain messages
  • get_ai_metrics_from_response(response: AIMessage) -> LDAIMetrics - Extract metrics from a LangChain response
  • map_provider(ld_provider_name: str) -> str - Map LaunchDarkly provider names to LangChain names
  • create_langchain_model(ai_config: AIConfigKind) -> BaseChatModel - Create a LangChain model from AI configuration

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 with structured output
  • get_chat_model() -> BaseChatModel - Get the underlying LangChain model

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_langchain-0.5.0.tar.gz (26.7 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_langchain-0.5.0.tar.gz.

File metadata

File hashes

Hashes for launchdarkly_server_sdk_ai_langchain-0.5.0.tar.gz
Algorithm Hash digest
SHA256 fa43a8c0e50999e5ddf2c2814e582e47d022890bd4078b0c1eba64c5149c26cf
MD5 f5146ad9214a9abd5ee20ae7d8d24793
BLAKE2b-256 aade06654d63a3a0e59b50ea63f8c6383798fad78010104efec29c212dc278ee

See more details on using hashes here.

File details

Details for the file launchdarkly_server_sdk_ai_langchain-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for launchdarkly_server_sdk_ai_langchain-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 24383e963bba35ff5e94b1d08cd0e6a6122fe64b4baf421ac311b4fa74467d2d
MD5 2b8a693ea99bae981746b8214b562399
BLAKE2b-256 be0e0dcdffbad5bc4c90387767eafd9e573567ea6704eddd625ea79b94627083

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