Skip to content

Commit 84d3c2d

Browse files
authored
chore: Improve default examples (#17)
1 parent df885c3 commit 84d3c2d

10 files changed

Lines changed: 116 additions & 104 deletions

examples/bedrock_example.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,20 @@ def main():
4040
.build()
4141
)
4242

43-
DEFAULT_SYSTEM_MESSAGE = "You are a helpful assistant that can answer questions and help with tasks."
44-
45-
# Set a fallback AIConfig to use if a config is not found or your application is not able to connect to LaunchDarkly.
46-
default_value = AIConfig(
47-
enabled=True,
48-
model=ModelConfig(name='my-default-model', parameters={}),
49-
provider=ProviderConfig(name='bedrock'),
50-
messages=[LDMessage(role='system', content=DEFAULT_SYSTEM_MESSAGE)],
51-
)
52-
53-
# Optionally, you can use a disabled AIConfig
54-
# default_value = AIConfig(
55-
# enabled=False
56-
# )
57-
43+
# Pass a default for improved resiliency when the AI config is unavailable
44+
# or LaunchDarkly is unreachable; omit for a disabled default.
45+
# Example:
46+
# default = AIConfig(
47+
# enabled=True,
48+
# model=ModelConfig(name='my-default-model'),
49+
# provider=ProviderConfig(name='bedrock'),
50+
# messages=[LDMessage(role='system', content='You are a helpful assistant.')],
51+
# )
52+
# config_value, tracker = aiclient.config(ai_config_key, context, default, {'myUserVariable': "Testing Variable"})
5853
config_value, tracker = aiclient.config(
5954
ai_config_key,
6055
context,
61-
default_value,
62-
{'myUserVariable': "Testing Variable"}
56+
variables={'myUserVariable': "Testing Variable"}
6357
)
6458

6559
if not config_value.enabled:

examples/chat_judge_example.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@ async def async_main():
3838
)
3939

