|
1 | 1 | import os |
2 | 2 | from dotenv import load_dotenv |
3 | | -import json |
4 | 3 | import asyncio |
5 | 4 | import ldclient |
6 | 5 | from ldclient import Context |
@@ -63,33 +62,38 @@ async def async_main(): |
63 | 62 | user_input = 'How can LaunchDarkly help me?' |
64 | 63 | print("User Input:", user_input) |
65 | 64 |
|
66 | | - # The invoke method will automatically evaluate the chat response with any judges defined in the AI config |
67 | | - chat_response = await chat.invoke(user_input) |
68 | | - print("Chat Response:", chat_response.message.content) |
| 65 | + # The run method will automatically evaluate the chat response with any judges defined in the AI config |
| 66 | + chat_response = await chat.run(user_input) |
| 67 | + print("Chat Response:", chat_response.content) |
69 | 68 |
|
70 | | - # Judge evaluations run asynchronously. Await them (e.g. with asyncio.gather) so they |
71 | | - # complete before the process or request ends—even if you don't need to log or use |
72 | | - # the results. Below we await and then log the results for demonstration. |
| 69 | + # Judge evaluations run asynchronously. Await them so they complete before the |
| 70 | + # process or request ends—even if you don't need to log or use the results. |
| 71 | + # Below we await and then log the results for demonstration. |
73 | 72 |
|
74 | 73 | # Log judge evaluation results with full detail |
75 | | - if chat_response.evaluations is not None and len(chat_response.evaluations) > 0: |
| 74 | + if chat_response.evaluations is not None: |
76 | 75 | # Note: Judge evaluations run asynchronously and do not block your application. |
77 | 76 | # Results are automatically sent to LaunchDarkly for AI config metrics. |
78 | 77 | # You only need to await if you want to access the evaluation results in your code. |
79 | 78 | print("\nNote: Awaiting judge results (optional - done here for demonstration only).") |
80 | | - eval_results = await asyncio.gather(*chat_response.evaluations) |
81 | | - |
82 | | - # Convert results, replacing None with a message |
83 | | - results_to_display = [ |
84 | | - result.to_dict() if result is not None else "not evaluated" |
85 | | - for result in eval_results |
86 | | - ] |
87 | | - |
| 79 | + eval_results = await chat_response.evaluations |
| 80 | + |
88 | 81 | print("Judge results:") |
89 | | - print(json.dumps(results_to_display, indent=2, default=str)) |
90 | | - |
91 | | - if None in eval_results: |
92 | | - print("\nNote: Some judge evaluations were skipped.") |
| 82 | + for result in eval_results: |
| 83 | + print(f" - sampled: {result.sampled}") |
| 84 | + print(f" success: {result.success}") |
| 85 | + if result.error_message is not None: |
| 86 | + print(f" error_message: {result.error_message}") |
| 87 | + if result.metric_key is not None: |
| 88 | + print(f" metric_key: {result.metric_key}") |
| 89 | + if result.score is not None: |
| 90 | + print(f" score: {result.score}") |
| 91 | + if result.reasoning is not None: |
| 92 | + print(f" reasoning: {result.reasoning}") |
| 93 | + |
| 94 | + skipped = [r for r in eval_results if not r.sampled] |
| 95 | + if skipped: |
| 96 | + print("\nNote: Some judge evaluations were skipped (not sampled).") |
93 | 97 | print("This typically happens when the sample rate doesn't require this evaluation, or due to a configuration issue.") |
94 | 98 | print("Check application logs for more details.") |
95 | 99 | else: |
|
0 commit comments