Skip to content

Commit

Permalink
Retry mechanism in MotleyTool (#88)
Browse files Browse the repository at this point in the history
* Tool retry mechanism

* Move REPL tool
  • Loading branch information
whimo authored Sep 30, 2024
1 parent 068e396 commit 010a05e
Show file tree
Hide file tree
Showing 26 changed files with 515 additions and 459 deletions.
4 changes: 2 additions & 2 deletions examples/Advanced output handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"from motleycrew.tasks import SimpleTask\n",
"from motleycrew.common.exceptions import InvalidOutput\n",
"\n",
"from motleycrew.tools import PythonREPLTool\n",
"from motleycrew.tools import LLMTool\n",
"from motleycrew.tools.code import PythonREPLTool\n",
"from motleycrew.tools.llm_tool import LLMTool\n",
"from motleycrew.tools import MotleyTool"
]
},
Expand Down
321 changes: 149 additions & 172 deletions examples/Blog with Images.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/Math via python code with a single agent.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"\n",
"from motleycrew import MotleyCrew\n",
"from motleycrew.agents.crewai import CrewAIMotleyAgent\n",
"from motleycrew.tools import PythonREPLTool\n",
"from motleycrew.tools.code import PythonREPLTool\n",
"from motleycrew.common import configure_logging\n",
"from motleycrew.tasks import SimpleTask\n",
"\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/Using AutoGen with motleycrew.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
"metadata": {},
"outputs": [],
"source": [
"from motleycrew.tools import AutoGenChatTool\n",
"from motleycrew.tools.autogen_chat_tool import AutoGenChatTool\n",
"\n",
"knowledge_retrieval_tool = AutoGenChatTool(\n",
" name=\"retrieve_knowledge_by_topic\",\n",
Expand Down
Binary file added examples/images/0b96405f.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/images/651cf780.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed examples/images/92ead4f1.png
Binary file not shown.
Binary file removed examples/images/a7700351.png
Binary file not shown.
Binary file added examples/images/cc33d04d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed examples/images/ee0e80ec.png
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/old/math_crewai.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from motleycrew.agents.crewai import CrewAIMotleyAgent
from motleycrew.common import configure_logging
from motleycrew.tasks import SimpleTask
from motleycrew.tools import PythonREPLTool
from motleycrew.tools.code import PythonREPLTool


def main():
Expand Down
3 changes: 2 additions & 1 deletion motleycrew/applications/research_agent/question_answerer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from motleycrew.applications.research_agent.question import Question
from motleycrew.common.utils import print_passthrough
from motleycrew.storage import MotleyGraphStore
from motleycrew.tools import LLMTool, MotleyTool
from motleycrew.tools import MotleyTool
from motleycrew.tools.llm_tool import LLMTool

_default_prompt = PromptTemplate.from_template(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from motleycrew.applications.research_agent.question import Question
from motleycrew.common.utils import print_passthrough
from motleycrew.tools import LLMTool, MotleyTool
from motleycrew.tools import MotleyTool
from motleycrew.tools.llm_tool import LLMTool


class QuestionPrioritizerTool(MotleyTool):
Expand Down
4 changes: 2 additions & 2 deletions motleycrew/common/defaults.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from motleycrew.common.enums import GraphStoreType
from motleycrew.common.enums import LLMProvider
from motleycrew.common.enums import GraphStoreType, LLMProvider


class Defaults:
Expand All @@ -20,6 +19,7 @@ class Defaults:
"aider": "pip install aider-chat",
"pglast": "pip install pglast",
"crewai_tools": "pip install 'crewai[tools]'",
"replicate": "pip install replicate",
}

DEFAULT_NUM_THREADS = 4
Expand Down
14 changes: 2 additions & 12 deletions motleycrew/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
"""MotleyTool class and tools library."""

from motleycrew.tools.tool import MotleyTool
from motleycrew.tools.tool import DirectOutput
from motleycrew.tools.tool import DirectOutput, MotleyTool, RetryConfig

from .autogen_chat_tool import AutoGenChatTool
from .code.postgresql_linter import PostgreSQLLinterTool
from .code.python_linter import PythonLinterTool
from .html_render_tool import HTMLRenderTool
from .image.dall_e import DallEImageGeneratorTool
from .llm_tool import LLMTool
from .mermaid_evaluator_tool import MermaidEvaluatorTool
from .python_repl import PythonREPLTool

__all__ = ["MotleyTool"]
__all__ = ["MotleyTool", "RetryConfig", "DirectOutput"]
6 changes: 6 additions & 0 deletions motleycrew/tools/code/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .aider_tool import AiderTool
from .postgresql_linter import PostgreSQLLinterTool
from .python_linter import PythonLinterTool
from .python_repl import PythonREPLTool

__all__ = ["PythonLinterTool", "PostgreSQLLinterTool", "AiderTool", "PythonREPLTool"]
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from langchain_experimental.utilities import PythonREPL
from pydantic import BaseModel, Field

from .tool import MotleyTool
from ..tool import MotleyTool


class PythonREPLTool(MotleyTool):
Expand Down
4 changes: 4 additions & 0 deletions motleycrew/tools/image/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .dall_e import DallEImageGeneratorTool
from .replicate_tool import ReplicateImageGeneratorTool

__all__ = ["DallEImageGeneratorTool", "ReplicateImageGeneratorTool"]
6 changes: 5 additions & 1 deletion motleycrew/tools/image/dall_e.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from typing import List, Optional

from langchain.agents import Tool
Expand Down Expand Up @@ -94,12 +95,15 @@ def run_dalle_and_save_images(

prompt_value = dall_e_prompt.invoke({"text": description})

dalle_api = DallEAPIWrapper(
dalle_api = DallEAPIWrapper.model_construct(
model=model,
quality=quality,
size=size,
model_kwargs={"style": style} if (model == "dall-e-3" and style) else {},
openai_api_key=os.getenv("OPENAI_API_KEY"),
# TODO: remove this after https://github.com/langchain-ai/langchain/issues/26941 is fixed
)
dalle_api.validate_environment()

dalle_result = dalle_api.run(prompt_value.text)
logger.info("Dall-E API output: %s", dalle_result)
Expand Down
10 changes: 9 additions & 1 deletion motleycrew/tools/image/replicate_tool.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
from typing import List, Optional

import replicate
from langchain.agents import Tool
from pydantic import BaseModel, Field

import motleycrew.common.utils as motley_utils
from motleycrew.common import logger
from motleycrew.common.utils import ensure_module_is_installed
from motleycrew.tools.image.download_image import download_url_to_directory
from motleycrew.tools.tool import MotleyTool

try:
import replicate
except ImportError:
replicate = None


model_map = {
"sdxl": "stability-ai/sdxl:39ed52f2a78e934b3ba6e2a89f5b1c712de7dfea535525255b1aa35c5565e08b",
"flux-pro": "black-forest-labs/flux-pro",
Expand Down Expand Up @@ -64,6 +70,8 @@ def __init__(
:param images_directory: the directory to save the images to
:param kwargs: model-specific parameters, from pages such as https://replicate.com/black-forest-labs/flux-dev/api/schema
"""
ensure_module_is_installed("replicate")

self.model_name = model_name
self.kwargs = kwargs
langchain_tool = create_replicate_image_generator_langchain_tool(
Expand Down
Loading

0 comments on commit 010a05e

Please sign in to comment.