working v1

This commit is contained in:
Hassan El Mghari
2023-02-13 00:28:20 -05:00
parent 3280c0e2d1
commit a55661b7cc
5 changed files with 39 additions and 7 deletions

View File

@@ -1 +0,0 @@
{OPENAI_API_KEY: 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx'}

1
.example.env.json Normal file
View File

@@ -0,0 +1 @@
{ "OPENAI_API_KEY": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx" }

View File

@@ -1,3 +1,29 @@
# AI Commit # AI Commit
Have AI write your git commit messages for you so you never have to waste time writing commits again. Have AI Commit write your git commit messages for you so you never have to write a commit message again.
[![AI Commit Screenshot](./screenshot.png)](https://twitter.com/nutlope/status/1624646872890589184)
## How to install
1. Run `npm i -g zx` (this CLI uses zx)
2. Run `git clone github.com/nutlope/aicommit` in your root directory
3. Add a file called `.env.json` in the folder with your OpenAI API key. You can get this by [signing up](https://openai.com/api/)
4. [Optional] If you're on mac, add `alias aicommit="zx ~/ai-commit/aicommit.mjs"` to your `.zshrc` so you can trigger the program simply by typing `aicommit`
**Note:** The process to install this will get vastly simplified when I rewrite this CLI and publish it as an npm package to be run with `npx`.
## How it works
This project uses a command line interface tool called zx that's developed by google. In the script, it runs a `git diff` command to grab all the latest changes, sends this to OpenAI's GPT-3, then returns the AI generated commit message. Video coming soon where I rebuild it from scratch to show you how to easily build your own CLI tools powered by AI. Fully privacy friendly as commands only run on your local machine using your OpenAI account.
## Remaining tasks
- [x] Get a working first version with zx
- [ ] pwd trick to get rid of /Users/hassan from the code
- [ ] Add better error handling for missing git add . and commit msgs about a certain amount of chars
- [ ] Try openai curie And/OR codex
- [ ] Try supporting more than 200 lines by grabbing the diff per file
- [ ] Add support for conventional commits
- [ ] Rewrite this CLI tool using a Node framework and publish on npm to simplify the process
- [ ] Build landing page w/ demo

View File

@@ -1,12 +1,17 @@
#!/usr/bin/env zx #!/usr/bin/env zx
$.verbose = false; $.verbose = false;
// Add console.log with "no git diff"
console.log(chalk.white("▲ ") + chalk.green("Welcome to AICommit!")); console.log(chalk.white("▲ ") + chalk.green("Welcome to AICommit!"));
let { OPENAI_API_KEY } = await fs.readJson("./.env.json"); let { OPENAI_API_KEY } = await fs.readJson(
"/Users/hassan/dev/ai-commit/.env.json"
);
let diff = await quiet($`git diff --cached`); let diff = await quiet($`git diff --cached`);
let prompt = `Write one detailed commit message based on the following commit. Do not preface the commit with anything, write it right away: ${diff}`;
let prompt = `I want you to act like a git commit message writer. I will input a git diff and your job is to convert it into a useful commit message. Do not preface the commit with anything, do not repeat yourself, and return a complete sentence: ${diff}`;
// let prompt = `Write one detailed commit message based on the following commit. Do not preface the commit with anything, write it right away: `;
const payload = { const payload = {
model: "text-davinci-003", model: "text-davinci-003",
@@ -21,7 +26,7 @@ const payload = {
}; };
console.log( console.log(
chalk.white("▲ ") + chalk.black("Generating your AI commit message...") chalk.white("▲ ") + chalk.gray("Generating your AI commit message...")
); );
const response = await fetch("https://api.openai.com/v1/completions", { const response = await fetch("https://api.openai.com/v1/completions", {
@@ -40,13 +45,14 @@ let cleanedUpAiCommit = aiCommit.replace(/(\r\n|\n|\r)/gm, "");
echo(cleanedUpAiCommit); echo(cleanedUpAiCommit);
let confirmationMessage = await question( let confirmationMessage = await question(
"\nWould you like to use this commit message? " + chalk.yellow("(Y/n) \n"), "\nWould you like to use this commit message? " + chalk.yellow("(Y/n) "),
{ {
choices: ["Y", "n"], choices: ["Y", "n"],
} }
); );
$.verbose = true; $.verbose = true;
echo("\n");
if (confirmationMessage !== "n") { if (confirmationMessage !== "n") {
await $`git commit -m ${cleanedUpAiCommit}`; await $`git commit -m ${cleanedUpAiCommit}`;

BIN
screenshot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 962 KiB