Skip to content

Commit

Permalink
Merge pull request #18 from ShoggothAI/scary_refactor
Browse files Browse the repository at this point in the history
Refactor app logic for graph-based dispatch
  • Loading branch information
whimo committed May 14, 2024
2 parents e277fb4 + 3115b37 commit 3de8160
Show file tree
Hide file tree
Showing 85 changed files with 102,815 additions and 1,574 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ jobs:
- name: Run integration tests
env:
OPENAI_API_KEY: fake_key_1337
TIKTOKEN_CACHE_DIR: tests/itest_cache/tiktoken_cache
run: poetry run python tests/run_integration_tests.py
8 changes: 3 additions & 5 deletions examples/blog_post/blog_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from motleycrew.agent.langchain.react import ReactMotleyAgent

from motleycrew.tool.llm_tool import LLMTool
from motleycrew import MotleyCrew, Task
from motleycrew import MotleyCrew, TaskRecipe

from .blog_post_input import text

Expand Down Expand Up @@ -48,9 +48,7 @@

illustrator_prompt = ChatPromptTemplate.from_messages(
[
SystemMessage(
content="You are a professional illustrator with 10 years of experience."
),
SystemMessage(content="You are a professional illustrator with 10 years of experience."),
HumanMessage(
content="You are given the following draft story, delimited by triple back quotes: ```{second_draft}```"
),
Expand Down Expand Up @@ -113,7 +111,7 @@

# Create tasks for your agents
crew = MotleyCrew()
task1 = Task(
task1 = TaskRecipe(
crew=crew,
name="Write a blog post from the provided information",
description=f"""Write a blog post of at most {max_words} words and at least {min_words}
Expand Down
22 changes: 8 additions & 14 deletions examples/delegation_crewai.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dotenv import load_dotenv
from langchain_community.tools import DuckDuckGoSearchRun

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

Expand Down Expand Up @@ -31,27 +31,24 @@ def main():

# Create tasks for your agents
crew = MotleyCrew()
task1 = Task(
crew=crew,

analysis_report_task = crew.create_simple_task(
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,
literature_summary_task = crew.create_simple_task(
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,
blog_post_task = crew.create_simple_task(
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.
Expand All @@ -61,19 +58,16 @@ def main():
agent=writer,
)

[task1, task2] >> task3
[analysis_report_task, literature_summary_task] >> blog_post_task

# 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)

return task3.outputs
print(blog_post_task.output)
return blog_post_task.output


if __name__ == "__main__":
Expand Down
21 changes: 21 additions & 0 deletions examples/dummy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@



@dataclass
ToolDescription:
name: str
description: str
framework: str
toolkit: Optional[str] = None

motley.tool.get_descriptions(framework: Optional[str] = None) -> ToolDescription
motleycrew.tool.from_description(d: ToolDescription) -> MotleyTool
motleycrew.tool.init_embeddings(embed_model) -> ToolSearcher
class ToolSearcher:
def match(self, task: str) -> List[MotleyTool]:
pass

def register_tools(self, ...):
@classmethod
def default(cls) -> ToolSearcher:
pass
106 changes: 56 additions & 50 deletions examples/image_generation_crewai.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,62 @@
from dotenv import load_dotenv

from motleycrew import MotleyCrew, Task
from motleycrew import MotleyCrew, TaskRecipe
from motleycrew.agent.crewai import CrewAIMotleyAgent
from motleycrew.tool.image_generation import DallEImageGeneratorTool
from motleycrew.common.utils import configure_logging

load_dotenv()
configure_logging(verbose=True)

image_generator_tool = DallEImageGeneratorTool()
# For saving images locally use the line below
# image_generator_tool = DallEImageGeneratorTool(images_directory="images")

writer = CrewAIMotleyAgent(
role="Short stories writer",
goal="Write short stories for children",
backstory="You are an accomplished children's writer, known for your funny and interesting short stories.\n"
"Many parents around the world read your books to their children.",
verbose=True,
delegation=True,
)

illustrator = CrewAIMotleyAgent(
role="Illustrator",
goal="Create beautiful and insightful illustrations",
backstory="You are an expert in creating illustrations for all sorts of concepts and articles. "
"You do it by skillfully prompting a text-to-image model.\n"
"Your final answer MUST be the exact URL or filename of the illustration.",
verbose=True,
delegation=False,
tools=[image_generator_tool],
)

# Create tasks for your agents
crew = MotleyCrew()
write_task = Task(
crew=crew,
name="write a short story about a cat",
description="Creatively write a short story of about 4 paragraphs "
"about a house cat that was smarter than its owners. \n"
"Write it in a cool and simple language, "
"making it intriguing yet suitable for children's comprehension.\n"
"You must include a fun illustration.\n"
"Your final answer MUST be the full story with the illustration URL or filename attached.",
agent=writer,
)


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

print(write_task.outputs)

def main():
"""Main function of running the example."""
image_generator_tool = DallEImageGeneratorTool()
# For saving images locally use the line below
# image_generator_tool = DallEImageGeneratorTool(images_directory="images")

illustrator = CrewAIMotleyAgent(
role="Illustrator",
goal="Create beautiful and insightful illustrations",
backstory="You are an expert in creating illustrations for all sorts of concepts and articles. "
"You do it by skillfully prompting a text-to-image model.\n"
"Your final answer MUST be the exact URL or filename of the illustration.",
verbose=True,
delegation=False,
tools=[image_generator_tool],
)

writer = CrewAIMotleyAgent(
role="Short stories writer",
goal="Write short stories for children",
backstory="You are an accomplished children's writer, known for your funny and interesting short stories.\n"
"Many parents around the world read your books to their children.",
verbose=True,
delegation=True,
tools=[illustrator],
)

# Create tasks for your agents
crew = MotleyCrew()
write_task = crew.create_simple_task(
name="write a short story about a cat",
description="Creatively write a short story of about 4 paragraphs "
"about a house cat that was smarter than its owners. \n"
"Write it in a cool and simple language, "
"making it intriguing yet suitable for children's comprehension.\n"
"You must include a fun illustration.\n"
"Your final answer MUST be the full story with the illustration URL or filename attached.",
agent=writer,
)

# Get your crew to work!
crew.run(
verbose=2, # You can set it to 1 or 2 to different logging levels
)

print(write_task.output)
return write_task.output


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

load_dotenv()
main()
105 changes: 56 additions & 49 deletions examples/math_crewai.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,60 @@
from dotenv import load_dotenv

from motleycrew import MotleyCrew, Task
from motleycrew import MotleyCrew, TaskRecipe
from motleycrew.agent.crewai import CrewAIMotleyAgent
from motleycrew.tool.python_repl import create_repl_tool

load_dotenv()

repl_tool = create_repl_tool()

# Define your agents with roles and goals
solver1 = CrewAIMotleyAgent(
role="High School Math Teacher",
goal="Generate great solutions to math problems",
backstory="""You are a high school math teacher with a passion for problem-solving.
To solve a math problem, you first reason about it, step by step, then generate the code to solve it exactly,
using sympy, then use the REPL tool to evaluate that code, and then
use the output to generate a human-readable solution.""",
verbose=True,
delegation=False,
tools=[repl_tool],
)

solver = solver1

problems = [
"Problem: If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what are the values of $x$, $y$, and $x - y$ ?",
"Drei Personen erhalten zusammen Fr. 2450. Die erste Person erh\u00e4lt Fr. 70 mehr als die zweite, aber Fr. 60 weniger als die dritte.\nWie viel erh\u00e4lt jede Person? ",
"Find all numbers $a$ for which the graph of $y=x^2+a$ and the graph of $y=ax$ intersect. Express your answer in interval notation.",
]

# Create tasks for your agents
crew = MotleyCrew()
task1 = Task(
crew=crew,
name="solve math problem",
description=f"""Create a nice human-readable solution to the following problem:
{problems[1]}
IN THE SAME LANGUAGE AS THE PROBLEM""",
agent=solver,
)


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

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

print("######################")
from motleycrew.common.utils import configure_logging


def main():
"""Main function of running the example."""
repl_tool = create_repl_tool()

# Define your agents with roles and goals
solver1 = CrewAIMotleyAgent(
role="High School Math Teacher",
goal="Generate great solutions to math problems",
backstory="""You are a high school math teacher with a passion for problem-solving.
To solve a math problem, you first reason about it, step by step, then generate the code to solve it exactly,
using sympy, then use the REPL tool to evaluate that code, and then
use the output to generate a human-readable solution.""",
verbose=True,
delegation=False,
tools=[repl_tool],
)

solver = solver1

problems = [
"Problem: If $725x + 727y = 1500$ and $729x+ 731y = 1508$, "
"what are the values of $x$, $y$, and $x - y$ ?",
"Drei Personen erhalten zusammen Fr. 2450. Die erste Person erh\u00e4lt Fr. 70 mehr als "
"die zweite, aber Fr. 60 weniger als die dritte.\nWie viel erh\u00e4lt jede Person? ",
"Find all numbers $a$ for which the graph of $y=x^2+a$ and the graph of $y=ax$ intersect. "
"Express your answer in interval notation.",
]

# Create tasks for your agents
crew = MotleyCrew()
task = crew.create_simple_task(
name="solve math problem",
description=f"""Create a nice human-readable solution to the following problem:
{problems[1]}
IN THE SAME LANGUAGE AS THE PROBLEM""",
agent=solver,
)

# Get your crew to work!
crew.run(
verbose=2, # You can set it to 1 or 2 to different logging levels
)

print(task.output)
return task.output


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

load_dotenv()
main()
Loading

0 comments on commit 3de8160

Please sign in to comment.