Class: AiCommitMessage::Suggester

Inherits:
Object
  • Object
show all
Defined in:
lib/ai-commit-message/suggester.rb

Instance Method Summary collapse

Constructor Details

#initialize(git_diff_output, git_log_output, git_current_branch) ⇒ Suggester

Returns a new instance of Suggester.



7
8
9
10
11
# File 'lib/ai-commit-message/suggester.rb', line 7

def initialize(git_diff_output, git_log_output, git_current_branch)
  @git_diff_output = git_diff_output
  @git_log_output = git_log_output
  @git_current_branch = git_current_branch
end

Instance Method Details

#generate_commit_message(url:, model:) ⇒ Object



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
# File 'lib/ai-commit-message/suggester.rb', line 13

def generate_commit_message(url:, model:)
  url = URI(url + '/api/generate')
  body = {
    model: model,
    prompt: "Create a concise git commit message with no more than 250 characters. Exclude anything unnecessary such as translation, backticks characters or multiple suggestions, since your entire response will be passed directly into git commit. Consider the following messages as example to follow: #{@git_log_output}.
        The First [#XXX] option is the branch name. The current branch name is: #{@git_current_branch}. Now Do it for the following git diff: #{@git_diff_output}",
    stream: false,
    options: { temperature: 0.8 }
  }
  json_body = body.to_json
  headers = { 'Content-Type' => 'application/json' }

  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)
  request.body = json_body
  headers.each { |key, value| request[key] = value }
  response = http.request(request)

  begin
    json = JSON.parse(response.body)
    return json['response'] if json
  rescue JSON::ParserError => e
    puts "Failed to parse API response as JSON: #{e.message}"
  end

  nil
end