Class: GitAuto::Services::AIService
- Inherits:
-
Object
- Object
- GitAuto::Services::AIService
show all
- Defined in:
- lib/git_auto/services/ai_service.rb
Defined Under Namespace
Classes: APIKeyError, DiffTooLargeError, EmptyDiffError, Error, RateLimitError
Constant Summary
collapse
- OPENAI_API_URL =
"https://api.openai.com/v1/chat/completions"
- CLAUDE_API_URL =
"https://api.anthropic.com/v1/messages"
- GEMINI_API_URL =
"https://generativelanguage.googleapis.com/v1beta/models"
- MAX_DIFF_SIZE =
10_000
- MAX_RETRIES =
3
- BACKOFF_BASE =
2
- TEMPERATURE_VARIATIONS =
[
{ openai: 0.7, claude: 0.7, gemini: 0.7 },
{ openai: 0.8, claude: 0.8, gemini: 0.8 },
{ openai: 0.9, claude: 0.9, gemini: 0.9 },
{ openai: 1.0, claude: 1.0, gemini: 1.0 }
].freeze
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(settings) ⇒ AIService
Returns a new instance of AIService.
Class Method Details
.reset_temperature ⇒ Object
23
24
25
|
# File 'lib/git_auto/services/ai_service.rb', line 23
def reset_temperature
@@temperature = nil
end
|
Instance Method Details
#generate_commit_message(diff, style: :conventional, scope: nil) ⇒ Object
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/git_auto/services/ai_service.rb', line 154
def generate_commit_message(diff, style: :conventional, scope: nil)
raise EmptyDiffError, "No changes to commit" if diff.empty?
if diff.length > MAX_DIFF_SIZE
puts "\n⚠️ Diff is large, using summarized version...".yellow if @debug_mode
diff = @diff_summarizer.summarize(diff)
end
@settings.set(:commit_style, style.to_s)
retries = 0
begin
case @settings.get(:ai_provider)
when "openai"
generate_openai_commit_message(diff, style, scope)
when "claude"
generate_claude_commit_message(diff, style, scope)
when "gemini"
generate_gemini_commit_message(diff, style, scope)
else
raise GitAuto::Errors::InvalidProviderError, "Invalid AI provider specified"
end
rescue StandardError => e
retries += 1
if retries < MAX_RETRIES
sleep(retries * BACKOFF_BASE)
retry
end
raise e
end
end
|
#generate_conventional_commit(diff) ⇒ Object
138
139
140
|
# File 'lib/git_auto/services/ai_service.rb', line 138
def generate_conventional_commit(diff)
generate_commit_message(diff, style: :conventional)
end
|
#generate_scoped_commit(diff, scope) ⇒ Object
146
147
148
|
# File 'lib/git_auto/services/ai_service.rb', line 146
def generate_scoped_commit(diff, scope)
generate_commit_message(diff, style: :conventional, scope: scope)
end
|
#generate_simple_commit(diff) ⇒ Object
142
143
144
|
# File 'lib/git_auto/services/ai_service.rb', line 142
def generate_simple_commit(diff)
generate_commit_message(diff, style: :simple)
end
|
#get_system_prompt(style, retry_attempt = 0) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/git_auto/services/ai_service.rb', line 74
def get_system_prompt(style, retry_attempt = 0)
base_prompt = case style.to_s
when "minimal"
"You are an expert in writing minimal commit messages that follow the format: <type>: <description>\n" \
"Rules:\n" \
"1. ALWAYS start with a type from the list above\n" \
"2. NEVER include a scope\n" \
"3. Keep the message under 72 characters\n" \
"4. ALWAYS use lowercase - this is mandatory\n" \
"5. Use present tense\n" \
"6. Be descriptive but concise\n" \
"7. Do not include a period at the end"
when "conventional"
"You are an expert in writing conventional commit messages that follow the format: <type>(<scope>): <description>\n" \
"Rules:\n" \
"1. ALWAYS start with a type from the list above\n" \
"2. Include a scope in parentheses when relevant\n" \
"3. Keep the message under 72 characters\n" \
"4. ALWAYS use lowercase - this is mandatory\n" \
"5. Use present tense\n" \
"6. Be descriptive but concise\n" \
"7. Do not include a period at the end"
when "detailed"
"You are an expert in writing detailed commit messages. Your message MUST follow this format:\n" \
"<summary line>\n" \
"\n" \
"<detailed description>\n" \
"\n" \
"Rules:\n" \
"1. First line is a summary under 72 characters\n" \
"2. ALWAYS use lowercase - this is mandatory\n" \
"3. ALWAYS include a blank line after the summary\n" \
"4. ALWAYS include a detailed description explaining:\n" \
" - What changes were made\n" \
" - Why the changes were necessary\n" \
" - Any technical details worth noting\n" \
"5. Use bullet points for multiple changes\n" \
"6. Use present tense\n" \
"7. You can use periods in the detailed description"
else
"You are an expert in writing clear and concise git commit messages.\n" \
"Rules:\n" \
"1. Keep the message under 72 characters\n" \
"2. ALWAYS use lowercase - this is mandatory\n" \
"3. Use present tense\n" \
"4. Be descriptive but concise\n" \
"5. Do not include a period at the end"
end
if retry_attempt.positive?
base_prompt += "\nPlease provide a different perspective or approach than previous attempts."
base_prompt += "\nBe more #{["specific", "detailed", "creative", "concise"].sample} in this attempt."
end
base_prompt
end
|
#get_temperature(retry_attempt = 0) ⇒ Object
65
66
67
68
69
70
71
72
|
# File 'lib/git_auto/services/ai_service.rb', line 65
def get_temperature(retry_attempt = 0)
provider = @settings.get(:ai_provider).to_sym
return TEMPERATURE_VARIATIONS[0][provider] if retry_attempt.zero?
variation_index = [retry_attempt - 1, TEMPERATURE_VARIATIONS.length - 1].min
TEMPERATURE_VARIATIONS[variation_index][provider]
end
|
#log_api_request(provider, payload, temperature) ⇒ Object
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/git_auto/services/ai_service.rb', line 46
def log_api_request(provider, payload, temperature)
return unless @debug_mode
puts "\n=== API Request ##{@request_count += 1} ===".yellow
puts "Provider: #{provider}"
puts "Temperature: #{temperature}"
puts "Full Payload:"
puts JSON.pretty_generate(payload)
puts "===================="
end
|
#log_api_response(response_body) ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/git_auto/services/ai_service.rb', line 57
def log_api_response(response_body)
return unless @debug_mode
puts "\n=== API Response ===".yellow
puts JSON.pretty_generate(JSON.parse(response_body.to_s))
puts "===================="
end
|
#next_temperature_variation ⇒ Object
132
133
134
135
136
|
# File 'lib/git_auto/services/ai_service.rb', line 132
def next_temperature_variation
@@temperature = (@@temperature + 1) % TEMPERATURE_VARIATIONS.length
provider = @settings.get(:ai_provider).to_sym
TEMPERATURE_VARIATIONS[@@temperature][provider]
end
|
#suggest_commit_scope(diff) ⇒ Object
150
151
152
|
# File 'lib/git_auto/services/ai_service.rb', line 150
def suggest_commit_scope(diff)
generate_commit_message(diff, style: :scope)
end
|