Skip to content

Commit 3aaca7d

Browse files
jsonbaileyclaude
andcommitted
chore: update deprecated API usage in hello-python-ai examples
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent aeda568 commit 3aaca7d

9 files changed

Lines changed: 30 additions & 18 deletions

examples/bedrock_example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,6 @@ def main():
8585
# Continue the conversation by adding user input to the messages list and invoking the LLM again.
8686
print("Success.")
8787

88-
# Close the client to flush events and close the connection.
88+
# Flush pending events and close the client.
89+
ldclient.get().flush()
8990
ldclient.get().close()

examples/chat_judge_example.py

Lines changed: 5 additions & 4 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.client import LDAIClient, AICompletionConfigDefault
88

99
# Set sdk_key to your LaunchDarkly SDK key.
1010
sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY')
@@ -47,8 +47,8 @@ async def async_main():
4747
# provider={'name': 'openai'},
4848
# messages=[{'role': 'system', 'content': 'You are a helpful assistant.'}],
4949
# )
50-
# chat = await aiclient.create_chat(ai_config_key, context, default, {'companyName': 'LaunchDarkly'})
51-
chat = await aiclient.create_chat(ai_config_key, context, variables={
50+
# chat = await aiclient.create_model(ai_config_key, context, default, {'companyName': 'LaunchDarkly'})
51+
chat = await aiclient.create_model(ai_config_key, context, variables={
5252
'companyName': 'LaunchDarkly',
5353
})
5454

@@ -98,7 +98,8 @@ async def async_main():
9898
except Exception as err:
9999
print("Error:", err)
100100
finally:
101-
# Close the client to flush events and close the connection.
101+
# Flush pending events and close the client.
102+
ldclient.get().flush()
102103
ldclient.get().close()
103104

104105

examples/chat_observability_example.py

Lines changed: 5 additions & 3 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.client import LDAIClient, AICompletionConfigDefault
88
from ldobserve import ObservabilityConfig, ObservabilityPlugin
99

1010
logging.getLogger('ldclient').setLevel(logging.WARNING)
@@ -67,8 +67,8 @@ async def async_main():
6767
# provider={'name': 'openai'},
6868
# messages=[{'role': 'system', 'content': 'You are a helpful assistant.'}],
6969
# )
70-
# chat = await aiclient.create_chat(ai_config_key, context, default, {'example_type': 'observability_demo'})
71-
chat = await aiclient.create_chat(
70+
# chat = await aiclient.create_model(ai_config_key, context, default, {'example_type': 'observability_demo'})
71+
chat = await aiclient.create_model(
7272
ai_config_key,
7373
context,
7474
variables={
@@ -107,6 +107,8 @@ async def async_main():
107107
except Exception as err:
108108
print("Error:", err)
109109
finally:
110+
# Flush pending events and close the client.
111+
ldclient.get().flush()
110112
ldclient.get().close()
111113

112114

examples/direct_judge_example.py

Lines changed: 3 additions & 2 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, AIJudgeConfigDefault
7+
from ldai.client import LDAIClient, AIJudgeConfigDefault
88

99
# Set sdk_key to your LaunchDarkly SDK key.
1010
sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY')
@@ -86,7 +86,8 @@ async def async_main():
8686
except Exception as err:
8787
print("Error:", err)
8888
finally:
89-
# Close the client to flush events and close the connection.
89+
# Flush pending events and close the client.
90+
ldclient.get().flush()
9091
ldclient.get().close()
9192

9293

examples/gemini_example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ def main():
162162
# Continue the conversation by adding user input to the messages list and invoking the LLM again.
163163
print("Success.")
164164

165-
# Close the client to flush events and close the connection.
165+
# Flush pending events and close the client.
166+
ldclient.get().flush()
166167
ldclient.get().close()
167168

168169
if __name__ == "__main__":

examples/langchain_example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ async def async_main():
104104
print(f"Error during completion: {e}")
105105
print("Please ensure you have the correct API keys and credentials set up for the detected provider.")
106106

107-
# Close the client to flush events and close the connection.
107+
# Flush pending events and close the client.
108+
ldclient.get().flush()
108109
ldclient.get().close()
109110

110111

examples/langgraph_agent_example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ def main():
123123
print(f"Error: {e}")
124124
print("Please ensure you have the correct API keys and credentials set up for the detected providers.")
125125

126-
# Close the client to flush events and close the connection.
126+
# Flush pending events and close the client.
127+
ldclient.get().flush()
127128
ldclient.get().close()
128129

129130
if __name__ == "__main__":

examples/langgraph_multi_agent_example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ def calculate_average(numbers):
271271
print(f"❌ Error during workflow execution: {e}")
272272
print("Please ensure you have the correct API keys and credentials set up for the detected providers.")
273273

274-
# Close the client to flush events and close the connection.
274+
# Flush pending events and close the client.
275+
ldclient.get().flush()
275276
ldclient.get().close()
276277

277278
if __name__ == "__main__":

examples/openai_example.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from ldclient import Context
44
from ldclient.config import Config
55
from ldai.client import LDAIClient
6+
from ldai_openai import get_ai_metrics_from_response
67
from openai import OpenAI
78

89
openai_client = OpenAI()
@@ -69,13 +70,14 @@ def main():
6970
print("User Input:\n", USER_INPUT)
7071
messages.append({'role': 'user', 'content': USER_INPUT})
7172

72-
# Track the OpenAI completion with LaunchDarkly metrics
73-
completion = tracker.track_openai_metrics(
73+
# Track the OpenAI completion with LaunchDarkly metrics using the LD OpenAI provider's extractor
74+
completion = tracker.track_metrics_of(
7475
lambda:
7576
openai_client.chat.completions.create(
7677
model=config_value.model.name,
7778
messages=messages,
78-
)
79+
),
80+
get_ai_metrics_from_response,
7981
)
8082
ai_response = completion.choices[0].message.content
8183

@@ -86,5 +88,6 @@ def main():
8688
# Continue the conversation by adding user input to the messages list and invoking the LLM again.
8789
print("Success.")
8890

89-
# Close the client to flush events and close the connection.
91+
# Flush pending events and close the client.
92+
ldclient.get().flush()
9093
ldclient.get().close()

0 commit comments

Comments
 (0)