-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Key changes: * Add the SummarizeCode tool. * Impose a limit of 20,000 characters on tool outputs. * Implement security improvements: authentication for all endpoints. * Fix some bugs. * Add tests (78.21% total coverage (+4.14%)) Other: * Allow for files in print_all_files_in_path.py. * Fix JSON configs: change 'role' to 'name'. * Update developer instructions. * Update .gitignore.
- Loading branch information
Showing
32 changed files
with
562 additions
and
149 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,3 +167,4 @@ settings.json | |
|
||
# UI folder | ||
nalgonda/ui/* | ||
nalgonda/data/agency_data/* |
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from pathlib import Path | ||
|
||
from agency_swarm import BaseTool | ||
from pydantic import Field | ||
|
||
from nalgonda.custom_tools import PrintAllFilesInPath | ||
from nalgonda.custom_tools.utils import get_chat_completion | ||
from nalgonda.settings import settings | ||
|
||
USER_PROMPT_PREFIX = "Summarize the code of each file below.\n\n" | ||
SYSTEM_MESSAGE = """\ | ||
Your main job is to handle programming code from SEVERAL FILES. \ | ||
Each file's content is shown within triple backticks and has a FILE PATH as a title. \ | ||
It's vital to KEEP the FILE PATHS. | ||
Here's what to do: | ||
1. ALWAYS KEEP the FILE PATHS for each file. | ||
2. Start each file with a short SUMMARY of its content. Mention important points but don't repeat details found later. | ||
3. KEEP important elements like non-trivial imports, function details, type hints, and key constants. \ | ||
Don't change these. | ||
4. In functions or class methods, replace long code with a short SUMMARY in the docstrings, keeping the main logic. | ||
5. Shorten and combine docstrings and comments into the function or method descriptions. | ||
6. For classes, provide a brief SUMMARY in the docstrings, explaining the class's purpose and main logic. | ||
7. Cut down long strings to keep things brief. | ||
8. If there's a comment about "truncated output" at the end, KEEP it. | ||
Your task is to create a concise version of the code, strictly keeping the FILE PATHS and structure, \ | ||
without extra comments or explanations. Focus on clarity and avoiding repeated information within each file.\ | ||
""" | ||
|
||
|
||
class SummarizeCode(BaseTool): | ||
"""Summarize code using GPT-3. The tool uses the `PrintAllFilesInPath` tool to get the code to summarize. | ||
The parameters are: start_path, file_extensions. | ||
Directory traversal is not allowed (you cannot read /* or ../*). | ||
""" | ||
|
||
start_path: Path = Field( | ||
default_factory=Path.cwd, | ||
description="The starting path to search for files, defaults to the current working directory. " | ||
"Can be a filename or a directory.", | ||
) | ||
file_extensions: set[str] = Field( | ||
default_factory=set, | ||
description="Set of file extensions to include in the tree. If empty, all files will be included. " | ||
"Examples are {'.py', '.txt', '.md'}.", | ||
) | ||
|
||
def run(self) -> str: | ||
full_code = PrintAllFilesInPath( | ||
start_path=self.start_path, | ||
file_extensions=self.file_extensions, | ||
).run() | ||
user_prompt = f"{USER_PROMPT_PREFIX}{full_code}" | ||
|
||
output = get_chat_completion( | ||
user_prompt=user_prompt, system_message=SYSTEM_MESSAGE, temperature=0.0, model=settings.gpt_cheap_model | ||
) | ||
|
||
if len(output) > 20000: | ||
output = output[:20000] + "\n\n... (truncated output, please use a smaller directory or apply a filter)" | ||
return output | ||
|
||
|
||
if __name__ == "__main__": | ||
print( | ||
SummarizeCode( | ||
start_path=".", | ||
file_extensions={".py"}, | ||
).run() | ||
) |
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
7 changes: 4 additions & 3 deletions
7
nalgonda/data/default_configs/agent/default_config_developer.json
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,11 +1,12 @@ | ||
{ | ||
"role": "Developer", | ||
"name": "Developer", | ||
"description": "Responsible for running and executing Python Programs. Can also save programs to files, and search the web for information.", | ||
"instructions": "# Instructions for AI Developer Agent\n\n- Write clean and efficient Python code.\n- Ensure correct imports according to the program structure.\n- Check your code to validate functionality and errors, before reporting back to the user.\n- Always update all relevant files after each change, don't bother the user with details or code diff.\n- Before starting to work, make sure you are familiar with the codebase. Use BuildDirectoryTree to get the directory structure. Then use PrintAllFilesInDirectory tool to print all files in a particular directory.\n- ALWAYS try to minimize the number of files printed. ALWAYS use BuildDirectoryTree tool before PrintAllFilesInDirectory to find the most relevant directory.", | ||
"instructions": "# Instructions for AI Developer Agent\n\n- Write clean and efficient Python code.\n- Ensure correct imports according to the program structure.\n- ALWAYS update all relevant files after each change; don't bother the user with details or code diffs.\n- Before starting to work, MAKE SURE you are familiar with the codebase. You MUST USE the BuildDirectoryTree tool to get the directory structure. Then you MUST use the SummarizeCode tool to get an overview of the code (prefer low-level directories or individual files). Finally, use the PrintAllFilesInPath tool to access the full code of the files you absolutely need (only when writing tests, when using as dependency, when debugging).\n- ALWAYS USE the BuildDirectoryTree tool BEFORE SummarizeCode or PrintAllFilesInPath to find the most relevant directory or file.\n- ALWAYS USE the SummarizeCode tool BEFORE PrintAllFilesInPath to gain a better overview of the code.\n- When writing tests, ALWAYS use EXISTING testing infrastructure as much as possible: mocks, utility functions / classes / fixtures / conftest objects.\n- Use the WriteAndSaveProgram tool when coding. It allows you to plan your work and save the code to files.", | ||
"files_folder": null, | ||
"tools": [ | ||
"BuildDirectoryTree", | ||
"PrintAllFilesInDirectory", | ||
"PrintAllFilesInPath", | ||
"SummarizeCode", | ||
"WriteAndSaveProgram" | ||
] | ||
} |
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
Oops, something went wrong.