@@ -23,64 +23,135 @@ pip install launchdarkly-server-sdk-ai-openai
2323
2424``` python
2525import asyncio
26- from ldai import AIClient
27- from ldai_openai import OpenAIProvider
26+ from ldclient import LDClient, Config, Context
27+ from ldai import init
28+ from ldai.models import AICompletionConfigDefault, ModelConfig, ProviderConfig
29+
30+ # Initialize LaunchDarkly client
31+ ld_client = LDClient(Config(" your-sdk-key" ))
32+ ai_client = init(ld_client)
33+
34+ context = Context.builder(" user-123" ).build()
2835
2936async def main ():
30- # Initialize the AI client
31- ai_client = AIClient(ld_client)
32-
33- # Get AI config. Pass a default for improved resiliency when the flag is unavailable or
34- # LaunchDarkly is unreachable; omit for a disabled default. Example:
35- # from ldai.models import AICompletionConfigDefault, LDMessage, ModelConfig, ProviderConfig
36- # default = AICompletionConfigDefault(
37- # enabled=True,
38- # model=ModelConfig("gpt-4"),
39- # provider=ProviderConfig("openai"),
40- # messages=[LDMessage(role="system", content="You are a helpful assistant.")]
41- # )
42- # ai_config = ai_client.config("my-ai-config-key", context, default)
43- ai_config = ai_client.config(" my-ai-config-key" , context)
44-
45- # Create an OpenAI provider from the config
46- provider = await OpenAIProvider.create(ai_config)
47-
48- # Invoke the model
49- response = await provider.invoke_model(ai_config.messages)
50- print (response.message.content)
37+ # Create a ManagedModel backed by the OpenAI provider
38+ model = await ai_client.create_model(
39+ " ai-config-key" ,
40+ context,
41+ AICompletionConfigDefault(
42+ enabled = True ,
43+ model = ModelConfig(" gpt-4" ),
44+ provider = ProviderConfig(" openai" ),
45+ ),
46+ )
47+
48+ if model:
49+ result = await model.run(" Hello, how are you?" )
50+ print (result.content)
5151
5252asyncio.run(main())
5353```
5454
55- ## Features
55+ ## Usage
5656
57- - Full integration with OpenAI's chat completions API
58- - Automatic token usage tracking
59- - Support for structured output (JSON schema)
60- - Static utility methods for custom integrations
57+ ### Using ` create_model ` (recommended)
6158
62- ## API Reference
59+ The recommended entry point is ` LDAIClient.create_model ` , which evaluates a
60+ LaunchDarkly AI config flag, selects the OpenAI runner automatically, and
61+ returns a ` ManagedModel ` that wraps the runner:
6362
64- ### OpenAIProvider
63+ ``` python
64+ model = await ai_client.create_model(" ai-config-key" , context)
65+
66+ if model:
67+ result = await model.run(" What is feature flagging?" )
68+ print (result.content)
69+ ```
6570
66- #### Constructor
71+ ### Using the runner directly
72+
73+ If you need to construct a runner manually (e.g. for testing), you can use
74+ ` OpenAIRunnerFactory ` from the ` ldai_openai ` package:
6775
6876``` python
69- OpenAIProvider(client: OpenAI, model_name: str , parameters: Dict[str , Any], logger: Optional[Any] = None )
77+ from ldai_openai import OpenAIRunnerFactory
78+
79+ factory = OpenAIRunnerFactory() # uses OPENAI_API_KEY from environment
80+ runner = factory.create_model(ai_config)
81+
82+ result = await runner.run(" Hello!" )
83+ print (result.content)
7084```
7185
72- #### Static Methods
86+ ### Structured Output
87+
88+ Pass a JSON schema dict as ` output_type ` to request structured output:
7389
74- - ` create(ai_config: AIConfigKind, logger: Optional[Any] = None) -> OpenAIProvider ` - Factory method to create a provider from an AI config
75- - ` get_ai_metrics_from_response(response: Any) -> LDAIMetrics ` - Extract metrics from an OpenAI response
90+ ``` python
91+ response_structure = {
92+ " type" : " object" ,
93+ " properties" : {
94+ " sentiment" : {" type" : " string" , " enum" : [" positive" , " negative" , " neutral" ]},
95+ " confidence" : {" type" : " number" },
96+ },
97+ " required" : [" sentiment" , " confidence" ],
98+ }
99+
100+ result = await runner.run(messages, output_type = response_structure)
101+ print (result.parsed) # {"sentiment": "positive", "confidence": 0.95}
102+ ```
103+
104+ ### Tracking Metrics
105+
106+ ` ManagedModel.run() ` automatically tracks metrics via the associated
107+ ` LDAIConfigTracker ` . For manual tracking, use the tracker directly:
108+
109+ ``` python
110+ model = await ai_client.create_model(" ai-config-key" , context)
111+
112+ if model:
113+ result = await model.run(" Explain feature flags." )
114+ # Metrics are tracked automatically; access them via result.metrics
115+ print (result.metrics.usage)
116+ ```
76117
77- #### Instance Methods
118+ ### Static Utility Methods
78119
79- - ` invoke_model(messages: List[LDMessage]) -> ChatResponse ` - Invoke the model with messages
80- - ` invoke_structured_model(messages: List[LDMessage], response_structure: Dict[str, Any]) -> StructuredResponse ` - Invoke the model with structured output
81- - ` get_client() -> OpenAI ` - Get the underlying OpenAI client
120+ The ` ldai_openai ` helper module provides several utility functions:
121+
122+ #### Converting Messages
123+
124+ ``` python
125+ from ldai.models import LDMessage
126+ from ldai_openai import convert_messages_to_openai
127+
128+ messages = [
129+ LDMessage(role = " system" , content = " You are helpful." ),
130+ LDMessage(role = " user" , content = " Hello!" ),
131+ ]
132+
133+ openai_messages = convert_messages_to_openai(messages)
134+ ```
135+
136+ #### Extracting Metrics
137+
138+ ``` python
139+ from ldai_openai import get_ai_metrics_from_response
140+
141+ # After getting a response from OpenAI
142+ metrics = get_ai_metrics_from_response(response)
143+ print (f " Success: { metrics.success} " )
144+ print (f " Tokens used: { metrics.usage.total if metrics.usage else ' N/A' } " )
145+ ```
146+
147+ ## Documentation
148+
149+ For full documentation, please refer to the [ LaunchDarkly AI SDK documentation] ( https://docs.launchdarkly.com/sdk/ai/python ) .
150+
151+ ## Contributing
152+
153+ See [ CONTRIBUTING.md] ( ../../../CONTRIBUTING.md ) in the repository root.
82154
83155## License
84156
85157Apache-2.0
86-
0 commit comments