Skip to content
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

Integrate Code Formatting and Linting Tools to Improve Function Workflow #1079

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gpt_engineer/applications/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ def main(
if improve_mode:
fileselector = FileSelector(project_path)
original_files_dict = fileselector.ask_for_files()
original_files_dict = store.lint_files_dict(original_files_dict)
files_dict = handle_improve_mode(prompt, agent, memory, original_files_dict)
if not prompt_yesno("\nDo you want to apply these changes?"):
return
Expand Down
46 changes: 46 additions & 0 deletions gpt_engineer/core/default/file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from pathlib import Path
from typing import Union

import autopep8

from gpt_engineer.core.files_dict import FilesDict


Expand Down Expand Up @@ -54,3 +56,47 @@ def download(self) -> FilesDict:
content = "binary file"
files[str(path.relative_to(self.working_dir))] = content
return FilesDict(files)

def lint_files_dict(self, files_dict: FilesDict) -> FilesDict:
"""
Lints the given files dictionary. The dictionary keys are file names and the values are the file contents.
The function supports linting for Python and JavaScript files.

Parameters
----------
files_dict : dict
The dictionary containing file names as keys and file contents as values.

Returns
-------
dict
The dictionary containing linted file contents.
"""
lint_types = {
".py": lint_python_code,
# ".js": lint_javascript_code,
}
for file_name, file_content in files_dict.items():
for file_type, lint_func in lint_types.items():
if file_name.endswith(file_type):
files_dict[file_name] = lint_func(file_content)
print(f"Formatted {file_name} with {lint_func.__name__}")
return files_dict


# Static method to lint Python code
def lint_python_code(code: str) -> str:
"""
Lints the given Python code using autopep8.

Parameters
----------
code : str
The Python code to be linted.

Returns
-------
str
The linted Python code.
"""
return autopep8.fix_code(code)
1 change: 1 addition & 0 deletions gpt_engineer/core/default/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ def improve(
messages = [
SystemMessage(content=setup_sys_prompt_existing_code(preprompts)),
]

# Add files as input
messages.append(HumanMessage(content=f"{files_dict.to_chat()}"))
messages.append(HumanMessage(content=f"Request: {prompt}"))
Expand Down
54 changes: 52 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ tomlkit = "^0.12.4"
pyperclip = "^1.8.2"
langchain-anthropic = "^0.1.1"
regex = "^2023.12.25"
autopep8 = "1.6.0"
jsbeautifier = "1.14.0"

[tool.poetry.group.dev.dependencies]
pytest = ">=7.3.1"
Expand Down