Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Writes git commit messages with AI #670

Closed
@chihsuan

Description

@chihsuan

Description

Use open AI to write commit message automatically

Possible Solution

Add an option to the commit command to write a commitizen-style commit message using Open AI API.

Additional context

There is an AI commit tool: https://github.com/Nutlope/aicommits

Additional context

Ask ChatGPT to write a Python script to write a commit message based on git diff using open API in the conventional Commits style with characters limits < 8000:

import openai
import subprocess

# Authenticate with OpenAI API
openai.api_key = "YOUR_API_KEY_HERE"

git_diff = subprocess.check_output(["git", "diff"])

# Check if diff length is too large
if len(git_diff) > 8000:
  print("The diff is too large to write a commit message.")
  exit()

# Prepare prompt for OpenAI in Conventional Commits style
prompt = f"Please generate a commit message in Conventional Commits style for the following changes:\n\n{git_diff.decode('utf-8')}\n\nType 'feat' for a new feature, 'fix' for a bug fix, 'docs' for documentation updates, 'style' for code style changes, 'refactor' for code refactoring, 'test' for test updates, 'chore' for build and tooling updates, or 'other' for any other changes:"

# Generate text with OpenAI's GPT-3
response = openai.Completion.create(
  engine="text-davinci-002",
  prompt=prompt,
  max_tokens=64,
  n=1,
  stop=None,
  temperature=0.7,
)

# Extract the generated text from the OpenAI response
commit_type = response.choices[0].text.strip()

# Format the commit message using the commit type and the diff
commit_message = f"{commit_type}: {git_diff.decode('utf-8').splitlines()[0]}"

# Add and commit changes with formatted message
subprocess.call(["git", "add", "."])
subprocess.call(["git", "commit", "-m", commit_message])

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions