Elffuss BraidHub

kikocis/Based-Agent public

a87f47c62cfe · 2.5 KiBRawPermalink
1from swarm import Swarm
2from agents import triage_agent, sales_agent, refunds_agent
3from evals_util import evaluate_with_llm_bool, BoolEvalResult
4import pytest
5import json
6
7client = Swarm()
8
9CONVERSATIONAL_EVAL_SYSTEM_PROMPT = """
10You will be provided with a conversation between a user and an agent, as well as a main goal for the conversation.
11Your goal is to evaluate, based on the conversation, if the agent achieves the main goal or not.
12
13To assess whether the agent manages to achieve the main goal, consider the instructions present in the main goal, as well as the way the user responds:
14is the answer satisfactory for the user or not, could the agent have done better considering the main goal?
15It is possible that the user is not satisfied with the answer, but the agent still achieves the main goal because it is following the instructions provided as part of the main goal.
16"""
17
18
19def conversation_was_successful(messages) -> bool:
20 conversation = f"CONVERSATION: {json.dumps(messages)}"
21 result: BoolEvalResult = evaluate_with_llm_bool(
22 CONVERSATIONAL_EVAL_SYSTEM_PROMPT, conversation
23 )
24 return result.value
25
26
27def run_and_get_tool_calls(agent, query):
28 message = {"role": "user", "content": query}
29 response = client.run(
30 agent=agent,
31 messages=[message],
32 execute_tools=False,
33 )
34 return response.messages[-1].get("tool_calls")
35
36
37@pytest.mark.parametrize(
38 "query,function_name",
39 [
40 ("I want to make a refund!", "transfer_to_refunds"),
41 ("I want to talk to sales.", "transfer_to_sales"),
42 ],
43)
44def test_triage_agent_calls_correct_function(query, function_name):
45 tool_calls = run_and_get_tool_calls(triage_agent, query)
46
47 assert len(tool_calls) == 1
48 assert tool_calls[0]["function"]["name"] == function_name
49
50
51@pytest.mark.parametrize(
52 "messages",
53 [
54 [
55 {"role": "user", "content": "Who is the lead singer of U2"},
56 {"role": "assistant", "content": "Bono is the lead singer of U2."},
57 ],
58 [
59 {"role": "user", "content": "Hello!"},
60 {"role": "assistant", "content": "Hi there! How can I assist you today?"},
61 {"role": "user", "content": "I want to make a refund."},
62 {"role": "tool", "tool_name": "transfer_to_refunds"},
63 {"role": "user", "content": "Thank you!"},
64 {"role": "assistant", "content": "You're welcome! Have a great day!"},
65 ],
66 ],
67)
68def test_conversation_is_successful(messages):
69 result = conversation_was_successful(messages)
70 assert result == True