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
5 changes: 3 additions & 2 deletions examples/bedrock_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
7 changes: 4 additions & 3 deletions examples/chat_judge_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})

Expand Down Expand Up @@ -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()


Expand Down
6 changes: 4 additions & 2 deletions examples/chat_observability_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand Down Expand Up @@ -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()


Expand Down
3 changes: 2 additions & 1 deletion examples/direct_judge_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
5 changes: 3 additions & 2 deletions examples/gemini_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__":
Expand Down
5 changes: 3 additions & 2 deletions examples/langchain_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()


Expand Down
5 changes: 3 additions & 2 deletions examples/langgraph_agent_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__":
Expand Down
5 changes: 3 additions & 2 deletions examples/langgraph_multi_agent_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__":
Expand Down
13 changes: 8 additions & 5 deletions examples/openai_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand All @@ -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()
Loading