diff --git a/examples/bedrock_example.py b/examples/bedrock_example.py index f3990e9..ad87994 100755 --- a/examples/bedrock_example.py +++ b/examples/bedrock_example.py @@ -2,7 +2,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai.client import LDAIClient +from ldai import LDAIClient import boto3 client = boto3.client("bedrock-runtime", region_name="us-east-1") @@ -85,5 +85,6 @@ def main(): # Continue the conversation by adding user input to the messages list and invoking the LLM again. print("Success.") - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() diff --git a/examples/chat_judge_example.py b/examples/chat_judge_example.py index 90a4d18..57094f8 100644 --- a/examples/chat_judge_example.py +++ b/examples/chat_judge_example.py @@ -47,8 +47,8 @@ async def async_main(): # 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={ + # chat = await aiclient.create_model(ai_config_key, context, default, {'companyName': 'LaunchDarkly'}) + chat = await aiclient.create_model(ai_config_key, context, variables={ 'companyName': 'LaunchDarkly', }) @@ -98,7 +98,8 @@ async def async_main(): except Exception as err: print("Error:", err) finally: - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() diff --git a/examples/chat_observability_example.py b/examples/chat_observability_example.py index 9751eed..296ec4d 100644 --- a/examples/chat_observability_example.py +++ b/examples/chat_observability_example.py @@ -67,8 +67,8 @@ async def async_main(): # 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( + # chat = await aiclient.create_model(ai_config_key, context, default, {'example_type': 'observability_demo'}) + chat = await aiclient.create_model( ai_config_key, context, variables={ @@ -107,6 +107,8 @@ async def async_main(): except Exception as err: print("Error:", err) finally: + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() diff --git a/examples/direct_judge_example.py b/examples/direct_judge_example.py index 510e41f..7d2faa6 100644 --- a/examples/direct_judge_example.py +++ b/examples/direct_judge_example.py @@ -86,7 +86,8 @@ async def async_main(): except Exception as err: print("Error:", err) finally: - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() diff --git a/examples/gemini_example.py b/examples/gemini_example.py index 5c2939e..d53735e 100644 --- a/examples/gemini_example.py +++ b/examples/gemini_example.py @@ -2,7 +2,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai.client import LDAIClient, LDMessage +from ldai import LDAIClient, LDMessage from ldai.tracker import TokenUsage from google import genai from google.genai import types @@ -162,7 +162,8 @@ def main(): # Continue the conversation by adding user input to the messages list and invoking the LLM again. print("Success.") - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() if __name__ == "__main__": diff --git a/examples/langchain_example.py b/examples/langchain_example.py index 0fe3f70..60e604f 100644 --- a/examples/langchain_example.py +++ b/examples/langchain_example.py @@ -3,7 +3,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai.client import LDAIClient +from ldai import LDAIClient from ldai_langchain import get_ai_metrics_from_response from langchain.chat_models import init_chat_model @@ -104,7 +104,8 @@ async def async_main(): print(f"Error during completion: {e}") print("Please ensure you have the correct API keys and credentials set up for the detected provider.") - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() diff --git a/examples/langgraph_agent_example.py b/examples/langgraph_agent_example.py index 93b072f..37f5212 100644 --- a/examples/langgraph_agent_example.py +++ b/examples/langgraph_agent_example.py @@ -3,7 +3,7 @@ from pprint import pprint from ldclient import Context from ldclient.config import Config -from ldai.client import LDAIClient +from ldai import LDAIClient from ldai.tracker import TokenUsage from ldai_langchain import get_ai_metrics_from_response from langchain.chat_models import init_chat_model @@ -123,7 +123,8 @@ def main(): print(f"Error: {e}") print("Please ensure you have the correct API keys and credentials set up for the detected providers.") - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() if __name__ == "__main__": diff --git a/examples/langgraph_multi_agent_example.py b/examples/langgraph_multi_agent_example.py index 6989b4a..424b30f 100644 --- a/examples/langgraph_multi_agent_example.py +++ b/examples/langgraph_multi_agent_example.py @@ -2,7 +2,7 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai.client import LDAIClient +from ldai import LDAIClient from ldai.tracker import TokenUsage from ldai_langchain import get_ai_metrics_from_response from langchain.chat_models import init_chat_model @@ -271,7 +271,8 @@ def calculate_average(numbers): print(f"❌ Error during workflow execution: {e}") print("Please ensure you have the correct API keys and credentials set up for the detected providers.") - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close() if __name__ == "__main__": diff --git a/examples/openai_example.py b/examples/openai_example.py index 2330ab2..168d8e8 100755 --- a/examples/openai_example.py +++ b/examples/openai_example.py @@ -2,7 +2,8 @@ import ldclient from ldclient import Context from ldclient.config import Config -from ldai.client import LDAIClient +from ldai import LDAIClient +from ldai_openai import get_ai_metrics_from_response from openai import OpenAI openai_client = OpenAI() @@ -69,13 +70,14 @@ def main(): print("User Input:\n", USER_INPUT) messages.append({'role': 'user', 'content': USER_INPUT}) - # Track the OpenAI completion with LaunchDarkly metrics - completion = tracker.track_openai_metrics( + # Track the OpenAI completion with LaunchDarkly metrics using the LD OpenAI provider's extractor + completion = tracker.track_metrics_of( lambda: openai_client.chat.completions.create( model=config_value.model.name, messages=messages, - ) + ), + get_ai_metrics_from_response, ) ai_response = completion.choices[0].message.content @@ -86,5 +88,6 @@ def main(): # Continue the conversation by adding user input to the messages list and invoking the LLM again. print("Success.") - # Close the client to flush events and close the connection. + # Flush pending events and close the client. + ldclient.get().flush() ldclient.get().close()