|
1 | 1 | import os |
| 2 | +import logging |
2 | 3 | from dotenv import load_dotenv |
3 | 4 | import ldclient |
4 | 5 | from ldclient import Context |
5 | 6 | from ldclient.config import Config |
6 | 7 | from ldai import LDAIClient |
| 8 | +from ldai.tracker import TokenUsage |
| 9 | +from ldai.providers import LDAIMetrics |
7 | 10 | import boto3 |
8 | 11 |
|
9 | 12 | load_dotenv() |
10 | 13 |
|
| 14 | +logging.basicConfig() |
| 15 | +logging.getLogger('ldclient').setLevel(logging.WARNING) |
| 16 | + |
11 | 17 | client = boto3.client("bedrock-runtime", region_name=os.getenv('AWS_DEFAULT_REGION', 'us-east-1')) |
12 | 18 |
|
| 19 | + |
| 20 | +def get_bedrock_metrics(response): |
| 21 | + """Extract metrics from a Bedrock converse response.""" |
| 22 | + status_code = response.get("ResponseMetadata", {}).get("HTTPStatusCode", 0) |
| 23 | + success = status_code == 200 |
| 24 | + |
| 25 | + usage = None |
| 26 | + if response.get("usage"): |
| 27 | + u = response["usage"] |
| 28 | + usage = TokenUsage( |
| 29 | + total=u.get("totalTokens", 0), |
| 30 | + input=u.get("inputTokens", 0), |
| 31 | + output=u.get("outputTokens", 0), |
| 32 | + ) |
| 33 | + |
| 34 | + duration_ms = response.get("metrics", {}).get("latencyMs") |
| 35 | + |
| 36 | + return LDAIMetrics(success=success, usage=usage, duration_ms=duration_ms) |
| 37 | + |
13 | 38 | # Set sdk_key to your LaunchDarkly SDK key. |
14 | 39 | sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY') |
15 | 40 |
|
16 | 41 | # Set config_key to the AI Config key you want to evaluate. |
17 | | -ai_config_key = os.getenv('LAUNCHDARKLY_AI_CONFIG_KEY', 'sample-ai-config') |
| 42 | +ai_config_key = os.getenv('LAUNCHDARKLY_AI_CONFIG_KEY', 'sample-completion-config') |
18 | 43 |
|
19 | 44 | def main(): |
20 | 45 | if not sdk_key: |
@@ -69,25 +94,35 @@ def main(): |
69 | 94 | chat_messages = [{'role': msg.role, 'content': [{'text': msg.content}]} for msg in config_value.messages if msg.role != 'system'] |
70 | 95 | system_messages = [{'text': msg.content} for msg in config_value.messages if msg.role == 'system'] |
71 | 96 |
|
72 | | - # Add the user input to the conversation |
73 | | - USER_INPUT = "What can you help me with?" |
74 | | - print("User Input:\n", USER_INPUT) |
75 | | - chat_messages.append({'role': 'user', 'content': [{'text': USER_INPUT}]}) |
| 97 | + SAMPLE_QUESTION = "What can you help me with?" |
| 98 | + chat_messages.append({'role': 'user', 'content': [{'text': SAMPLE_QUESTION}]}) |
76 | 99 |
|
77 | | - converse = tracker.track_bedrock_converse_metrics( |
78 | | - client.converse( |
| 100 | + print(f'\nSending sample question to {config_value.model.name}: "{SAMPLE_QUESTION}"') |
| 101 | + print("Waiting for response...") |
| 102 | + |
| 103 | + converse = tracker.track_metrics_of( |
| 104 | + get_bedrock_metrics, |
| 105 | + lambda: client.converse( |
79 | 106 | modelId=config_value.model.name, |
80 | 107 | messages=chat_messages, |
81 | 108 | system=system_messages, |
82 | | - ) |
| 109 | + ), |
83 | 110 | ) |
84 | 111 |
|
85 | | - # Append the AI response to the conversation history |
86 | 112 | chat_messages.append(converse["output"]["message"]) |
87 | | - print("AI Response:\n", converse["output"]["message"]["content"][0]["text"]) |
88 | 113 |
|
89 | | - # Continue the conversation by adding user input to the messages list and invoking the LLM again. |
90 | | - print("Success.") |
| 114 | + print(f"\nModel response:\n{converse['output']['message']['content'][0]['text']}") |
| 115 | + |
| 116 | + summary = tracker.get_summary() |
| 117 | + print("\nDone! The AI config was evaluated and the following metrics were tracked:") |
| 118 | + print(f" Duration: {summary.duration_ms}ms") |
| 119 | + print(f" Success: {summary.success}") |
| 120 | + if summary.usage: |
| 121 | + print(f" Input tokens: {summary.usage.input}") |
| 122 | + print(f" Output tokens: {summary.usage.output}") |
| 123 | + print(f" Total tokens: {summary.usage.total}") |
| 124 | + if summary.tool_calls: |
| 125 | + print(f" Tool calls: {', '.join(summary.tool_calls)}") |
91 | 126 |
|
92 | 127 | # Flush pending events and close the client. |
93 | 128 | ldclient.get().flush() |
|
0 commit comments