From 3410fb8a98d1cad9e09d181f99969593314a7f01 Mon Sep 17 00:00:00 2001 From: Saurav Panda Date: Sun, 15 Sep 2024 17:44:09 -0700 Subject: [PATCH] feat: kaizen generate pr commit msg and description --- examples/code_review/main.py | 2 + kaizen/generator/pr_description.py | 12 ++++++ kaizen/llms/prompts/pr_desc_prompts.py | 55 ++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/examples/code_review/main.py b/examples/code_review/main.py index 97c2a7e7..08fb9c69 100644 --- a/examples/code_review/main.py +++ b/examples/code_review/main.py @@ -52,3 +52,5 @@ user="kaizen/example", ) print(desc_data) + +comit_message = pr_desc.generate_pr_commit_message(desc_data.desc) diff --git a/kaizen/generator/pr_description.py b/kaizen/generator/pr_description.py index 61f4f3aa..957ec461 100644 --- a/kaizen/generator/pr_description.py +++ b/kaizen/generator/pr_description.py @@ -9,6 +9,7 @@ PR_DESCRIPTION_PROMPT, MERGE_PR_DESCRIPTION_PROMPT, PR_DESCRIPTION_SYSTEM_PROMPT, + PR_COMMIT_MESSAGE_PROMPT, ) @@ -170,3 +171,14 @@ def _process_file_chunk( self.total_usage = self.provider.update_usage(self.total_usage, usage) return desc + + def generate_pr_commit_message( + self, + desc: str, + user: Optional[str] = None, + ) -> str: + prompt = PR_COMMIT_MESSAGE_PROMPT.format( + DESC=desc, + ) + resp, usage = self.provider.chat_completion_with_json(prompt, user=user) + return resp, usage diff --git a/kaizen/llms/prompts/pr_desc_prompts.py b/kaizen/llms/prompts/pr_desc_prompts.py index 46227854..6785a501 100644 --- a/kaizen/llms/prompts/pr_desc_prompts.py +++ b/kaizen/llms/prompts/pr_desc_prompts.py @@ -52,3 +52,58 @@ Here is list of PR descriptions in json format: {DESCS} """ + + +PR_COMMIT_MESSAGE_PROMPT = """ +Generate a concise and informative Git commit message based on the following description of code changes. +Follow the Conventional Commits format and these best practices: + +1. Start with a type prefix, followed by a colon and space (e.g., feat:, fix:, docs:) +2. Use lowercase for the entire subject line +3. Keep the subject line (first line) under 50 characters +4. Summarize the main change in the subject line +5. Leave a blank line between the subject and the body (if a body is needed) +6. Use the body to explain the what and why of the change, not the how +7. Wrap the body at 72 characters +8. Use bullet points in the body for multiple changes if necessary +9. Include relevant issue numbers (e.g., "fixes #123" or "relates to #456") + +Common type prefixes: +- feat: A new feature +- fix: A bug fix +- docs: Documentation only changes +- style: Changes that do not affect the meaning of the code (white-space, formatting, etc) +- refactor: A code change that neither fixes a bug nor adds a feature +- perf: A code change that improves performance +- test: Adding missing tests or correcting existing tests +- chore: Changes to the build process or auxiliary tools and libraries + +Examples: + +1. Simple change: +{{ + "subject": "fix: typo in README.md", + "body": null +}} + +2. Complex change: +{{ + "subject": "feat: user authentication feature", + "body": "Implement login and logout functionality for users.\\n\\n- Create login form and handle submission\\n- Set up session management\\n- Add logout button and route\\n- Update navbar to show login status\\n\\nRelates to #42" +}} + + +Based on this description, generate a commit message following the above guidelines and examples. +If the changes are simple, a subject line alone may suffice. For more complex changes, include a body with additional details. + +Provide the output in JSON format with the following structure: +``` +{{ + "subject": "prefix:the commit message subject line", + "body": "The commit message body (if necessary, otherwise null)" +}} +``` + +Code change description: +{DESC} +"""