-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare-commit-msg
executable file
·67 lines (52 loc) · 2.05 KB
/
prepare-commit-msg
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
#!/usr/bin/env python3
import os
import sys
import sh
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.pydantic_v1 import BaseModel, Field
myself = sys.argv[0]
git = sh.git.bake("--no-pager")
if not os.getenv("GPTCOMMIT"):
exit(0)
commit_msg_file = sys.argv[1]
diff = git.diff("--cached")
class GitMessage(BaseModel):
subject: str = Field(description="The subject of the git message")
message_body: str = Field(
description="The message body of the git message")
# ref https://github.com/zurawiki/gptcommit/tree/main/prompts
prompt_template = """
You are an expert programmer writing a commit message.
Write a commit message following the Linux kernel git commit style, including a subject and message body.
The requirements for the subject are as follows:
1. one line and no more than 50 characters
2. The first letter should be capitalized.
For the message body, the requirements are as follows:
1. Write every summary comment in a new line.
2. Comments should be in a bullet point list, each line starting with a `-`.
3. The summary should not include comments copied from the code.
4. The output should be easily readable. When in doubt, write fewer comments and not more. Do not output comments that
simply repeat the contents of the file.
5. Readability is top priority. Write only the most important comments about the diff.
========= patch start ==============
{patch}
========= patch end ================
Respond in JSON with `subject` and `message_body` keys, and the message_body should be a string"
"""
prompt = PromptTemplate(
input_variables=["patch"], template=prompt_template
)
llm = ChatOpenAI(
model="gpt-3.5-turbo").with_structured_output(GitMessage, method="json_mode")
chain = prompt | llm
output = chain.invoke(diff)
subject = output.subject
message = output.message_body
commit_msg = f"{subject}\n\n{message}"
content = None
with open(commit_msg_file) as fd:
content = fd.read()
msg = commit_msg + content
with open(commit_msg_file, "w") as fd:
fd.write(msg)