Skip to content

Instantly share code, notes, and snippets.

@alexaivars
Created January 18, 2024 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexaivars/58121cb0de2c04f94e3dffc8b9a55c24 to your computer and use it in GitHub Desktop.
Save alexaivars/58121cb0de2c04f94e3dffc8b9a55c24 to your computer and use it in GitHub Desktop.
Analyzes the output of 'git diff --cached' to generate a meaningful commit message.
package main
import (
"bytes"
"context"
"fmt"
"log"
"os"
"os/exec"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/cognitiveservices/azopenai"
)
func main() {
azureOpenAIDeploymentAdvanced := os.Getenv("AZURE_OPENAI_DEPLOYMENT_ADVANCED")
azureOpenAIEndpoint := os.Getenv("AZURE_OPENAI_ENDPOINT")
azureOpenAIKey := os.Getenv("AZURE_OPENAI_KEY")
if azureOpenAIDeploymentAdvanced == "" || azureOpenAIEndpoint == "" || azureOpenAIKey == "" {
log.Fatal("Please set the following environment variables: AZURE_OPENAI_DEPLOYMENT_ADVANCED, AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_KEY")
}
gitDiffText, err := getGitDiff()
if err != nil {
log.Fatalf("Failed to get git diff: %v", err)
}
message, err := generateMessage(azureOpenAIEndpoint, azureOpenAIKey, azureOpenAIDeploymentAdvanced, gitDiffText)
if err != nil {
log.Fatalf("Failed to generate message: %v", err)
}
fmt.Println(message)
}
func getGitDiff() (string, error) {
cmd := exec.Command("git", "diff", "--cached")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", err
}
return out.String(), nil
}
func generateMessage(endpoint, key, deployment, gitDiffText string) (string, error) {
cred, err := azopenai.NewKeyCredential(key)
if err != nil {
return "", err
}
client, err := azopenai.NewClientWithKeyCredential(endpoint, cred, nil)
if err != nil {
return "", err
}
messages := []azopenai.ChatMessage{
{
Role: to.Ptr(azopenai.ChatRoleSystem),
Content: to.Ptr(`
Your task is to analyze the output of a 'git diff' command to generate a concise and meaningful commit message.
Look for changes in the code, such as additions, deletions, or modifications.
Identify the purpose of these changes and the files affected.
Consider coding style alterations, feature implementations, bug fixes, refactoring, or documentation updates.
Based on your analysis, summarize the changes in a simple, clear commit message.
`),
},
{
Role: to.Ptr(azopenai.ChatRoleUser),
Content: to.Ptr(fmt.Sprintf("Please analyze the following 'git diff' output and generate a commit message: %s", gitDiffText)),
},
}
resp, err := client.GetChatCompletions(context.Background(), azopenai.ChatCompletionsOptions{
Messages: messages,
MaxTokens: to.Ptr(int32(512)),
Temperature: to.Ptr(float32(0.7)),
DeploymentID: deployment,
}, nil)
if err != nil {
return "", err
}
if len(resp.Choices) == 0 {
return "", fmt.Errorf("no choices returned in the response")
}
return *resp.Choices[0].Message.Content, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment