diff --git a/autocommit/commit.py b/autocommit/commit.py index 2906dca..e6a58a7 100644 --- a/autocommit/commit.py +++ b/autocommit/commit.py @@ -27,9 +27,8 @@ def format(self, **kwargs) -> str: prompt = CustomPromptTemplate( - input_variables=["diff"], - template=""" - What follows "-------" is a git diff for a potential commit. + input_variables=["diff", "repo_url"], template=""" + What follows "-------" is a git diff for a potential commit on the repository located at {repo_url}. Reply with a markdown unordered list of 5 possible, different Git commit messages (a Git commit message should be concise but also try to describe the important changes in the commit), order the list by what you think @@ -38,11 +37,10 @@ def format(self, **kwargs) -> str: ------- {diff} ------- -""", -) +""") -def generate_suggestions(diff, openai_api_key=OPENAI_KEY): +def generate_suggestions(diff, repo_url, openai_api_key=OPENAI_KEY): llm = OpenAI( temperature=0.2, @@ -52,7 +50,7 @@ def generate_suggestions(diff, openai_api_key=OPENAI_KEY): ) # type: ignore # query OpenAI - formattedPrompt = prompt.format(diff=diff) + formattedPrompt = prompt.format(diff=diff, repo_url=repo_url) response = llm(formattedPrompt) # Convert the markdown string to HTML @@ -85,6 +83,9 @@ def main(): openai_api_key = keyring.get_password(SERVICE_ID, "user") if openai_api_key is None: openai_api_key = prompt_for_openai_api_key() + + repo_url = subprocess.check_output(["git", "config", "--get", "remote.origin.url"]) + repo_url = repo_url.decode("utf-8").strip() # Get the diff including untracked files (see https://stackoverflow.com/a/52093887) git_command = "git --no-pager diff; for next in $( git ls-files --others --exclude-standard ) ; do git --no-pager diff --no-index /dev/null $next; done;" @@ -117,7 +118,7 @@ def main(): suggestions = [] try: - suggestions = generate_suggestions(diff[:7000], openai_api_key=openai_api_key) + suggestions = generate_suggestions(diff[:7000], repo_url, openai_api_key=openai_api_key) except Exception as e: print("There was an error generating suggestions from OpenAI: ") # Prompt for OpenAI API key if it's incorrect diff --git a/autocommit/llm.py b/autocommit/llm.py index 38794e3..da053ff 100644 --- a/autocommit/llm.py +++ b/autocommit/llm.py @@ -21,8 +21,8 @@ def format(self, **kwargs) -> str: prompt = CustomPromptTemplate( - input_variables=["diff"], template=""" - What follows "-------" is a git diff for a potential commit. + input_variables=["diff", "repo_url"], template=""" + What follows "-------" is a git diff for a potential commit on the repository located at {repo_url}. Reply with a markdown unordered list of 5 possible, different Git commit messages (a Git commit message should be concise but also try to describe the important changes in the commit), order the list by what you think @@ -34,13 +34,13 @@ def format(self, **kwargs) -> str: """) -def generate_suggestions(diff, openai_api_key=OPENAI_KEY): +def generate_suggestions(diff, repo_url, openai_api_key=OPENAI_KEY): llm = OpenAI(temperature=0.2, openai_api_key=openai_api_key, max_tokens=100, model_name="text-davinci-003") # type: ignore # query OpenAI - formattedPrompt = prompt.format(diff=diff) + formattedPrompt = prompt.format(diff=diff, repo_url=repo_url) response = llm(formattedPrompt) # Convert the markdown string to HTML diff --git a/scan_repo.py b/scan_repo.py index 3d1d6e1..77e2289 100644 --- a/scan_repo.py +++ b/scan_repo.py @@ -52,7 +52,7 @@ # text-davinci-003 supports 4000 tokens. Let's use upto 3500 tokens for the prompt. # 3500 tokens = 14,000 characters (https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them) # But in practice, > 7000 seems to exceed the limit - suggestions = generate_suggestions(commit.diff[:7000]) + suggestions = generate_suggestions(commit.diff[:7000], GIT_REPO_URL) # generate CSV row for item in suggestions: