Skip to content

Commit

Permalink
Merge branch 'main' into add_readthedocs
Browse files Browse the repository at this point in the history
  • Loading branch information
whimo authored May 6, 2024
2 parents 770aeac + ae3846d commit 854854a
Show file tree
Hide file tree
Showing 33 changed files with 296 additions and 270 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ jobs:
run: poetry run pytest

- name: Run integration tests
env:
OPENAI_API_KEY: fake_key_1337
run: poetry run python tests/run_integration_tests.py
123 changes: 67 additions & 56 deletions examples/delegation_crewai.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,81 @@

from motleycrew import MotleyCrew, Task
from motleycrew.agent.crewai import CrewAIMotleyAgent
from motleycrew.common.utils import configure_logging

load_dotenv()

search_tool = DuckDuckGoSearchRun()
def main():
search_tool = DuckDuckGoSearchRun()

researcher = 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=[search_tool],
)
researcher = 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=[search_tool],
)

writer = CrewAIMotleyAgent(
role="Tech Content Strategist",
goal="Craft compelling content on tech advancements",
backstory="""You are a renowned Content Strategist, known for your insightful and engaging articles.
You transform complex concepts into compelling narratives.""",
verbose=True,
delegation=True,
)
writer = CrewAIMotleyAgent(
role="Tech Content Strategist",
goal="Craft compelling content on tech advancements",
backstory="""You are a renowned Content Strategist, known for your insightful and engaging articles.
You transform complex concepts into compelling narratives.""",
verbose=True,
delegation=True,
)

# Create tasks for your agents
crew = MotleyCrew()
task1 = Task(
crew=crew,
name="produce comprehensive analysis report on AI advancements",
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""",
agent=researcher,
documents=["paper1.pdf", "paper2.pdf"], # will be ignored for now
)
# Create tasks for your agents
crew = MotleyCrew()
task1 = Task(
crew=crew,
name="produce comprehensive analysis report on AI advancements",
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""",
agent=researcher,
documents=["paper1.pdf", "paper2.pdf"], # will be ignored for now
)

task2 = Task(
crew=crew,
name="provide a literature summary of recent papers on AI",
description="""Conduct a comprehensive literature review of the latest advancements in AI in 2024.
Identify key papers, researchers, and companies in the space.
Your final answer MUST be a full literature review with citations""",
agent=researcher,
)
task2 = Task(
crew=crew,
name="provide a literature summary of recent papers on AI",
description="""Conduct a comprehensive literature review of the latest advancements in AI in 2024.
Identify key papers, researchers, and companies in the space.
Your final answer MUST be a full literature review with citations""",
agent=researcher,
)

task3 = Task(
crew=crew,
name="produce blog post on AI advancements",
description="""Using the insights provided by a thorough web search, develop an engaging blog
post that highlights the most significant AI advancements.
Your post should be informative yet accessible, catering to a tech-savvy audience.
Make it sound cool, avoid complex words so it doesn't sound like AI.
Create a blog post of at least 4 paragraphs.""",
agent=writer,
)

task3 = Task(
crew=crew,
name="produce blog post on AI advancements",
description="""Using the insights provided by a thorough web search, develop an engaging blog
post that highlights the most significant AI advancements.
Your post should be informative yet accessible, catering to a tech-savvy audience.
Make it sound cool, avoid complex words so it doesn't sound like AI.
Create a blog post of at least 4 paragraphs.""",
agent=writer,
)
[task1, task2] >> task3

[task1, task2] >> task3
# Get your crew to work!
result = crew.run(
agents=[researcher, writer],
verbose=2, # You can set it to 1 or 2 to different logging levels
)

# Get your crew to work!
result = crew.run(
agents=[researcher, writer],
verbose=2, # You can set it to 1 or 2 to different logging levels
)
# Get the outputs of the task
for output in task3.outputs:
print(output)

print(list(result._done)[0].outputs)
return task3.outputs


if __name__ == "__main__":
configure_logging(verbose=True)

load_dotenv()
main()
14 changes: 4 additions & 10 deletions examples/single_llama_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from motleycrew import MotleyCrew, Task
from motleycrew.agent.llama_index import ReActLlamaIndexMotleyAgent
from motleycrew.caching import enable_cache, disable_cache
from motleycrew.common.utils import configure_logging


Expand All @@ -19,7 +18,6 @@ def main():
verbose=True,
)


crew = MotleyCrew()

# Create tasks for your agents
Expand All @@ -33,24 +31,20 @@ def main():
documents=["paper1.pdf", "paper2.pdf"],
)

# Instantiate your crew with a sequential process
# Get your crew to work!
result = crew.run(
agents=[researcher],
verbose=2, # You can set it to 1 or 2 to different logging levels
)

# Get your crew to work!
outputs = list(result._done)[0].outputs
outputs = task1.outputs
print(outputs)
print("######################")

return outputs[0].response
return [output.response for output in outputs]


if __name__ == '__main__':
if __name__ == "__main__":
configure_logging(verbose=True)

load_dotenv()
enable_cache()
main()
disable_cache()
68 changes: 34 additions & 34 deletions examples/single_openai_tools_react.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,48 @@
from motleycrew import MotleyCrew, Task
from motleycrew.agent.langchain.openai_tools_react import ReactOpenAIToolsAgent
from motleycrew.agent.langchain.react import ReactMotleyAgent
from motleycrew.common.utils import configure_logging

