forked from ywkim/gpt-commit
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpt-commit.el
140 lines (112 loc) · 5.98 KB
/
gpt-commit.el
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
;;; gpt-commit.el --- GPT Conventional Commit Title -*- lexical-binding: t; -*-
;; Author: Youngwook Kim <youngwook.kim@gmail.com>
;; URL: https://github.com/ywkim/gpt-commit
;; Version: 0.0.1
;; Package-Requires: ((emacs "27.1") (magit "2.90") (request "0.3.2"))
;; SPDX-License-Identifier: GPL-3.0-or-later
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; A hook using GPT
;; (require 'gpt-commit)
;; (setq gpt-commit-openai-key "YOUR_OPENAI_API_KEY")
;; (setq gpt-commit-model-name "gpt-3.5-turbo-16k")
;; (add-hook 'git-commit-setup-hook 'gpt-commit-message)
;;; Code:
(provide 'gpt-commit)
(require 'magit)
(require 'request)
(defvar gpt-commit-openai-key nil "API key for the OpenAI.")
(defvar gpt-commit-model-name "gpt-3.5-turbo"
"Model name to use for GPT chat completions.")
(defconst gpt-commit-api-url "https://api.openai.com/v1/chat/completions"
"API endpoint for GPT chat completions.")
(defconst gpt-commit-system-prompt-en
"The user provides the result of running `git diff --cached`. You suggest a conventional commit message. Don't add anything else to the response. The following describes conventional commits.
# Conventional Commits 1.0.0
## Summary
The Conventional Commits specification is a lightweight convention on top of commit messages.
It provides an easy set of rules for creating an explicit commit history;
which makes it easier to write automated tools on top of.
This convention dovetails with [SemVer](http://semver.org),
by describing the features, fixes, and breaking changes made in commit messages.
The commit message should be structured as follows:
---
```
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
```
---
<br />
The commit contains the following structural elements, to communicate intent to the
consumers of your library:
1. **fix:** a commit of the _type_ `fix` patches a bug in your codebase (this correlates with [`PATCH`](http://semver.org/#summary) in Semantic Versioning).
1. **feat:** a commit of the _type_ `feat` introduces a new feature to the codebase (this correlates with [`MINOR`](http://semver.org/#summary) in Semantic Versioning).
1. **BREAKING CHANGE:** a commit that has a footer `BREAKING CHANGE:`, or appends a `!` after the type/scope, introduces a breaking API change (correlating with [`MAJOR`](http://semver.org/#summary) in Semantic Versioning).
A BREAKING CHANGE can be part of commits of any _type_.
1. _types_ other than `fix:` and `feat:` are allowed, for example [@commitlint/config-conventional](https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional) (based on the [Angular convention](https://github.com/angular/angular/blob/22b96b9/CONTRIBUTING.md#-commit-message-guidelines)) recommends `build:`, `chore:`,
`ci:`, `docs:`, `style:`, `refactor:`, `perf:`, `test:`, and others.
1. _footers_ other than `BREAKING CHANGE: <description>` may be provided and follow a convention similar to
[git trailer format](https://git-scm.com/docs/git-interpret-trailers).
Additional types are not mandated by the Conventional Commits specification, and have no implicit effect in Semantic Versioning (unless they include a BREAKING CHANGE).
<br /><br />
A scope may be provided to a commit's type, to provide additional contextual information and is contained within parenthesis, e.g., `feat(parser): add ability to parse arrays`.")
(defun gpt-commit-parse-response (data)
"Parse the GPT response DATA."
(let* ((choices (cdr (assoc 'choices data)))
(choice (elt choices 0))
(message (assoc 'message choice))
(content (cdr (assoc 'content message))))
(decode-coding-string content 'utf-8)))
(defun gpt-commit-openai-chat-completions-api (messages callback)
"Call OpenAI's Chat Completions API with MESSAGES and CALLBACK."
(let* ((headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " gpt-commit-openai-key))))
(json-string (json-serialize `((model . ,gpt-commit-model-name)
(messages . ,messages))))
(payload (encode-coding-string json-string 'utf-8)))
(request gpt-commit-api-url
:type "POST"
:headers headers
:data payload
:parser 'json-read
:timeout 10
:success
(cl-function
(lambda (&key data &allow-other-keys)
(funcall callback (gpt-commit-parse-response data))))
:error
(cl-function
(lambda (&rest args &key data error-thrown &allow-other-keys)
(message "Error: %s %s" error-thrown data))))))
(defun gpt-commit-generate-message (callback)
"Generate a commit message using GPT and pass it to the CALLBACK."
(let* ((lines (magit-git-lines "diff" "--cached"))
(changes (string-join lines "\n"))
(messages `[((role . "system")
(content . ,gpt-commit-system-prompt-en))
((role . "user")
(content . ,changes))]))
(gpt-commit-openai-chat-completions-api messages callback)))
(defun gpt-commit-message ()
"Automatically generate a commit message using GPT."
(interactive)
(let ((buffer (current-buffer)))
(gpt-commit-generate-message
(lambda (commit-message)
(when commit-message
(with-current-buffer buffer
(insert commit-message)))))))
;;; gpt-commit.el ends here