Skip to content

Commit

Permalink
add MockTool for agent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
User committed Jul 5, 2024
1 parent 98504d6 commit 408dd8c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
21 changes: 21 additions & 0 deletions tests/test_agents/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import Type
from langchain_core.tools import BaseTool
from langchain_core.pydantic_v1 import BaseModel, Field


class MockToolInput(BaseModel):
"""Input for the MockTool tool."""

tool_input: str = Field(description="tool_input")


class MockTool(BaseTool):
"""Mock tool for run agent tests"""

name: str = "mock tool"
description: str = "Mock tool for tests"

args_schema: Type[BaseModel] = MockToolInput

def _run(self, tool_input: str, *args, **kwargs):
return tool_input
4 changes: 2 additions & 2 deletions tests/test_agents/test_langchain_output_handler.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytest
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_core.agents import AgentFinish, AgentAction

from motleycrew.agents import MotleyOutputHandler
from motleycrew.agents.langchain.tool_calling_react import ReActToolCallingAgent
from motleycrew.agents.parent import DirectOutput
from motleycrew.common.exceptions import InvalidOutput, OutputHandlerMaxIterationsExceeded
from tests.test_agents import MockTool

invalid_output = "Add more information about AI applications in medicine."

Expand Down Expand Up @@ -38,7 +38,7 @@ def fake_agent_take_next_step(
@pytest.fixture
def agent():
agent = ReActToolCallingAgent(
tools=[DuckDuckGoSearchRun()],
tools=[MockTool()],
verbose=True,
chat_history=True,
output_handler=ReportOutputHandler(max_iterations=5),
Expand Down
27 changes: 11 additions & 16 deletions tests/test_agents/test_llama_index_output_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from collections import deque
import pytest

from langchain_community.tools import DuckDuckGoSearchRun
from langchain_core.tools import StructuredTool

try:
Expand All @@ -20,9 +19,9 @@
from motleycrew.agents import MotleyOutputHandler
from motleycrew.common.exceptions import (
InvalidOutput,
ModuleNotInstalled,
OutputHandlerMaxIterationsExceeded,
)
from tests.test_agents import MockTool


invalid_output = "Add more information about AI applications in medicine."
Expand All @@ -49,20 +48,16 @@ def fake_run_step(*args, **kwargs):

@pytest.fixture
def agent():
try:
search_tool = DuckDuckGoSearchRun()
agent = ReActLlamaIndexMotleyAgent(
description="Your goal is to uncover cutting-edge developments in AI and data science",
tools=[search_tool],
output_handler=ReportOutputHandler(max_iterations=5),
verbose=True,
)
agent.materialize()
agent._agent._run_step = fake_run_step
agent._agent._run_step = agent.run_step_decorator()(agent._agent._run_step)

except ModuleNotInstalled:
return

agent = ReActLlamaIndexMotleyAgent(
description="Your goal is to uncover cutting-edge developments in AI and data science",
tools=[MockTool()],
output_handler=ReportOutputHandler(max_iterations=5),
verbose=True,
)
agent.materialize()
agent._agent._run_step = fake_run_step
agent._agent._run_step = agent.run_step_decorator()(agent._agent._run_step)

return agent

Expand Down

0 comments on commit 408dd8c

Please sign in to comment.