4040
try:
41-
# Example using the chat functionality which automates the judge evaluation
42-
default_value = AICompletionConfigDefault(
43-
enabled=False,
44-
)
45-
46-
chat = await aiclient.create_chat(ai_config_key, context, default_value, {
41+
# Pass a default for improved resiliency when the AI config is unavailable
42+
# or LaunchDarkly is unreachable; omit for a disabled default.
43+
# Example:
44+
# default = AICompletionConfigDefault(
45+
# enabled=True,
46+
# model={'name': 'gpt-4'},
47+
# provider={'name': 'openai'},
48+
# messages=[{'role': 'system', 'content': 'You are a helpful assistant.'}],
49+
# )
50+
# chat = await aiclient.create_chat(ai_config_key, context, default, {'companyName': 'LaunchDarkly'})
51+
chat = await aiclient.create_chat(ai_config_key, context, variables={
4752
'companyName': 'LaunchDarkly',
4853
})
4954

@@ -59,6 +64,10 @@ async def async_main():
5964
chat_response = await chat.invoke(user_input)
6065
print("Chat Response:", chat_response.message.content)
6166

67+
# Judge evaluations run asynchronously. Await them (e.g. with asyncio.gather) so they
68+
# complete before the process or request ends—even if you don't need to log or use
69+
# the results. Below we await and then log the results for demonstration.
70+
6271
# Log judge evaluation results with full detail
6372
if chat_response.evaluations is not None and len(chat_response.evaluations) > 0:
6473
# Note: Judge evaluations run asynchronously and do not block your application.

examples/chat_observability_example.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,20 @@ async def async_main():
5858
)
5959

6060
try:
61-
# Create a chat instance with custom variables
62-
default_value = AICompletionConfigDefault(enabled=False)
63-
61+
# Pass a default for improved resiliency when the AI config is unavailable
62+
# or LaunchDarkly is unreachable; omit for a disabled default.
63+
# Example:
64+
# default = AICompletionConfigDefault(
65+
# enabled=True,
66+
# model={'name': 'gpt-4'},
67+
# provider={'name': 'openai'},
68+
# messages=[{'role': 'system', 'content': 'You are a helpful assistant.'}],
69+
# )
70+
# chat = await aiclient.create_chat(ai_config_key, context, default, {'example_type': 'observability_demo'})
6471
chat = await aiclient.create_chat(
65-
ai_config_key,
66-
context,
67-
default_value,
68-
{
72+
ai_config_key,
73+
context,
74+
variables={
6975
'example_type': 'observability_demo',
7076
'session_id': 'demo-session-123',
7177
'feature': 'ai_chat'
@@ -84,10 +90,18 @@ async def async_main():
8490

8591
user_input_2 = "Give me a specific use case example."
8692
print("\nUser Input:", user_input_2)
87-
93+
8894
response_2 = await chat.invoke(user_input_2)
8995
print("Chat Response:", response_2.message.content)
9096

97+
# Judge evaluations run asynchronously. Await them (e.g. with asyncio.gather) so they
98+
# complete before the process or request ends—even if you don't need to log or use
99+
# the results.
100+
if response_1.evaluations:
101+
await asyncio.gather(*response_1.evaluations)
102+
if response_2.evaluations:
103+
await asyncio.gather(*response_2.evaluations)
104+
91105
print("\nSuccess.")
92106

93107
except Exception as err:

examples/direct_judge_example.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import ldclient
55
from ldclient import Context
66
from ldclient.config import Config
7-
from ldai import LDAIClient, AICompletionConfigDefault
7+
from ldai import LDAIClient, AIJudgeConfigDefault
88

99
# Set sdk_key to your LaunchDarkly SDK key.
1010
sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY')
@@ -38,12 +38,21 @@ async def async_main():
3838
)
3939

4040
try:
41-
# Example of using the judge functionality with direct input and output
42-
# Get AI judge configuration from LaunchDarkly
43-
judge_default_value = AICompletionConfigDefault(
44-
enabled=False,
45-
)
46-
judge = await aiclient.create_judge(judge_key, context, judge_default_value)
41+
# Pass a default for improved resiliency when the AI config is unavailable
42+
# or LaunchDarkly is unreachable; omit for a disabled default.
43+
# Example (enabled default; judge default has three messages):
44+
# default = AIJudgeConfigDefault(
45+
# enabled=True,
46+
# model={'name': 'gpt-4'},
47+
# provider={'name': 'openai'},
48+
# messages=[
49+
# {'role': 'system', 'content': 'Your judge criteria here.'},
50+
# {'role': 'assistant', 'content': 'MESSAGE HISTORY: {{message_history}}'},
51+
# {'role': 'user', 'content': 'RESPONSE TO EVALUATE: {{response_to_evaluate}}'},
52+
# ],
53+
# )
54+
# judge = await aiclient.create_judge(judge_key, context, default)
55+
judge = await aiclient.create_judge(judge_key, context)
4756

4857
if not judge:
4958
print(f"*** AI judge configuration is not enabled for key: {judge_key}")

examples/gemini_example.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,26 +110,20 @@ def main():
110110
.build()
111111
)
112112

113-
DEFAULT_SYSTEM_MESSAGE = "You are a helpful assistant that can answer questions and help with tasks."
114-
115-
# Set a fallback AIConfig to use if a config is not found or your application is not able to connect to LaunchDarkly.
116-
default_value = AIConfig(
117-
enabled=True,
118-
model=ModelConfig(name='gemini-pro', parameters={}),
119-
provider=ProviderConfig(name='google'),
120-
messages=[LDMessage(role='system', content=DEFAULT_SYSTEM_MESSAGE)],
121-
)
122-
123-
# Optionally, you can use a disabled AIConfig
124-
# default_value = AIConfig(
125-
# enabled=False
126-
# )
127-
113+
# Pass a default for improved resiliency when the AI config is unavailable
114+
# or LaunchDarkly is unreachable; omit for a disabled default.
115+
# Example:
116+
# default = AIConfig(
117+
# enabled=True,
118+
# model=ModelConfig(name='gemini-pro'),
119+
# provider=ProviderConfig(name='google'),
120+
# messages=[LDMessage(role='system', content='You are a helpful assistant.')],
121+
# )
122+
# config_value, tracker = aiclient.config(ai_config_key, context, default, {'myUserVariable': "Testing Variable"})
128123
config_value, tracker = aiclient.config(
129124
ai_config_key,
130125
context,
131-
default_value,
132-
{'myUserVariable': "Testing Variable"}
126+
variables={'myUserVariable': "Testing Variable"}
133127
)
134128

135129
if not config_value.enabled:

examples/langchain_example.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,20 @@ def main():
8383
.build()
8484
)
8585

86-
DEFAULT_SYSTEM_MESSAGE = "You are a helpful assistant that can answer questions and help with tasks."
87-
88-
# Set a fallback AIConfig to use if a config is not found or your application is not able to connect to LaunchDarkly.
89-
default_value = AIConfig(
90-
enabled=True,
91-
model=ModelConfig(name='gpt-3.5-turbo', parameters={}), # Default to OpenAI
92-
provider=ProviderConfig(name='openai'),
93-
messages=[LDMessage(role='system', content=DEFAULT_SYSTEM_MESSAGE)],
94-
)
95-
96-
# Optionally, you can use a disabled AIConfig
97-
# default_value = AIConfig(
98-
# enabled=False
99-
# )
100-
86+
# Pass a default for improved resiliency when the AI config is unavailable
87+
# or LaunchDarkly is unreachable; omit for a disabled default.
88+
# Example:
89+
# default = AIConfig(
90+
# enabled=True,
91+
# model=ModelConfig(name='gpt-4'),
92+
# provider=ProviderConfig(name='openai'),
93+
# messages=[LDMessage(role='system', content='You are a helpful assistant.')],
94+
# )
95+
# config_value, tracker = aiclient.config(ai_config_key, context, default, {'myUserVariable': "Testing Variable"})
10196
config_value, tracker = aiclient.config(
10297
ai_config_key,
10398
context,
104-
default_value,
105-
{'myUserVariable': "Testing Variable"}
99+
variables={'myUserVariable': "Testing Variable"}
106100
)
107101

108102
if not config_value.enabled:

examples/langgraph_agent_example.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,17 @@ def main():
8585
print(f"🔍 Using agent config: {agent_config_key}")
8686
print()
8787

88-
# Create a LangChain model with LaunchDarkly AI config.
89-
# Default value with disabled agent
90-
default_value = LDAIAgentDefaults(
91-
enabled=False, # Disabled by default
92-
)
93-
94-
agent_config = aiclient.agent(
88+
# Pass a default for improved resiliency when the agent config is unavailable
89+
# or LaunchDarkly is unreachable; omit for a disabled default.
90+
# Example (enabled default):
91+
# default = LDAIAgentDefaults(
92+
# enabled=True,
93+
# instructions='You are a helpful assistant.',
94+
# )
95+
# agent_config = aiclient.agent_config(agent_config_key, context, default=default)
96+
agent_config = aiclient.agent_config(
9597
LDAIAgentConfig(
9698
key=agent_config_key,
97-
default_value=default_value,
9899
),
99100
context
100101
)

examples/langgraph_multi_agent_example.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,17 @@ def track_langgraph_metrics(tracker, func, prev_message_count=0):
7373

7474
def create_agent_with_config(aiclient, config_key, context):
7575
"""Create a LangChain model with LaunchDarkly AI config."""
76-
default_value = LDAIAgentDefaults(
77-
enabled=False, # Disabled by default
78-
)
79-
80-
agent_config = aiclient.agent(
76+
# Pass a default for improved resiliency when the agent config is unavailable
77+
# or LaunchDarkly is unreachable; omit for a disabled default.
78+
# Example (enabled default):
79+
# default = LDAIAgentDefaults(
80+
# enabled=True,
81+
# instructions='You are a helpful assistant.',
82+
# )
83+
# agent_config = aiclient.agent_config(LDAIAgentConfig(key=config_key, default=default), context)
84+
agent_config = aiclient.agent_config(
8185
LDAIAgentConfig(
8286
key=config_key,
83-
default_value=default_value,
8487
),
8588
context
8689
)

examples/openai_example.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,20 @@ def main():
4141
.build()
4242
)
4343

44-
DEFAULT_SYSTEM_MESSAGE = "You are a helpful assistant that can answer questions and help with tasks."
45-
46-
# Set a fallback AIConfig to use if a config is not found or your application is not able to connect to LaunchDarkly.
47-
default_value = AIConfig(
48-
enabled=True,
49-
model=ModelConfig(name='my-default-model', parameters={}),
50-
provider=ProviderConfig(name='openai'),
51-
messages=[LDMessage(role='system', content=DEFAULT_SYSTEM_MESSAGE)],
52-
)
53-
54-
# Optionally, you can use a disabled AIConfig
55-
# default_value = AIConfig(
56-
# enabled=False
57-
# )
58-
44+
# Pass a default for improved resiliency when the AI config is unavailable
45+
# or LaunchDarkly is unreachable; omit for a disabled default.
46+
# Example:
47+
# default = AIConfig(
48+
# enabled=True,
49+
# model=ModelConfig(name='gpt-4'),
50+
# provider=ProviderConfig(name='openai'),
51+
# messages=[LDMessage(role='system', content='You are a helpful assistant.')],
52+
# )
53+
# config_value, tracker = aiclient.config(ai_config_key, context, default, {'myUserVariable': "Testing Variable"})
5954
config_value, tracker = aiclient.config(
6055
ai_config_key,
6156
context,
62-
default_value,
63-
{'myUserVariable': "Testing Variable"}
57+
variables={'myUserVariable': "Testing Variable"}
6458
)
6559

6660
if not config_value.enabled:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ direct-judge-example = 'examples.direct_judge_example:main'
2020

2121
[tool.poetry.dependencies]
2222
python = "^3.10"
23-
launchdarkly-server-sdk-ai = "^0.15.0"
23+
launchdarkly-server-sdk-ai = "^0.16.0"
2424
launchdarkly-server-sdk-ai-langchain = "^0.3.0"
2525
launchdarkly-server-sdk-ai-openai = "^0.1.0"
2626
launchdarkly-observability = { version = ">=0.1.0", optional = true }

0 commit comments

Comments
 (0)