Skip to content

Commit

Permalink
feat: Update GptClient to accept model, improve README.md and use con…
Browse files Browse the repository at this point in the history
…text in request
  • Loading branch information
nguyenvanduocit committed Mar 29, 2023
1 parent a5ba790 commit 4ce6354
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

> No more headaches with commit messages.
AI-Commit is a command line tool that uses OpenAI's language generation capabilities to generate conventional commit messages for your Git repositories.
AI-Commit is a command line tool that uses OpenAI's ChatGPT model to generate commit messages for your Git repositories. As a result, you can write meaningful commit messages without having to think about them.

## Prerequisites

Expand Down
9 changes: 5 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,30 @@ type ChatCompleteRequest struct {

type GptClient struct {
apiKey string
model string
httpClient *http.Client
}

const chatModel = "gpt-3.5-turbo"
const chatEndpoint = "https://api.openai.com/v1/chat/completions"

func NewGptClient(apiKey string) *GptClient {
func NewGptClient(apiKey string, model string) *GptClient {
return &GptClient{
apiKey: apiKey,
model: model,
httpClient: http.DefaultClient,
}
}

func (c *GptClient) ChatComplete(ctx context.Context, messages []*Message) (string, error) {

request := &ChatCompleteRequest{
Model: chatModel,
Model: c.model,
Messages: messages,
}

payload, _ := json.Marshal(request)
payloadReader := bytes.NewReader(payload)
req, err := http.NewRequest("POST", chatEndpoint, payloadReader)
req, err := http.NewRequestWithContext(ctx, "POST", chatEndpoint, payloadReader)
if err != nil {
return "", err
}
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func main() {
os.Exit(1)
}

model := os.Getenv("AI_COMMIT_MODEL")
if model == "" {
model = "gpt-3.5-turbo"
}

systemPrompt := os.Getenv("AI_COMMIT_SYSTEM_PROMPT")
if systemPrompt == "" {
systemPrompt = `You are a GitCommitGPT-4, You will help user to write commit message, commit message should be short (less than 100 chars), clean and meaningful. Only response the message.`
Expand All @@ -33,7 +38,7 @@ func main() {
},
}

client := NewGptClient(apiKey)
client := NewGptClient(apiKey, model)

// prepare the diff
diff, err := getDiff()
Expand Down

0 comments on commit 4ce6354

Please sign in to comment.