Write test cases, run the agent on each, and get a score, so you know it really works.
1# part3/testing_agents.py
2import json
3from openai import OpenAI
4client = OpenAI()
5
6# --- The agent under test: a calculator with one tool ---
7def add(a, b):
8 return a + b
9
10tools = [{"type": "function", "function": {
11 "name": "add",
12 "description": "Add two numbers together",
13 "parameters": {
14 "type": "object", "required": ["a", "b"],
15 "properties": {"a": {"type": "number"},
16 "b": {"type": "number"}}},
17}}]
18
19def ask_agent(question): # returns (tool_used, answer)
20 messages = [{"role": "user", "content": question}]
21 response = client.chat.completions.create(
22 model="gpt-4o-mini", messages=messages,
23 tools=tools, temperature=0)
24 message = response.choices[0].message
25 if not message.tool_calls:
26 return None, message.content # no tool used
27 tool_call = message.tool_calls[0] # asked for add
28 arguments = json.loads(tool_call.function.arguments)
29 result = add(arguments["a"], arguments["b"])
30 messages.append(message)
31 messages.append({"role": "tool",
32 "tool_call_id": tool_call.id,
33 "content": str(result)})
34 final = client.chat.completions.create(
35 model="gpt-4o-mini", messages=messages,
36 temperature=0)
37 answer = final.choices[0].message.content
38 return tool_call.function.name, answer
39
40# --- Tests: a question + what a good answer must have ---
41tests = [
42 {"question": "What is 45 + 13?",
43 "expect_tool": "add", "must_contain": "58"},
44 {"question": "What is 250 + 175?",
45 "expect_tool": "add", "must_contain": "425"},
46 {"question": "What is the capital of France?",
47 "expect_tool": None, "must_contain": "Paris"},
48]
49
50# --- Run every test, check it, and keep score ---
51passed = 0
52for test in tests:
53 tool_used, answer = ask_agent(test["question"])
54 print(f"\nQ: {test['question']}\nA: {answer}")
55 tool_ok = tool_used == test["expect_tool"] # check 1
56 answer_ok = test["must_contain"] in answer # check 2
57 if tool_ok and answer_ok:
58 print("โ
PASS")
59 passed += 1
60 if not tool_ok:
61 print("โ FAIL: expected tool "
62 f"{test['expect_tool']}, "
63 f"but it used {tool_used}")
64 if not answer_ok:
65 print("โ FAIL: the answer should contain "
66 f"'{test['must_contain']}'")
67
68print(f"\nScore: {passed}/{len(tests)} passed")No output yet...