Skip to content

Commit

Permalink
add agent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
User committed May 21, 2024
1 parent e9da22a commit 9ba31fc
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
Empty file added tests/test_agents/__init__.py
Empty file.
87 changes: 87 additions & 0 deletions tests/test_agents/test_agents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import pytest
from dotenv import load_dotenv

from langchain_community.tools import DuckDuckGoSearchRun
from motleycrew.agents.crewai.crewai_agent import CrewAIMotleyAgent
from motleycrew.agents.langchain.react import ReactMotleyAgent
from motleycrew.agents.llama_index.llama_index_react import ReActLlamaIndexMotleyAgent
from motleycrew.common.exceptions import AgentNotMaterialized, CannotModifyMaterializedAgent
from motleycrew.tools.python_repl import create_repl_tool
from motleycrew.tools.tool import MotleyTool


load_dotenv()

test_agents_names = ("crewai", "langchain", "llama_index")


class TestAgents:

@pytest.fixture(scope="class")
def crewai_agent(self):
agent = CrewAIMotleyAgent(
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI and data science",
backstory="""You work at a leading tech think tank.
Your expertise lies in identifying emerging trends.
You have a knack for dissecting complex data and presenting actionable insights.""",
verbose=True,
delegation=False,
tools=[DuckDuckGoSearchRun()],
)
return agent

@pytest.fixture(scope="class")
def langchain_agent(self):
agent = ReactMotleyAgent(
name="AI writer agent",
description="""Conduct a comprehensive analysis of the latest advancements in AI in 2024.
Identify key trends, breakthrough technologies, and potential industry impacts.
Your final answer MUST be a full analysis report""",
tools=[DuckDuckGoSearchRun()],
verbose=True,
)
return agent

@pytest.fixture(scope="class")
def llama_index_agent(self):
agent = ReActLlamaIndexMotleyAgent(
description="Uncover cutting-edge developments in AI and data science",
tools=[DuckDuckGoSearchRun()],
verbose=True,
)
return agent

@pytest.fixture(scope="class")
def agent(self, request, crewai_agent, langchain_agent, llama_index_agent):
agents = {
"crewai": crewai_agent,
"langchain": langchain_agent,
"llama_index": llama_index_agent,
}
return agents.get(request.param)

@pytest.mark.parametrize("agent", test_agents_names, indirect=True)
def test_add_tools(self, agent):
assert len(agent.tools) == 1
tools = [DuckDuckGoSearchRun()]
agent.add_tools(tools)
assert len(agent.tools) == 1

@pytest.mark.parametrize("agent", test_agents_names, indirect=True)
def test_materialized(self, agent):
with pytest.raises(AgentNotMaterialized):
agent.agent

assert not agent.is_materialized
agent.materialize()
assert agent.is_materialized

with pytest.raises(CannotModifyMaterializedAgent):
tool = create_repl_tool()
agent.add_tools([tool])

@pytest.mark.parametrize("agent", test_agents_names, indirect=True)
def test_as_tool(self, agent):
tool = agent.as_tool()
assert isinstance(tool, MotleyTool)
25 changes: 25 additions & 0 deletions tests/test_agents/test_llms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest

from langchain_openai import ChatOpenAI
from llama_index.llms.openai import OpenAI

from motleycrew.common.llms import init_llm
from motleycrew.common import LLMFamily, LLMFramework
from motleycrew.common.exceptions import LLMFamilyNotSupported


@pytest.mark.parametrize(
"llm_family, llm_framework, expected_class",
[
(LLMFamily.OPENAI, LLMFramework.LANGCHAIN, ChatOpenAI),
(LLMFamily.OPENAI, LLMFramework.LLAMA_INDEX, OpenAI),
],
)
def test_init_llm(llm_family, llm_framework, expected_class):
llm = init_llm(llm_family=llm_family, llm_framework=llm_framework)
assert isinstance(llm, expected_class)


def test_raise_init_llm():
with pytest.raises(LLMFamilyNotSupported):
llm = init_llm(llm_family=LLMFamily.OPENAI, llm_framework="unknown_framework")

0 comments on commit 9ba31fc

Please sign in to comment.