# # You can delete this block if you don't want to use Langsmith
# from langsmith import Client
#
# unique_id = uuid4().hex[0:8]
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_PROJECT"] = f"Tracing Walkthrough - {unique_id}"
# os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com"
# os.environ["LANGCHAIN_API_KEY"] = credentials["LANGCHAIN_API_KEY"]
#
# client = Client()
# # End of Langsmith block

load_dotenv()
def main():
search_tool = DuckDuckGoSearchRun()

search_tool = DuckDuckGoSearchRun()
tools = [search_tool]

researcher = ReactOpenAIToolsAgent(tools=tools, verbose=True)
researcher2 = ReactMotleyAgent(tools=tools, verbose=True)

tools = [search_tool]
outputs = []

for r in [researcher, researcher2]:
crew = MotleyCrew()
task = Task(
crew=crew,
name="produce comprehensive analysis report on AI advancements",
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""",
agent=r,
documents=["paper1.pdf", "paper2.pdf"], # will be ignored for now
)
result = crew.run(
agents=[r],
verbose=2, # You can set it to 1 or 2 to different logging levels
)

researcher = ReactOpenAIToolsAgent(tools=tools, verbose=True)
researcher2 = ReactMotleyAgent(tools=tools, verbose=True)
# Get your crew to work!
print(task.outputs)
outputs += task.outputs

for r in [researcher, researcher2]:
return outputs

crew = MotleyCrew()
task1 = Task(
crew=crew,
name="produce comprehensive analysis report on AI advancements",
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""",
agent=r,
documents=["paper1.pdf", "paper2.pdf"], # will be ignored for now
)
result = crew.run(
agents=[r],
verbose=2, # You can set it to 1 or 2 to different logging levels
)

# Get your crew to work!
print(list(result._done)[0].outputs)
if __name__ == "__main__":
from motleycrew.caching import enable_cache, set_cache_location, set_strong_cache

print("######################")
configure_logging(verbose=True)

load_dotenv()
enable_cache()
set_strong_cache(False)
main()
10 changes: 5 additions & 5 deletions motleycrew/caching/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@


def set_strong_cache(val: bool):
"""Enabling disabling the strictly caching option"""
"""Enable or disable the strict-caching option"""
BaseHttpCache.strong_cache = bool(val)


def set_update_cache_if_exists(val: bool):
"""Enabling disabling cache updates"""
"""Enable or disable cache updates"""
BaseHttpCache.update_cache_if_exists = bool(val)


def set_cache_location(location: str) -> str:
"""Sets the caching root directory, returns the absolute path of the derrictory"""
"""Set the caching root directory, return the absolute path of the directory"""
BaseHttpCache.root_cache_dir = location
return os.path.abspath(BaseHttpCache.root_cache_dir)


def enable_cache():
"""The function of enable the caching process"""
"""Enable global caching"""
global is_caching
for http_cache in caching_http_library_list:
http_cache.enable()
is_caching = True


def disable_cache():
"""The function of disable the caching process"""
"""Disable global caching"""
global is_caching
for http_cache in caching_http_library_list:
http_cache.disable()
Expand Down
23 changes: 6 additions & 17 deletions motleycrew/caching/http_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ class StrongCacheException(BaseException):
"""Exception use of cache only"""


load_dotenv()


def file_cache(http_cache: "BaseHttpCache", updating_parameters: dict = {}):
"""Decorator to cache function output based on its inputs, ignoring specified parameters."""

Expand Down Expand Up @@ -90,18 +87,16 @@ def enable(self):
"""Enable caching"""
self._enable()
self.is_caching = True
library_log = (
"for {} library.".format(self.library_name) if self.library_name else "."
)

library_log = "for {} library.".format(self.library_name) if self.library_name else "."
logging.info("Enable caching {} class {}".format(self.__class__, library_log))

def disable(self):
"""Disable caching"""
self._disable()
self.is_caching = False
library_log = (
"for {} library.".format(self.library_name) if self.library_name else "."
)

library_log = "for {} library.".format(self.library_name) if self.library_name else "."
logging.info("Disable caching {} class {}".format(self.__class__, library_log))

def prepare_response(self, response: Any) -> Any:
Expand Down Expand Up @@ -130,11 +125,7 @@ def get_cache_file(self, func: Callable, *args, **kwargs) -> Union[tuple, None]:

# check or create cache dirs
root_dir = Path(self.root_cache_dir)
cache_dir = (
root_dir
/ url_parsed.hostname
/ url_parsed.path.strip("/").replace("/", "_")
)
cache_dir = root_dir / url_parsed.hostname / url_parsed.path.strip("/").replace("/", "_")
cache_dir.mkdir(parents=True, exist_ok=True)

# Convert args to a dictionary based on the function's signature
Expand Down Expand Up @@ -214,9 +205,7 @@ def read_from_cache(self, cache_file: Path, url: str = "") -> Union[Any, None]:
except Exception as e:
logging.warning("Unpickling failed for {}".format(cache_file))
if self.strong_cache:
msg = "Error reading cached file: {}\n{}".format(
str(e), str(cache_file)
)
msg = "Error reading cached file: {}\n{}".format(str(e), str(cache_file))
raise StrongCacheException(msg)
return None

Expand Down
10 changes: 10 additions & 0 deletions motleycrew/common/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ def __str__(self) -> str:
return "Cannot modify agent{} as it is already materialized".format(
f" `{self.agent_name}`" if self.agent_name is not None else ""
)


class IntegrationTestException(Exception):
"""Integration tests exception"""

def __init__(self, test_names: list[str]):
self.test_names = test_names

def __str__(self):
return "Some integration tests failed: {}".format(self.test_names)
Loading

0 comments on commit 854854a

Please sign in to comment.