-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Aider code generation tool #38
Changes from 4 commits
f4db3e7
f3b78fe
fa9cf42
1b658e7
8a760d4
fe6a8b7
911f045
1bcd94a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from pathlib import Path | ||
import os | ||
import sys | ||
|
||
from dotenv import load_dotenv | ||
|
||
from langchain_community.tools import ShellTool | ||
from motleycrew.agents.crewai import CrewAIMotleyAgent | ||
from motleycrew.common import configure_logging, AsyncBackend | ||
from motleycrew.tasks import SimpleTask | ||
from motleycache import enable_cache, disable_cache, logger | ||
from motleycrew.tools.aider_tool import AiderTool | ||
import logging | ||
|
||
logger.setLevel(logging.INFO) | ||
WORKING_DIR = Path(os.path.realpath(".")) | ||
|
||
try: | ||
from motleycrew import MotleyCrew | ||
except ImportError: | ||
# if we are running this from source | ||
motleycrew_location = os.path.realpath(WORKING_DIR / "..") | ||
sys.path.append(motleycrew_location) | ||
|
||
|
||
# run instruction | ||
# cd ../../ | ||
# git clone https://github.com/ShoggothAI/motleycrew-code-generation-example.git | ||
|
||
|
||
def main(): | ||
crew = MotleyCrew(async_backend=AsyncBackend.THREADING) | ||
|
||
git_dname = r"../../motleycrew-code-generation-example" # aider coder git dir name | ||
sys.path.insert(0, os.path.abspath(git_dname)) | ||
|
||
check_functions_file = os.path.join(git_dname, "math_functions.py") | ||
unit_tests_file = os.path.join(git_dname, "test_math_functions.py") | ||
fnames = [unit_tests_file, check_functions_file] | ||
|
||
aider_tool = AiderTool(fnames=fnames, git_dname=git_dname, auto_commits=False) | ||
shell_tool = ShellTool() | ||
|
||
researcher = CrewAIMotleyAgent( | ||
role="Software Engineer", | ||
goal="Writing unit tests", | ||
backstory="""You are a leading software engineer working in the field of computer technology""", | ||
delegation=False, | ||
verbose=True, | ||
tools=[aider_tool, shell_tool], | ||
) | ||
|
||
# Create tasks for your agent | ||
create_unit_tests_task = SimpleTask( | ||
crew=crew, | ||
name="Adding a unit test", | ||
description=f"""Generate unit tests for the module math_functions.py | ||
Using py test, you can also add checks for possible exceptions and comments to the tests.""", | ||
agent=researcher, | ||
) | ||
|
||
run_unit_tests_task = SimpleTask( | ||
crew=crew, | ||
name="Running unit tests", | ||
description=f"""Run unit tests from the {unit_tests_file} file, using pytest | ||
from the current directory and return the execution results""", | ||
agent=researcher | ||
) | ||
|
||
create_unit_tests_task >> run_unit_tests_task | ||
|
||
result = crew.run() | ||
|
||
# Get the outputs of the task | ||
print(run_unit_tests_task.output) | ||
return run_unit_tests_task.output | ||
|
||
|
||
if __name__ == "__main__": | ||
configure_logging(verbose=True) | ||
load_dotenv() | ||
main() |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,49 @@ | ||||||||||
from motleycrew.common.utils import ensure_module_is_installed | ||||||||||
|
||||||||||
try: | ||||||||||
from aider.coders import Coder | ||||||||||
from aider.models import Model | ||||||||||
except ImportError: | ||||||||||
ensure_module_is_installed("aider-chat", "poetry run pip install aider-chat") | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why poetry here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, let's put this inside AiderTool init in case we want to import this module in some init.py |
||||||||||
|
||||||||||
from langchain.tools import Tool | ||||||||||
from langchain_core.pydantic_v1 import BaseModel, Field | ||||||||||
|
||||||||||
from motleycrew.common import Defaults | ||||||||||
from motleycrew.tools import MotleyTool | ||||||||||
|
||||||||||
|
||||||||||
class AiderTool(MotleyTool): | ||||||||||
|
||||||||||
def __init__(self, model: str = None, **kwargs): | ||||||||||
|
||||||||||
model = model or Defaults.DEFAULT_LLM_NAME | ||||||||||
llm_model = Model(model=model) | ||||||||||
coder = Coder.create(main_model=llm_model, **kwargs) | ||||||||||
|
||||||||||
langchain_tool = create_aider_tool(coder) | ||||||||||
super(AiderTool, self).__init__(langchain_tool) | ||||||||||
|
||||||||||
|
||||||||||
class AiderToolInput(BaseModel): | ||||||||||
"""Input for the REPL tool. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
Attributes: | ||||||||||
with_message (str): | ||||||||||
""" | ||||||||||
|
||||||||||
with_message: str = Field(description="instructions for code generate") | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
|
||||||||||
def create_aider_tool(coder: Coder): | ||||||||||
""" Create langchain tool from aider coder | ||||||||||
|
||||||||||
Returns: | ||||||||||
Tool: | ||||||||||
""" | ||||||||||
return Tool.from_function( | ||||||||||
func=coder.run, | ||||||||||
name="aider tool", | ||||||||||
description="Tool for generate programming code", | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
args_schema=AiderToolInput, | ||||||||||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove async backend here