From bdb80f7a646b84d791d0806359b4988b6d74666a Mon Sep 17 00:00:00 2001 From: jsonbailey Date: Mon, 9 Mar 2026 10:55:15 -0500 Subject: [PATCH 1/2] chore: Improve default examples --- examples/bedrock_example.py | 28 +++++++++-------------- examples/chat_judge_example.py | 17 +++++++++----- examples/chat_observability_example.py | 20 ++++++++++------ examples/direct_judge_example.py | 23 +++++++++++++------ examples/gemini_example.py | 28 +++++++++-------------- examples/langchain_example.py | 28 +++++++++-------------- examples/langgraph_agent_example.py | 17 +++++++------- examples/langgraph_multi_agent_example.py | 15 +++++++----- examples/openai_example.py | 28 +++++++++-------------- 9 files changed, 102 insertions(+), 102 deletions(-) diff --git a/examples/bedrock_example.py b/examples/bedrock_example.py index c297dc9..de1a69d 100755 --- a/examples/bedrock_example.py +++ b/examples/bedrock_example.py @@ -40,26 +40,20 @@ def main(): .build() ) - DEFAULT_SYSTEM_MESSAGE = "You are a helpful assistant that can answer questions and help with tasks." - - # Set a fallback AIConfig to use if a config is not found or your application is not able to connect to LaunchDarkly. - default_value = AIConfig( - enabled=True, - model=ModelConfig(name='my-default-model', parameters={}), - provider=ProviderConfig(name='bedrock'), - messages=[LDMessage(role='system', content=DEFAULT_SYSTEM_MESSAGE)], - ) - - # Optionally, you can use a disabled AIConfig - # default_value = AIConfig( - # enabled=False - # ) - + # Pass a default for improved resiliency when the AI config is unavailable + # or LaunchDarkly is unreachable; omit for a disabled default. + # Example: + # default = AIConfig( + # enabled=True, + # model=ModelConfig(name='my-default-model'), + # provider=ProviderConfig(name='bedrock'), + # messages=[LDMessage(role='system', content='You are a helpful assistant.')], + # ) + # config_value, tracker = aiclient.config(ai_config_key, context, default, {'myUserVariable': "Testing Variable"}) config_value, tracker = aiclient.config( ai_config_key, context, - default_value, - {'myUserVariable': "Testing Variable"} + variables={'myUserVariable': "Testing Variable"} ) if not config_value.enabled: diff --git a/examples/chat_judge_example.py b/examples/chat_judge_example.py index ee907da..f8db9ea 100644 --- a/examples/chat_judge_example.py +++ b/examples/chat_judge_example.py @@ -38,12 +38,17 @@ async def async_main(): ) try: - # Example using the chat functionality which automates the judge evaluation - default_value = AICompletionConfigDefault( - enabled=False, - ) - - chat = await aiclient.create_chat(ai_config_key, context, default_value, { + # Pass a default for improved resiliency when the AI config is unavailable + # or LaunchDarkly is unreachable; omit for a disabled default. + # Example: + # default = AICompletionConfigDefault( + # enabled=True, + # model={'name': 'gpt-4'}, + # provider={'name': 'openai'}, + # messages=[{'role': 'system', 'content': 'You are a helpful assistant.'}], + # ) + # chat = await aiclient.create_chat(ai_config_key, context, default, {'companyName': 'LaunchDarkly'}) + chat = await aiclient.create_chat(ai_config_key, context, variables={ 'companyName': 'LaunchDarkly', }) diff --git a/examples/chat_observability_example.py b/examples/chat_observability_example.py index 58e1136..9841c66 100644 --- a/examples/chat_observability_example.py +++ b/examples/chat_observability_example.py @@ -58,14 +58,20 @@ async def async_main(): ) try: - # Create a chat instance with custom variables - default_value = AICompletionConfigDefault(enabled=False) - + # Pass a default for improved resiliency when the AI config is unavailable + # or LaunchDarkly is unreachable; omit for a disabled default. + # Example: + # default = AICompletionConfigDefault( + # enabled=True, + # model={'name': 'gpt-4'}, + # provider={'name': 'openai'}, + # messages=[{'role': 'system', 'content': 'You are a helpful assistant.'}], + # ) + # chat = await aiclient.create_chat(ai_config_key, context, default, {'example_type': 'observability_demo'}) chat = await aiclient.create_chat( - ai_config_key, - context, - default_value, - { + ai_config_key, + context, + variables={ 'example_type': 'observability_demo', 'session_id': 'demo-session-123', 'feature': 'ai_chat' diff --git a/examples/direct_judge_example.py b/examples/direct_judge_example.py index 1ce0a74..510e41f 100644 --- a/examples/direct_judge_example.py +++ b/examples/direct_judge_example.py @@ -4,7 +4,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai import LDAIClient, AICompletionConfigDefault +from ldai import LDAIClient, AIJudgeConfigDefault # Set sdk_key to your LaunchDarkly SDK key. sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY') @@ -38,12 +38,21 @@ async def async_main(): ) try: - # Example of using the judge functionality with direct input and output - # Get AI judge configuration from LaunchDarkly - judge_default_value = AICompletionConfigDefault( - enabled=False, - ) - judge = await aiclient.create_judge(judge_key, context, judge_default_value) + # Pass a default for improved resiliency when the AI config is unavailable + # or LaunchDarkly is unreachable; omit for a disabled default. + # Example (enabled default; judge default has three messages): + # default = AIJudgeConfigDefault( + # enabled=True, + # model={'name': 'gpt-4'}, + # provider={'name': 'openai'}, + # messages=[ + # {'role': 'system', 'content': 'Your judge criteria here.'}, + # {'role': 'assistant', 'content': 'MESSAGE HISTORY: {{message_history}}'}, + # {'role': 'user', 'content': 'RESPONSE TO EVALUATE: {{response_to_evaluate}}'}, + # ], + # ) + # judge = await aiclient.create_judge(judge_key, context, default) + judge = await aiclient.create_judge(judge_key, context) if not judge: print(f"*** AI judge configuration is not enabled for key: {judge_key}") diff --git a/examples/gemini_example.py b/examples/gemini_example.py index 919c754..4099db7 100644 --- a/examples/gemini_example.py +++ b/examples/gemini_example.py @@ -110,26 +110,20 @@ def main(): .build() ) - DEFAULT_SYSTEM_MESSAGE = "You are a helpful assistant that can answer questions and help with tasks." - - # Set a fallback AIConfig to use if a config is not found or your application is not able to connect to LaunchDarkly. - default_value = AIConfig( - enabled=True, - model=ModelConfig(name='gemini-pro', parameters={}), - provider=ProviderConfig(name='google'), - messages=[LDMessage(role='system', content=DEFAULT_SYSTEM_MESSAGE)], - ) - - # Optionally, you can use a disabled AIConfig - # default_value = AIConfig( - # enabled=False - # ) - + # Pass a default for improved resiliency when the AI config is unavailable + # or LaunchDarkly is unreachable; omit for a disabled default. + # Example: + # default = AIConfig( + # enabled=True, + # model=ModelConfig(name='gemini-pro'), + # provider=ProviderConfig(name='google'), + # messages=[LDMessage(role='system', content='You are a helpful assistant.')], + # ) + # config_value, tracker = aiclient.config(ai_config_key, context, default, {'myUserVariable': "Testing Variable"}) config_value, tracker = aiclient.config( ai_config_key, context, - default_value, - {'myUserVariable': "Testing Variable"} + variables={'myUserVariable': "Testing Variable"} ) if not config_value.enabled: diff --git a/examples/langchain_example.py b/examples/langchain_example.py index ba82e2a..e626626 100644 --- a/examples/langchain_example.py +++ b/examples/langchain_example.py @@ -83,26 +83,20 @@ def main(): .build() ) - DEFAULT_SYSTEM_MESSAGE = "You are a helpful assistant that can answer questions and help with tasks." - - # Set a fallback AIConfig to use if a config is not found or your application is not able to connect to LaunchDarkly. - default_value = AIConfig( - enabled=True, - model=ModelConfig(name='gpt-3.5-turbo', parameters={}), # Default to OpenAI - provider=ProviderConfig(name='openai'), - messages=[LDMessage(role='system', content=DEFAULT_SYSTEM_MESSAGE)], - ) - - # Optionally, you can use a disabled AIConfig - # default_value = AIConfig( - # enabled=False - # ) - + # Pass a default for improved resiliency when the AI config is unavailable + # or LaunchDarkly is unreachable; omit for a disabled default. + # Example: + # default = AIConfig( + # enabled=True, + # model=ModelConfig(name='gpt-4'), + # provider=ProviderConfig(name='openai'), + # messages=[LDMessage(role='system', content='You are a helpful assistant.')], + # ) + # config_value, tracker = aiclient.config(ai_config_key, context, default, {'myUserVariable': "Testing Variable"}) config_value, tracker = aiclient.config( ai_config_key, context, - default_value, - {'myUserVariable': "Testing Variable"} + variables={'myUserVariable': "Testing Variable"} ) if not config_value.enabled: diff --git a/examples/langgraph_agent_example.py b/examples/langgraph_agent_example.py index 0d77742..0f38ec4 100644 --- a/examples/langgraph_agent_example.py +++ b/examples/langgraph_agent_example.py @@ -85,16 +85,17 @@ def main(): print(f"🔍 Using agent config: {agent_config_key}") print() - # Create a LangChain model with LaunchDarkly AI config. - # Default value with disabled agent - default_value = LDAIAgentDefaults( - enabled=False, # Disabled by default - ) - - agent_config = aiclient.agent( + # Pass a default for improved resiliency when the agent config is unavailable + # or LaunchDarkly is unreachable; omit for a disabled default. + # Example (enabled default): + # default = LDAIAgentDefaults( + # enabled=True, + # instructions='You are a helpful assistant.', + # ) + # agent_config = aiclient.agent_config(LDAIAgentConfig(key=agent_config_key, default=default), context) + agent_config = aiclient.agent_config( LDAIAgentConfig( key=agent_config_key, - default_value=default_value, ), context ) diff --git a/examples/langgraph_multi_agent_example.py b/examples/langgraph_multi_agent_example.py index 5287a7f..8c1b0b0 100644 --- a/examples/langgraph_multi_agent_example.py +++ b/examples/langgraph_multi_agent_example.py @@ -73,14 +73,17 @@ def track_langgraph_metrics(tracker, func, prev_message_count=0): def create_agent_with_config(aiclient, config_key, context): """Create a LangChain model with LaunchDarkly AI config.""" - default_value = LDAIAgentDefaults( - enabled=False, # Disabled by default - ) - - agent_config = aiclient.agent( + # Pass a default for improved resiliency when the agent config is unavailable + # or LaunchDarkly is unreachable; omit for a disabled default. + # Example (enabled default): + # default = LDAIAgentDefaults( + # enabled=True, + # instructions='You are a helpful assistant.', + # ) + # agent_config = aiclient.agent_config(LDAIAgentConfig(key=config_key, default=default), context) + agent_config = aiclient.agent_config( LDAIAgentConfig( key=config_key, - default_value=default_value, ), context ) diff --git a/examples/openai_example.py b/examples/openai_example.py index 02904d6..f86672f 100755 --- a/examples/openai_example.py +++ b/examples/openai_example.py @@ -41,26 +41,20 @@ def main(): .build() ) - DEFAULT_SYSTEM_MESSAGE = "You are a helpful assistant that can answer questions and help with tasks." - - # Set a fallback AIConfig to use if a config is not found or your application is not able to connect to LaunchDarkly. - default_value = AIConfig( - enabled=True, - model=ModelConfig(name='my-default-model', parameters={}), - provider=ProviderConfig(name='openai'), - messages=[LDMessage(role='system', content=DEFAULT_SYSTEM_MESSAGE)], - ) - - # Optionally, you can use a disabled AIConfig - # default_value = AIConfig( - # enabled=False - # ) - + # Pass a default for improved resiliency when the AI config is unavailable + # or LaunchDarkly is unreachable; omit for a disabled default. + # Example: + # default = AIConfig( + # enabled=True, + # model=ModelConfig(name='gpt-4'), + # provider=ProviderConfig(name='openai'), + # messages=[LDMessage(role='system', content='You are a helpful assistant.')], + # ) + # config_value, tracker = aiclient.config(ai_config_key, context, default, {'myUserVariable': "Testing Variable"}) config_value, tracker = aiclient.config( ai_config_key, context, - default_value, - {'myUserVariable': "Testing Variable"} + variables={'myUserVariable': "Testing Variable"} ) if not config_value.enabled: From f95fd73ddaab6f063858bb0ced83f89401a41114 Mon Sep 17 00:00:00 2001 From: jsonbailey Date: Tue, 10 Mar 2026 11:00:32 -0500 Subject: [PATCH 2/2] add clarification on background judges --- examples/chat_judge_example.py | 4 ++++ examples/chat_observability_example.py | 10 +++++++++- examples/langgraph_agent_example.py | 2 +- pyproject.toml | 2 +- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/examples/chat_judge_example.py b/examples/chat_judge_example.py index f8db9ea..90a4d18 100644 --- a/examples/chat_judge_example.py +++ b/examples/chat_judge_example.py @@ -64,6 +64,10 @@ async def async_main(): chat_response = await chat.invoke(user_input) print("Chat Response:", chat_response.message.content) + # Judge evaluations run asynchronously. Await them (e.g. with asyncio.gather) so they + # complete before the process or request ends—even if you don't need to log or use + # the results. Below we await and then log the results for demonstration. + # Log judge evaluation results with full detail if chat_response.evaluations is not None and len(chat_response.evaluations) > 0: # Note: Judge evaluations run asynchronously and do not block your application. diff --git a/examples/chat_observability_example.py b/examples/chat_observability_example.py index 9841c66..9751eed 100644 --- a/examples/chat_observability_example.py +++ b/examples/chat_observability_example.py @@ -90,10 +90,18 @@ async def async_main(): user_input_2 = "Give me a specific use case example." print("\nUser Input:", user_input_2) - + response_2 = await chat.invoke(user_input_2) print("Chat Response:", response_2.message.content) + # Judge evaluations run asynchronously. Await them (e.g. with asyncio.gather) so they + # complete before the process or request ends—even if you don't need to log or use + # the results. + if response_1.evaluations: + await asyncio.gather(*response_1.evaluations) + if response_2.evaluations: + await asyncio.gather(*response_2.evaluations) + print("\nSuccess.") except Exception as err: diff --git a/examples/langgraph_agent_example.py b/examples/langgraph_agent_example.py index 0f38ec4..ebcc523 100644 --- a/examples/langgraph_agent_example.py +++ b/examples/langgraph_agent_example.py @@ -92,7 +92,7 @@ def main(): # enabled=True, # instructions='You are a helpful assistant.', # ) - # agent_config = aiclient.agent_config(LDAIAgentConfig(key=agent_config_key, default=default), context) + # agent_config = aiclient.agent_config(agent_config_key, context, default=default) agent_config = aiclient.agent_config( LDAIAgentConfig( key=agent_config_key, diff --git a/pyproject.toml b/pyproject.toml index d61c765..30cb7c9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,7 +20,7 @@ direct-judge-example = 'examples.direct_judge_example:main' [tool.poetry.dependencies] python = "^3.10" -launchdarkly-server-sdk-ai = "^0.14.0" +launchdarkly-server-sdk-ai = "^0.16.0" launchdarkly-server-sdk-ai-langchain = "^0.3.0" launchdarkly-server-sdk-ai-openai = "^0.1.0" launchdarkly-observability = { version = ">=0.1.0", optional = true }