diff --git a/pyproject.toml b/pyproject.toml index 183dc4a..7d4333c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,6 +13,7 @@ dependencies = [ "PyGithub==2.4.0", "ollama==0.3.3", "colorama==0.4.6", + "pyyaml==6.0.1", ] [project.urls] diff --git a/src/agents.py b/src/agents.py index b248fe3..6f1ea8e 100644 --- a/src/agents.py +++ b/src/agents.py @@ -1,18 +1,27 @@ import ollama +import yaml from model import post_request_generate -from prompts import CODE_REVIEW_PROMPT, FEEDBACK_IMPROVEMENT_PROMPT, EXAMPLES class Agent: - def __init__(self, model_name="llama3.1", role="code_reviewer", use_post_request=False): + def __init__(self, model_name="llama3.1", role="code_reviewer", use_post_request=False, prompt_style="default"): self.model_name = model_name self.role = role self.use_post_request = use_post_request + self.prompt_style = prompt_style + self.prompts = self._load_prompts() + print(f"Inited agent with model: {model_name}, role: {role}, use_post_request: {use_post_request}, prompt_style: {prompt_style}") + + def _load_prompts(self): + with open('src/prompts.yaml', 'r') as file: + return yaml.safe_load(file)['prompts'] def analyze(self, data, model_name=None): prompt = self._prepare_prompt(data) model = model_name or self.model_name + # print(f"Prompt:\n{prompt}\nENDOFPROMPT") + if self.use_post_request: response = post_request_generate(model, prompt) else: @@ -25,19 +34,19 @@ def _prepare_prompt(self, data): return self._prepare_code_review_prompt(data) elif self.role == "feedback_improver": prompt = self._prepare_feedback_improvement_prompt(data) - print(f"IMPROVERPROMPT:\n{prompt}\nENDOFIMPROVERPROMPT") + # print(f"IMPROVERPROMPT:\n{prompt}\nENDOFIMPROVERPROMPT") return prompt else: raise ValueError(f"Unknown role: {self.role}") def _prepare_code_review_prompt(self, pr_data): context = f"Additional Context:\n{pr_data['context']}" if pr_data.get('context') else "" - return CODE_REVIEW_PROMPT.format( + return self.prompts['instructions'][self.prompt_style]['review'].format( title=pr_data['title'], description=pr_data['description'], changes=pr_data['changes'], context=context, - examples=EXAMPLES + examples=self.prompts['examples'] ) def _prepare_feedback_improvement_prompt(self, review_data): @@ -50,11 +59,11 @@ def _prepare_feedback_improvement_prompt(self, review_data): {review} --- """ - return FEEDBACK_IMPROVEMENT_PROMPT.format( + return self.prompts['instructions'][self.prompt_style]['feedback'].format( title=review_data['pr_data']['title'], description=review_data['pr_data']['description'], changes=review_data['pr_data']['changes'], reviews=reviews, context=context, - examples=EXAMPLES - ) + examples=self.prompts['examples'] + ) \ No newline at end of file diff --git a/src/pearbot.py b/src/pearbot.py index faba669..d19663d 100644 --- a/src/pearbot.py +++ b/src/pearbot.py @@ -16,6 +16,7 @@ def main(): parser.add_argument("--server", action="store_true", help="Run as a server") parser.add_argument("--diff", type=str, nargs='?', const='-', help="Path to the diff file or '-' for stdin") parser.add_argument("--model", type=str, default="llama3.1", help="Model for the final review step") + parser.add_argument("--prompt-style", type=str, default="default", help="Prompt style (from prompts.yaml)") parser.add_argument("--initial-review-models", type=str, default="llama3.1,llama3.1,llama3.1", help="Comma-separated list of model names for the initial review (default: llama3.1,llama3.1,llama3.1)") args = parser.parse_args() @@ -25,8 +26,8 @@ def main(): print(f"Available models: {', '.join(get_available_models()) or 'NONE'}") - code_review_agent = Agent(role="code_reviewer", use_post_request=True) - feedback_improver_agent = Agent(role="feedback_improver", use_post_request=True) + code_review_agent = Agent(role="code_reviewer", use_post_request=True, prompt_style=args.prompt_style) + feedback_improver_agent = Agent(role="feedback_improver", use_post_request=True, prompt_style=args.prompt_style) if args.server: print("Running as a server...") diff --git a/src/prompts.py b/src/prompts.py deleted file mode 100644 index 3a173d1..0000000 --- a/src/prompts.py +++ /dev/null @@ -1,89 +0,0 @@ -CODE_REVIEW_PROMPT = """ - You are an experienced software engineer tasked with reviewing the following Pull Request. - - --- - Pull Request title: {title} - Pull Request description: -{description} - --- - - Analyze the following file changes: - -{changes} - - {context} - - Please provide specific comments only for changed lines (additions or deletions), where you see potential issues, improvements or suggestions for the developer. - Take into account the existing comments and avoid repeating already mentioned points. - Separate your comments by file and provide a clear and concise explanation for each comment. - Don't repeat or mention the instructions given to you, just perform them. - Reference the relevant diff lines and keep your suggestions short and concise as in these examples: - - {examples} - - DO NOT include or comment on the examples in your reply. Your response: - Let's work this out in a step by step way to be sure we provide only useful suggestions: -""" - -FEEDBACK_IMPROVEMENT_PROMPT = """ - As an expert software engineer, you are tasked with providing feedback for a Pull Request. - Consider the original Pull Request details and a number of preliminary reviews. - - Original Pull Request: - --- - Title: {title} - Description: -{description} - --- - - With the following file changes: - -{changes} - - {context} - - Preliminary Reviews: -{reviews} - - Based on these reviews, but also adding your expertise on top, provide a review that contains feedback for the important aspects of the code changes. - Include only points that could potentially fix errors or lead to improvements. - DO NOT comment on the quality of the preliminary reviews themselves and DO NOT quote the preliminary reviews, but address the points directly. - - Reference the relevant diff lines and keep your suggestions short and concise as in these examples: - - {examples} - - DO NOT include or comment on the examples in your reply. Only your feedback to the Pull Request code changes: -""" - -EXAMPLES = """ -Example #1: -Change: -```diff -+ fi - -+if [[ $2 =~ ^[0-9]+$ ]]; then -+ msgSize=$1 -``` -Suggestion: -The assignment of msgSize is incorrect; it should be assigned the value of $2 instead of $1: -```diff -- msgSize=$1 -+ msgSize=$2 -``` - -Example #2: -Change: -```diff -+ xterm -geometry 90x40+550+40 -hold -e @EX_BIN_DIR@/$PROCESSOR1 & -+ -+ PROCESSOR2="fairmq-ex-region-processor" -+ PROCESSOR2+=" --id processor1" -``` -Suggestion: -There seems to be a copy-paste error. The `PROCESSOR2` should have a unique identifier, but it is currently set to `processor1`, which is the same as `PROCESSOR1`. This should be corrected to ensure that each processor has a unique ID. -```diff -- msgSize=$1 -+ msgSize=$2 -``` -""" diff --git a/src/prompts.yaml b/src/prompts.yaml new file mode 100644 index 0000000..8e3bd1a --- /dev/null +++ b/src/prompts.yaml @@ -0,0 +1,129 @@ +prompts: + examples: | + Example #1: + Change: + ```diff + + fi + + +if [[ $2 =~ ^[0-9]+$ ]]; then + + msgSize=$1 + ``` + Suggestion: + The assignment of msgSize is incorrect; it should be assigned the value of $2 instead of $1: + ```diff + - msgSize=$1 + + msgSize=$2 + ``` + + Example #2: + Change: + ```diff + + xterm -geometry 90x40+550+40 -hold -e @EX_BIN_DIR@/$PROCESSOR1 & + + + + PROCESSOR2="fairmq-ex-region-processor" + + PROCESSOR2+=" --id processor1" + ``` + Suggestion: + There seems to be a copy-paste error. The `PROCESSOR2` should have a unique identifier, but it is currently set to `processor1`, which is the same as `PROCESSOR1`. This should be corrected to ensure that each processor has a unique ID. + ```diff + - msgSize=$1 + + msgSize=$2 + ``` + + instructions: + default: + review: | + You are an experienced software engineer tasked with reviewing the following Pull Request. + + --- + Pull Request title: {title} + Pull Request description: + {description} + --- + + Analyze the following file changes: + + --- + {changes} + --- + + {context} + + Please provide specific comments only for changed lines (additions or deletions), where you see potential issues, improvements or suggestions for the developer. + Take into account the existing comments and avoid repeating already mentioned points. + Separate your comments by file and provide a clear and concise explanation for each comment. + Don't repeat or mention the instructions given to you, just perform them. + Reference the relevant diff lines and keep your suggestions short and concise as in these examples: + + --- + {examples} + --- + + DO NOT include or comment on the examples in your reply. Your response: + Let's work this out in a step by step way to be sure we provide only useful suggestions: + + feedback: | + As an expert software engineer, you are tasked with providing feedback for a Pull Request. + Consider the original Pull Request details and a number of preliminary reviews. + + Original Pull Request: + --- + Title: {title} + Description: + {description} + --- + + With the following file changes: + + --- + {changes} + --- + + {context} + + Preliminary Reviews: + --- + {reviews} + --- + + Based on these reviews, but also adding your expertise on top, provide a review that contains feedback for the important aspects of the code changes. + Include only points that could potentially fix errors or lead to improvements. + DO NOT comment on the quality of the preliminary reviews themselves and DO NOT quote the preliminary reviews, but address the points directly. + + Reference the relevant diff lines and keep your suggestions short and concise as in these examples: + + --- + {examples} + --- + + DO NOT include or comment on the examples in your reply. Only your feedback to the Pull Request code changes: + + simple: + review: | + Review the following Pull Request: + + Title: {title} + Description: {description} + + Changes: + {changes} + + {context} + + Provide concise feedback on potential issues or improvements. Focus on changed lines only. + + feedback: | + Improve the feedback for this Pull Request: + + Title: {title} + Description: {description} + + Changes: + {changes} + + {context} + + Previous reviews: + {reviews} + + Provide a concise, improved review focusing on the most important aspects of the code changes. Consider preliminary reviews as reference, but do not quote or mention them in any way. \ No newline at end of file