-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSGAC_AI.py
More file actions
74 lines (59 loc) · 2.06 KB
/
SGAC_AI.py
File metadata and controls
74 lines (59 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# A Commit Message generator
# Powered by Groq AI
# karjok Pangesty
import re
import sys
import os
import subprocess
from groq import Groq
PROMPT = """
Summarize the given diff and Generate infomative commit message for following git diff with maximum 50 characters for TITLE and maximum 70 characters for DESCRIPTION:
```
%s
```
Please add Preffix by category:
- [ADD]: For new features, functions, or files.
- [FIX]: For bug fixes or corrections.
- [UPDATE]: For updates or modifications to existing code.
- [REMOVE]: For deletions of code or functionality.
- [DEBUG]: For general tasks, maintenance, or minor changes.
THE FINAL MESSAGE FORMAT:
```
[$PREFFIX] - $TITLE
$DESCRIPTION
```
DO NOT ANSWER WITH OTHER WORDS, JUST ANSWER WITH THE FINAL MESSAGE FORMAT ONLY
"""
def generate_commit_message_for(API_KEY,diff):
prompt = PROMPT % diff
client = Groq(
api_key= API_KEY,
)
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt,
}
],
model="llama3-8b-8192",
)
response = chat_completion.choices[0].message.content
try:
response = re.search(r"(\[[(['$A-Z]{3,10}\] ?\-? ?.*)\W{0,3}(?:description\:)?(.*)", response, re.IGNORECASE)
title = response.group(1)
description = response.group(2)
return title.replace("$",""), description.strip()
except:
return "[ERROR] Error when gerating commit message", response
def commit_message_from_AI(file_path, script_path):
script_dir = os.path.dirname(script_path)
API_KEY = open(os.path.join(script_dir, "GROQ_API_KEY.txt"), "r").read()
diff = subprocess.run(["git", "diff", "--", file_path], cwd=os.path.dirname(file_path), stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
title, commit_message = generate_commit_message_for(API_KEY, diff)
print(title)
print(commit_message)
if __name__ == "__main__":
file_path = sys.argv[1]
script_path = sys.argv[2]
commit_message_from_AI(file_path, script_path)