Test that an agent is able to address another agent in a message.
(
test_settings: Settings, prefix: str, address: str, x: int, answer: int
)
| 40 | ) |
| 41 | @pytest.mark.parametrize("x,answer", [(5, 25)]) |
| 42 | def test_addressing( |
| 43 | test_settings: Settings, prefix: str, address: str, x: int, answer: int |
| 44 | ): |
| 45 | """Test that an agent is able to address another agent in a message.""" |
| 46 | set_global(test_settings) |
| 47 | |
| 48 | class BobAgent(ChatAgent): |
| 49 | def llm_response( |
| 50 | self, message: Optional[str | ChatDocument] = None |
| 51 | ) -> Optional[ChatDocument]: |
| 52 | if ( |
| 53 | isinstance(message, ChatDocument) |
| 54 | and message.metadata.sender_name == "Alice" |
| 55 | ): |
| 56 | return self.create_llm_response(DONE + " " + message.content) |
| 57 | |
| 58 | addr = AT if AT in address else SEND_TO |
| 59 | # throw in some distracting addresses, to test that |
| 60 | # only the last one is picked up |
| 61 | return self.create_llm_response( |
| 62 | f"Ok {addr}all here {addr}Junk is my question: {address} {x}" |
| 63 | ) |
| 64 | |
| 65 | class AliceAgent(ChatAgent): |
| 66 | def llm_response( |
| 67 | self, message: Optional[str | ChatDocument] = None |
| 68 | ) -> Optional[ChatDocument]: |
| 69 | # message.content will either be just an an int-string "5" |
| 70 | # (if prefix != "") or Bob's entire msg otherwise (and hence not an int) |
| 71 | try: |
| 72 | y = int(message.content.strip()) |
| 73 | except ValueError: |
| 74 | return None |
| 75 | answer = y * y |
| 76 | return self.create_llm_response(f"{DONE} {answer}") |
| 77 | |
| 78 | bob_config = ChatAgentConfig(name="Bob") |
| 79 | |
| 80 | bob = BobAgent(bob_config) |
| 81 | bob_task = Task( |
| 82 | bob, |
| 83 | interactive=False, |
| 84 | config=TaskConfig(addressing_prefix=prefix), |
| 85 | ) |
| 86 | |
| 87 | alice_config = ChatAgentConfig(name="Alice") |
| 88 | alice = AliceAgent(alice_config) |
| 89 | alice_task = Task(alice, interactive=False) |
| 90 | |
| 91 | bob_task.add_sub_task(alice_task) |
| 92 | |
| 93 | result = bob_task.run() |
| 94 | if prefix == "" and AT in address: |
| 95 | assert result is None |
| 96 | else: |
| 97 | assert answer == int(result.content.strip()) |
| 98 | |
| 99 |
nothing calls this directly
no test coverage detected
searching dependent graphs…