AI-Powered Git Commit Message Generator

0 downloads

askrobots_commitgen/ai_modify.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_modify.py · 1.2 KB · 38 lines
1import sys
2from settings import OPENAI_API_KEY, OPENAI_MODEL
3import openai
4
5def modify_file(file_path: str, change_description: str) -> None:
6 try:
7 with open(file_path, 'r') as file:
8 file_contents = file.read()
9
10 openai.api_key = OPENAI_API_KEY
11 response = openai.ChatCompletion.create(
12 model=OPENAI_MODEL,
13 messages=[
14 {"role": "system", "content": "You are a helpful assistant."},
15 {"role": "user", "content": f"Modify the following file based on this description: '{change_description}'.\n\nFile content:\n{file_contents}\n\nModified file:"}
16 ]
17 )
18 modified_contents = response['choices'][0]['message']['content'].strip()
19
20 with open(file_path, 'w') as file:
21 file.write(modified_contents)
22
23 print(f"File '{file_path}' modified based on description: '{change_description}'")
24 except Exception as e:
25 print(f"Error modifying file: {e}")
26
27def main():
28 if len(sys.argv) < 3:
29 print("Usage: python ai_modify.py <file_path> <change_description>")
30 return
31
32 file_path = sys.argv[1]
33 change_description = sys.argv[2]
34
35 modify_file(file_path, change_description)
36
37if __name__ == "__main__":
38 main()