-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add PgSqlLinterTool * add pglast in poetry * add PgSqlLinterTool in motleycrew.tools * add PythonLinterTool * Minor refactoring of linter tools * fixes * cancel older concurrent workflows --------- Co-authored-by: User <[email protected]> Co-authored-by: whimo <[email protected]>
- Loading branch information
1 parent
acfbdd3
commit 20abb65
Showing
16 changed files
with
391 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from .tool import MotleyTool | ||
|
||
from .autogen_chat_tool import AutoGenChatTool | ||
from motleycrew.tools.image.dall_e import DallEImageGeneratorTool | ||
from .image.dall_e import DallEImageGeneratorTool | ||
from .llm_tool import LLMTool | ||
from .mermaid_evaluator_tool import MermaidEvaluatorTool | ||
from .python_repl import PythonREPLTool | ||
from .code.postgresql_linter import PostgreSQLLinterTool | ||
from .code.python_linter import PythonLinterTool |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from langchain_core.tools import Tool | ||
from langchain_core.pydantic_v1 import BaseModel, Field | ||
|
||
try: | ||
from pglast import parse_sql, prettify | ||
from pglast.parser import ParseError | ||
except ImportError: | ||
parse_sql = None | ||
prettify = None | ||
ParseError = None | ||
|
||
from motleycrew.tools import MotleyTool | ||
from motleycrew.common.utils import ensure_module_is_installed | ||
|
||
|
||
class PostgreSQLLinterTool(MotleyTool): | ||
|
||
def __init__(self): | ||
"""PostgreSQL code verification tool | ||
""" | ||
ensure_module_is_installed("pglast") | ||
|
||
langchain_tool = create_pgsql_linter_tool() | ||
super().__init__(langchain_tool) | ||
|
||
|
||
class PostgreSQLLinterInput(BaseModel): | ||
"""Input for the PostgreSQLLinterTool. | ||
Attributes: | ||
query (str): | ||
""" | ||
|
||
query: str = Field(description="SQL code for validation") | ||
|
||
|
||
def create_pgsql_linter_tool() -> Tool: | ||
"""Create the underlying langchain tool for PostgreSQLLinterTool | ||
Returns: | ||
Tool: | ||
""" | ||
def parse_func(query: str) -> str: | ||
try: | ||
parse_sql(query) | ||
return prettify(query) | ||
except ParseError as e: | ||
return str(e) | ||
|
||
return Tool.from_function( | ||
func=parse_func, | ||
name="PostgreSQL linter tool", | ||
description="Tool for validating PostgreSQL code", | ||
args_schema=PostgreSQLLinterInput, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import os | ||
from typing import Union | ||
|
||
from langchain_core.tools import StructuredTool | ||
from langchain_core.pydantic_v1 import BaseModel, Field | ||
|
||
from motleycrew.common.utils import ensure_module_is_installed | ||
from motleycrew.tools import MotleyTool | ||
|
||
Linter = None | ||
|
||
|
||
class PythonLinterTool(MotleyTool): | ||
|
||
def __init__(self): | ||
"""Python code verification tool | ||
""" | ||
ensure_module_is_installed("aider") | ||
|
||
langchain_tool = create_python_linter_tool() | ||
super().__init__(langchain_tool) | ||
|
||
|
||
class PythonLinterInput(BaseModel): | ||
"""Input for the PythonLinterTool. | ||
Attributes: | ||
code (str): | ||
file_name (str): | ||
""" | ||
|
||
code: str = Field(description="Python code for linting") | ||
file_name: str = Field(description="file name for the code", default="code.py") | ||
|
||
|
||
def create_python_linter_tool() -> StructuredTool: | ||
"""Create the underlying langchain tool for PythonLinterTool | ||
Returns: | ||
Tool: | ||
""" | ||
|
||
def lint(code: str, file_name: str = None) -> Union[str, None]: | ||
# create temp python file | ||
temp_file_name = file_name or "code.py" | ||
_, file_ext = os.path.splitext(temp_file_name) | ||
if file_ext != ".py": | ||
raise ValueError("The file extension must be .py") | ||
|
||
with open(temp_file_name, 'w') as f: | ||
f.write(code) | ||
|
||
# lint code | ||
try: | ||
linter = Linter() | ||
return linter.lint(temp_file_name) | ||
except Exception as e: | ||
return str(e) | ||
finally: | ||
os.remove(temp_file_name) | ||
|
||
return StructuredTool.from_function( | ||
func=lint, | ||
name="python linter tool", | ||
description="Tool for validating Python code", | ||
args_schema=PythonLinterInput, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.