| 1 | import datetime |
| 2 | import json |
| 3 | import uuid |
| 4 | |
| 5 | from swarm import Swarm |
| 6 | |
| 7 | |
| 8 | def run_function_evals(agent, test_cases, n=1, eval_path=None): |
| 9 | correct_function = 0 |
| 10 | results = [] |
| 11 | eval_id = str(uuid.uuid4()) |
| 12 | eval_timestamp = datetime.datetime.now().isoformat() |
| 13 | client = Swarm() |
| 14 | |
| 15 | for test_case in test_cases: |
| 16 | case_correct = 0 |
| 17 | case_results = { |
| 18 | "messages": test_case["conversation"], |
| 19 | "expected_function": test_case["function"], |
| 20 | "actual_function": [], |
| 21 | "actual_message": [], |
| 22 | } |
| 23 | print(50 * "--") |
| 24 | print(f"\033[94mConversation: \033[0m{test_case['conversation']}\n") |
| 25 | for i in range(n): |
| 26 | print(f"\033[90mIteration: {i + 1}/{n}\033[0m") |
| 27 | response = client.run( |
| 28 | agent=agent, messages=test_case["conversation"], max_turns=1 |
| 29 | ) |
| 30 | output = extract_response_info(response) |
| 31 | actual_function = output.get("tool_calls", "None") |
| 32 | actual_message = output.get("message", "None") |
| 33 | |
| 34 | case_results["actual_function"].append(actual_function) |
| 35 | case_results["actual_message"].append(actual_message) |
| 36 | |
| 37 | if "tool_calls" in output: |
| 38 | print( |
| 39 | f'\033[95mExpected function: \033[0m {test_case["function"]}, \033[95mGot: \033[0m{output["tool_calls"]}\n' |
| 40 | ) |
| 41 | if output["tool_calls"] == test_case["function"]: |
| 42 | case_correct += 1 |
| 43 | correct_function += 1 |
| 44 | |
| 45 | elif "message" in output: |
| 46 | print( |
| 47 | f'\033[95mExpected function: \033[0m {test_case["function"]}, \033[95mGot: \033[0mNone' |
| 48 | ) |
| 49 | print(f'\033[90mMessage: {output["message"]}\033[0m\n') |
| 50 | if test_case["function"] == "None": |
| 51 | case_correct += 1 |
| 52 | correct_function += 1 |
| 53 | |
| 54 | case_accuracy = (case_correct / n) * 100 |
| 55 | case_results["case_accuracy"] = f"{case_accuracy:.2f}%" |
| 56 | results.append(case_results) |
| 57 | |
| 58 | print( |
| 59 | f"\033[92mCorrect functions for this case: {case_correct} out of {n}\033[0m" |
| 60 | ) |
| 61 | print(f"\033[93mAccuracy for this case: {case_accuracy:.2f}%\033[0m") |
| 62 | overall_accuracy = (correct_function / (len(test_cases) * n)) * 100 |
| 63 | print(50 * "**") |
| 64 | print( |
| 65 | f"\n\033[92mOVERALL: Correct functions selected: {correct_function} out of {len(test_cases) * n}\033[0m" |
| 66 | ) |
| 67 | print(f"\033[93mOVERALL: Accuracy: {overall_accuracy:.2f}%\033[0m") |
| 68 | |
| 69 | final_result = { |
| 70 | "id": eval_id, |
| 71 | "timestamp": eval_timestamp, |
| 72 | "results": results, |
| 73 | "correct_evals": correct_function, |
| 74 | "total_evals": len(test_cases) * n, |
| 75 | "overall_accuracy_percent": f"{overall_accuracy:.2f}%", |
| 76 | } |
| 77 | |
| 78 | if eval_path: |
| 79 | try: |
| 80 | with open(eval_path, "r") as file: |
| 81 | existing_data = json.load(file) |
| 82 | except FileNotFoundError: |
| 83 | existing_data = [] |
| 84 | |
| 85 | if not isinstance(existing_data, list): |
| 86 | existing_data = [existing_data] |
| 87 | |
| 88 | existing_data.append(final_result) |
| 89 | |
| 90 | with open(eval_path, "w") as file: |
| 91 | json.dump(existing_data, file, indent=4) |
| 92 | |
| 93 | return overall_accuracy |
| 94 | |
| 95 | return overall_accuracy |
| 96 | |
| 97 | |
| 98 | def extract_response_info(response): |
| 99 | results = {} |
| 100 | for message in response.messages: |
| 101 | if message["role"] == "tool": |
| 102 | results["tool_calls"] = message["tool_name"] |
| 103 | break |
| 104 | elif not message["tool_calls"]: |
| 105 | results["message"] = message["content"] |
| 106 | return results |