|
| 1 | +# AI Feedback Integration in CodeOcean |
| 2 | + |
| 3 | +This project integrates ChatGPT into CodeOcean to provide automated feedback for both **Request for Comments (RFCs)** and **test results**. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- **API Key:** Stored securely via Rails credentials |
| 8 | +- **Internal User:** Requires an internal user (`chatgpt@example.org`) to create comments |
| 9 | +- **Gem Required:** `gem 'ruby-openai'` |
| 10 | + |
| 11 | +## Overview |
| 12 | + |
| 13 | +- **AI API:** OpenAI Chat Completions |
| 14 | +- **Jobs:** Asynchronous processing via Solid Queue |
| 15 | +- **Frontend:** Adds "Request Feedback from AI" buttons for test results on Score |
| 16 | + |
| 17 | +## Key Components |
| 18 | + |
| 19 | +### ChatGPT Service |
| 20 | + |
| 21 | +Encapsulates API communication with ChatGPT. |
| 22 | + |
| 23 | +- **Implementation:** [`app/services/chat_gpt_service/chat_gpt_request.rb`](app/services/chat_gpt_service/chat_gpt_request.rb) |
| 24 | +- **Prompt files:** [`app/services/chat_gpt_service/chat_gpt_prompts/`](app/services/chat_gpt_service/chat_gpt_prompts/) (EN and DE versions) |
| 25 | +- **Structured Output Schema:** [`app/services/chat_gpt_service/chat_gpt_prompts/response_format.json`](app/services/chat_gpt_service/chat_gpt_prompts/response_format.json) |
| 26 | + |
| 27 | +**Key Method:** |
| 28 | +- `execute(prompt, structured_output)`: Sends prompt and receives response |
| 29 | + |
| 30 | +### ChatGPT Helper |
| 31 | + |
| 32 | +Responsible for formatting prompts and parsing responses. |
| 33 | + |
| 34 | +- [`app/helpers/chat_gpt_helper.rb`](app/helpers/chat_gpt_helper.rb) |
| 35 | +- `format_prompt`: Loads locale-specific templates and replaces placeholders in the prompt from application |
| 36 | +- `format_response`: Parses structured JSON response from chatGPT to create general commenta(line 0) and line comments for RFC. |
| 37 | + |
| 38 | +### Automatic Comment Job (RFC) |
| 39 | + |
| 40 | +Handles background comment generation when students submit a Request for Comments. |
| 41 | + |
| 42 | +- **Job class:** [`GenerateAutomaticCommentsJob`](app/jobs/generate_automatic_comments_job.rb) |
| 43 | +- **Service:** Uses `ChatGptRequest` to communicate with the API |
| 44 | +- **Process:** |
| 45 | + 1. Prompts are built from student code and context |
| 46 | + 2. API response is parsed |
| 47 | + 3. General and line-specific comments are created |
| 48 | + 4. Emails are sent using Solid Queue |
| 49 | + |
| 50 | +### ️Request Feedback From AI |
| 51 | + |
| 52 | +Allows students to request feedback per test result after scoring. |
| 53 | + |
| 54 | +- **Output modification:** |
| 55 | + [`app/models/submission.rb`](app/models/submission.rb) |
| 56 | + Adds `testrun_id` to each test result: |
| 57 | + ```ruby |
| 58 | + output.merge!(filename:, message: feedback_message(file, output), weight: file.weight, hidden_feedback: file.hidden_feedback, testrun_id: testrun.id) |
| 59 | + ``` |
| 60 | + |
| 61 | +- **Frontend integration:** |
| 62 | + [`app/assets/javascripts/editor/editor.js.erb`](app/assets/javascripts/editor/editor.js.erb) |
| 63 | + ```js |
| 64 | + card.attr('data-testrun-id', result.testrun_id); // Add testrun_id to the card |
| 65 | + ``` |
| 66 | + |
| 67 | +- **Triggering feedback request:** |
| 68 | + [`app/assets/javascripts/editor.js`](app/assets/javascripts/editor.js) |
| 69 | + Handles button click, calls backend route, and updates the UI with the ChatGPT feedback. |
| 70 | + |
| 71 | +- **Route and logic:** |
| 72 | + - [`app/controllers/submissions_controller.rb`](app/controllers/submissions_controller.rb): Handles `/testrun_ai_feedback_message` route |
| 73 | + - [`app/models/testrun.rb`](app/models/testrun.rb): Contains `generate_ai_feedback` method that builds prompt and fetches response |
| 74 | + |
| 75 | +### Exercise-Level Controls |
| 76 | + |
| 77 | +Instructors can toggle AI features per exercise using boolean flags: |
| 78 | + |
| 79 | +- `allow_ai_comment_for_rfc`: Enables RFC-based AI feedback |
| 80 | +- `allow_ai_feedback_on_score`: Enables test-based feedback |
| 81 | + |
| 82 | +--- |
0 commit comments