Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions examples/bedrock_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 15 additions & 6 deletions examples/chat_judge_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})

Expand All @@ -59,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.
Expand Down
30 changes: 22 additions & 8 deletions examples/chat_observability_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -84,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:
Expand Down
23 changes: 16 additions & 7 deletions examples/direct_judge_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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}")
Expand Down
28 changes: 11 additions & 17 deletions examples/gemini_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 11 additions & 17 deletions examples/langchain_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 9 additions & 8 deletions examples/langgraph_agent_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(agent_config_key, context, default=default)
agent_config = aiclient.agent_config(
LDAIAgentConfig(
key=agent_config_key,
default_value=default_value,
),
context
)
Expand Down
15 changes: 9 additions & 6 deletions examples/langgraph_multi_agent_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
28 changes: 11 additions & 17 deletions examples/openai_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ direct-judge-example = 'examples.direct_judge_example:main'

[tool.poetry.dependencies]
python = "^3.10"
launchdarkly-server-sdk-ai = "^0.15.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 }
Expand Down
Loading