|
| 1 | +import os |
| 2 | +import asyncio |
| 3 | +import logging |
| 4 | +import ldclient |
| 5 | +from ldclient import Context |
| 6 | +from ldclient.config import Config |
| 7 | +from ldai import LDAIClient, AICompletionConfigDefault |
| 8 | +from ldobserve import ObservabilityConfig, ObservabilityPlugin |
| 9 | + |
| 10 | +logging.getLogger('ldclient').setLevel(logging.WARNING) |
| 11 | + |
| 12 | +# Set sdk_key to your LaunchDarkly SDK key. |
| 13 | +sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY') |
| 14 | + |
| 15 | +# Set config_key to the AI Config key you want to evaluate. |
| 16 | +ai_config_key = os.getenv('LAUNCHDARKLY_AI_CONFIG_KEY', 'sample-ai-config') |
| 17 | + |
| 18 | +# Service configuration for observability |
| 19 | +service_name = os.getenv('SERVICE_NAME', 'hello-python-ai-observability') |
| 20 | +service_version = os.getenv('SERVICE_VERSION', '1.0.0') |
| 21 | + |
| 22 | + |
| 23 | +async def async_main(): |
| 24 | + if not sdk_key: |
| 25 | + print("*** Please set the LAUNCHDARKLY_SDK_KEY env first") |
| 26 | + exit() |
| 27 | + |
| 28 | + |
| 29 | + # Initialize LaunchDarkly SDK with observability plugin |
| 30 | + ldclient.set_config(Config( |
| 31 | + sdk_key, |
| 32 | + plugins=[ |
| 33 | + ObservabilityPlugin( |
| 34 | + ObservabilityConfig( |
| 35 | + service_name=service_name, |
| 36 | + service_version=service_version, |
| 37 | + ) |
| 38 | + ) |
| 39 | + ] |
| 40 | + )) |
| 41 | + |
| 42 | + if not ldclient.get().is_initialized(): |
| 43 | + print("\n*** SDK failed to initialize. Please check your internet connection and SDK credential for any typo.") |
| 44 | + exit() |
| 45 | + |
| 46 | + aiclient = LDAIClient(ldclient.get()) |
| 47 | + print("*** SDK successfully initialized") |
| 48 | + |
| 49 | + # Set up the evaluation context with custom attributes for filtering |
| 50 | + context = ( |
| 51 | + Context |
| 52 | + .builder('example-user-key') |
| 53 | + .kind('user') |
| 54 | + .name('Sandy') |
| 55 | + .set('environment', 'observability-demo') |
| 56 | + .set('tier', 'premium') |
| 57 | + .build() |
| 58 | + ) |
| 59 | + |
| 60 | + try: |
| 61 | + # Create a chat instance with custom variables |
| 62 | + default_value = AICompletionConfigDefault(enabled=False) |
| 63 | + |
| 64 | + chat = await aiclient.create_chat( |
| 65 | + ai_config_key, |
| 66 | + context, |
| 67 | + default_value, |
| 68 | + { |
| 69 | + 'example_type': 'observability_demo', |
| 70 | + 'session_id': 'demo-session-123', |
| 71 | + 'feature': 'ai_chat' |
| 72 | + } |
| 73 | + ) |
| 74 | + |
| 75 | + if not chat: |
| 76 | + print(f"*** AI chat configuration is not enabled for key: {ai_config_key}") |
| 77 | + return |
| 78 | + |
| 79 | + user_input_1 = "What is feature flagging in 2 sentences?" |
| 80 | + print("User Input:", user_input_1) |
| 81 | + |
| 82 | + response_1 = await chat.invoke(user_input_1) |
| 83 | + print("Chat Response:", response_1.message.content) |
| 84 | + |
| 85 | + user_input_2 = "Give me a specific use case example." |
| 86 | + print("\nUser Input:", user_input_2) |
| 87 | + |
| 88 | + response_2 = await chat.invoke(user_input_2) |
| 89 | + print("Chat Response:", response_2.message.content) |
| 90 | + |
| 91 | + print("\nSuccess.") |
| 92 | + |
| 93 | + except Exception as err: |
| 94 | + print("Error:", err) |
| 95 | + finally: |
| 96 | + ldclient.get().close() |
| 97 | + |
| 98 | + |
| 99 | +def main(): |
| 100 | + """Synchronous entry point for Poetry script.""" |
| 101 | + asyncio.run(async_main()) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + main() |
0 commit comments