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

Updated the prompt to include the repository URL #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 9 additions & 8 deletions autocommit/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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;"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions autocommit/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion scan_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down