AI-Powered Git Commit Message Generator

0 downloads

askrobots_commitgen/ai_commit.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/askrobots_commitgen.git
HTTPS
https://gitstable.com/git/askrobots/askrobots_commitgen.git
SSH
ssh://git@gitstable.com:2222/askrobots/askrobots_commitgen.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Create commit message adjusting OpenAI initialization and error handling. c0d2e25 2 years, 6 months ago History
ai_commit.py · 1.1 KB · 25 lines
1from settings import OPENAI_API_KEY, OPENAI_MODEL, MAX_COMMIT_MESSAGE_LENGTH
2from openai import OpenAI
3import os
4
5# Assuming your API key is set in the environment or you set it somewhere in your settings
6api_key = os.getenv('OPENAI_API_KEY')
7client = OpenAI(api_key=api_key)
8
9def generate_commit_message(diff: str) -> str:
10 try:
11 chat_completion = client.chat.completions.create(
12 messages=[
13 {"role": "system", "content": "You are a helpful assistant."},
14 {"role": "user", "content": f"Generate a commit message for the following diff:\n{diff}"}
15 ],
16 model="gpt-3.5-turbo" # Ensure you are using an appropriate model that you have access to
17 )
18 # Correctly accessing the first message content from the response
19 if chat_completion and chat_completion.choices:
20 return chat_completion.choices[0].message.content.strip()
21 else:
22 return "No valid response received from the API."
23 except Exception as e:
24 print(f"Error generating commit message: {e}")
25 return ""