2023-05-17 15:19:09 +02:00
|
|
|
import pytest
|
2023-06-13 14:52:24 +02:00
|
|
|
from unittest.mock import MagicMock, Mock
|
2023-05-17 15:19:09 +02:00
|
|
|
|
|
|
|
from haystack.agents.conversational import ConversationalAgent
|
|
|
|
from haystack.agents.memory import ConversationSummaryMemory, ConversationMemory, NoMemory
|
2023-06-13 14:52:24 +02:00
|
|
|
from test.conftest import MockPromptNode
|
2023-05-17 15:19:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.unit
|
|
|
|
def test_init():
|
2023-06-13 14:52:24 +02:00
|
|
|
prompt_node = MockPromptNode()
|
|
|
|
agent = ConversationalAgent(prompt_node)
|
2023-05-23 15:22:58 +02:00
|
|
|
|
2023-05-17 15:19:09 +02:00
|
|
|
# Test normal case
|
|
|
|
assert isinstance(agent.memory, ConversationMemory)
|
|
|
|
assert callable(agent.prompt_parameters_resolver)
|
|
|
|
assert agent.max_steps == 2
|
|
|
|
assert agent.final_answer_pattern == r"^([\s\S]+)$"
|
|
|
|
|
|
|
|
# ConversationalAgent doesn't have tools
|
|
|
|
assert not agent.tm.tools
|
|
|
|
|
2023-05-23 15:22:58 +02:00
|
|
|
|
|
|
|
@pytest.mark.unit
|
|
|
|
def test_init_with_summary_memory():
|
2023-06-13 14:52:24 +02:00
|
|
|
# Test with summary memory
|
|
|
|
prompt_node = MockPromptNode()
|
|
|
|
agent = ConversationalAgent(prompt_node, memory=ConversationSummaryMemory(prompt_node))
|
|
|
|
assert isinstance(agent.memory, ConversationSummaryMemory)
|
2023-05-17 15:19:09 +02:00
|
|
|
|
2023-05-23 15:22:58 +02:00
|
|
|
|
|
|
|
@pytest.mark.unit
|
|
|
|
def test_init_with_no_memory():
|
2023-06-13 14:52:24 +02:00
|
|
|
prompt_node = MockPromptNode()
|
|
|
|
# Test with no memory
|
|
|
|
agent = ConversationalAgent(prompt_node, memory=NoMemory())
|
|
|
|
assert isinstance(agent.memory, NoMemory)
|
2023-05-17 15:19:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.unit
|
|
|
|
def test_run():
|
2023-06-13 14:52:24 +02:00
|
|
|
prompt_node = MockPromptNode()
|
|
|
|
agent = ConversationalAgent(prompt_node)
|
|
|
|
|
|
|
|
# Mock the Agent run method
|
|
|
|
result = agent.run("query")
|
|
|
|
|
|
|
|
# empty answer
|
|
|
|
assert result["answers"][0].answer == ""
|