Skip to content

Commit a469eca

Browse files
committed
updates and bug fix
1 parent 3c292ac commit a469eca

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

app/jobs/generate_automatic_comments_job.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,33 @@ class GenerateAutomaticCommentsJob < ApplicationJob
55
def perform(request_for_comment, current_user)
66
chat_gpt_user = InternalUser.find_by(email: '[email protected]')
77
chat_gpt_service = ChatGptService::ChatGptRequest.new
8-
chat_gpt_disclaimer = I18n.t('exercises.editor.chat_gpt_disclaimer')
98
request_for_comment.submission.files.each do |file|
10-
response_data = perform_chat_gpt_request(request_for_comment, file, chat_gpt_service)
9+
response_data = perform_request(request_for_comment, file, chat_gpt_service)
1110
next unless response_data.present?
1211

13-
create_general_comments(
12+
create_general_comment(
1413
response_data,
1514
file,
1615
chat_gpt_user,
17-
chat_gpt_disclaimer,
1816
request_for_comment,
1917
current_user
2018
)
2119

2220
# Create comments for each line-specific comment
23-
create_line_specific_comments(response_data, file, chat_gpt_user, chat_gpt_disclaimer)
21+
create_line_comments(response_data, file, chat_gpt_user)
2422
end
2523
end
2624

2725
private
2826

29-
def perform_chat_gpt_request(request_for_comment, file, chat_gpt_service)
27+
def perform_request(request_for_comment, file, chat_gpt_service)
3028
prompt = ChatGptHelper.format_prompt(
3129
learner_solution: file.content,
3230
exercise: request_for_comment.submission.exercise.description,
3331
test_results: Testrun.where(submission_id: request_for_comment.submission.id).map(&:log).join("\n"),
3432
question: request_for_comment.question
3533
)
36-
response = chat_gpt_service.make_chat_gpt_request(prompt, true)
34+
response = chat_gpt_service.execute(prompt, true)
3735
ChatGptHistoryOnRfc.create!(
3836
rfc_id: request_for_comment.id,
3937
prompt: prompt,
@@ -42,10 +40,10 @@ def perform_chat_gpt_request(request_for_comment, file, chat_gpt_service)
4240
ChatGptHelper.format_response(response)
4341
end
4442

45-
def create_general_comments(response_data, file, chat_gpt_user, chat_gpt_disclaimer, request_for_comment, current_user)
43+
def create_general_comment(response_data, file, chat_gpt_user, request_for_comment, current_user)
4644
if response_data[:requirements_comments].present?
4745
comment = create_comment(
48-
text: "#{response_data[:requirements_comments]}\n\n#{chat_gpt_disclaimer}",
46+
text: response_data[:requirements_comments],
4947
file_id: file.id,
5048
row: '0',
5149
column: '0',
@@ -55,10 +53,10 @@ def create_general_comments(response_data, file, chat_gpt_user, chat_gpt_disclai
5553
send_emails(comment, request_for_comment, current_user, chat_gpt_user) if comment.persisted?
5654
end
5755

58-
def create_line_specific_comments(response_data, file, chat_gpt_user, chat_gpt_disclaimer)
56+
def create_line_comments(response_data, file, chat_gpt_user)
5957
response_data[:line_specific_comments].each do |line_comment|
60-
comment = create_comment(
61-
text: "#{line_comment[:comment]}\n\n#{chat_gpt_disclaimer}",
58+
create_comment(
59+
text: line_comment[:comment],
6260
file_id: file.id,
6361
row: (line_comment[:line_number].positive? ? line_comment[:line_number] - 1 : line_comment[:line_number]).to_s,
6462
column: '0',
@@ -68,8 +66,9 @@ def create_line_specific_comments(response_data, file, chat_gpt_user, chat_gpt_d
6866
end
6967

7068
def create_comment(attributes)
69+
chat_gpt_disclaimer = I18n.t('exercises.editor.chat_gpt_disclaimer')
7170
Comment.create(
72-
text: attributes[:text],
71+
text: "#{attributes[:text]}\n\n#{chat_gpt_disclaimer}",
7372
file_id: attributes[:file_id],
7473
row: attributes[:row],
7574
column: attributes[:column],

app/models/testrun.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ def generate_ai_feedback
4040

4141
chatgpt_request = ChatGptService::ChatGptRequest.new
4242

43+
4344
prompt = ChatGptHelper.format_prompt(
44-
learner_solution: submission.main_file.content,
45-
exercise: submission.exercise.description,
45+
learner_solution: submission&.files&.select { |file| !file.read_only }.map(&:content).join("\n\n"),
46+
exercise: submission&.exercise&.description,
4647
test_results: output
4748
)
4849

49-
feedback_message = chatgpt_request.make_chat_gpt_request(prompt, false)
50+
feedback_message = chatgpt_request.execute(prompt, false)
5051

5152
# Store AI feedback history
5253
ChatGptHistoryOnScore.create!(

app/services/chat_gpt_service/chat_gpt_request.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def initialize
88
@client = OpenAI::Client.new(access_token: fetch_api_key)
99
end
1010

11-
def make_chat_gpt_request(prompt, response_format_needed = false)
11+
def execute(prompt, response_format_needed = false)
1212
wrap_api_error! do
1313
data = {
1414
model: MODEL_NAME,

0 commit comments

Comments
 (0)