- 
                Notifications
    You must be signed in to change notification settings 
- Fork 56
AI-powered marking #1248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            xxdydx
  wants to merge
  82
  commits into
  master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
feat/add-AI-generated-comments-grading
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    AI-powered marking #1248
Changes from 58 commits
      Commits
    
    
            Show all changes
          
          
            82 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      5cd6ac6
              
                feat: v1 of AI-generated comments
              
              
                xxdydx 853ba84
              
                feat: added logging of inputs and outputs
              
              
                xxdydx 4c37d14
              
                Update generate_ai_comments.ex
              
              
                xxdydx 8192b3d
              
                feat: function to save outputs to database
              
              
                xxdydx 8a235b3
              
                Format answers json before sending to LLM
              
              
                EugeneOYZ1203n d384e06
              
                Add LLM Prompt to question params when submitting assessment xml file
              
              
                EugeneOYZ1203n 98feac2
              
                Add LLM Prompt to api response when grading view is open
              
              
                EugeneOYZ1203n 7716d57
              
                feat: added llm_prompt from qn to raw_prompt
              
              
                xxdydx df34dbd
              
                feat: enabling/disabling of LLM feature by course level
              
              
                xxdydx 0a25fa8
              
                feat: added llm_grading boolean field to course creation API
              
              
                xxdydx 2723f5a
              
                feat: added api key storage in courses & edit api key/enable llm grading
              
              
                xxdydx 02f7ed1
              
                feat: encryption for llm_api_key
              
              
                xxdydx cb34984
              
                feat: added final comment editing route
              
              
                xxdydx 09a7b09
              
                feat: added logging of chosen comments
              
              
                xxdydx ed44a7e
              
                fix: bugs when certain fields were missing
              
              
                xxdydx 3715368
              
                feat: updated tests
              
              
                xxdydx 5bfe276
              
                formatting
              
              
                xxdydx c27b93b
              
                Merge branch 'master' into feat/add-AI-generated-comments-grading
              
              
                xxdydx 17884fd
              
                fix: error handling when calling openai API
              
              
                xxdydx f91cc92
              
                fix: credo issues
              
              
                xxdydx 81e5bf7
              
                formatting
              
              
                xxdydx 8f8b93a
              
                Merge branch 'master' into feat/add-AI-generated-comments-grading
              
              
                RichDom2185 1ec67d7
              
                Address some comments
              
              
                Tkaixiang 4f2af5d
              
                Fix formatting
              
              
                Tkaixiang ec67aa3
              
                rm IO.inspect
              
              
                Tkaixiang 11ff272
              
                a
              
              
                Tkaixiang 1a77f67
              
                Use case instead of if
              
              
                Tkaixiang 02922e9
              
                Streamlines generate_ai_comments to only send the selected question a…
              
              
                Tkaixiang f068aa9
              
                Remove unncessary field
              
              
                Tkaixiang 5f3ad2c
              
                default: false for llm_grading
              
              
                Tkaixiang 34d326c
              
                Add proper linking between ai_comments table and submissions. Return …
              
              
                Tkaixiang 6ff2864
              
                Resolve some migration comments
              
              
                Tkaixiang 0854822
              
                Add llm_model and llm_api_url to the DB + schema
              
              
                Tkaixiang f0ccaf6
              
                Moves api key, api url, llm model and course prompt to course level
              
              
                Tkaixiang d14c03e
              
                Add encryption_key to env
              
              
                Tkaixiang c345e03
              
                Do not hardcode formatting instructions
              
              
                Tkaixiang b782641
              
                Add Assessment level prompts to the XML
              
              
                Tkaixiang c4defb9
              
                Return some additional info for composing of prompts
              
              
                Tkaixiang 04590f4
              
                Remove un-used 'save comments'
              
              
                Tkaixiang 2920dda
              
                Fix existing assessment tests
              
              
                Tkaixiang 9088279
              
                Fix generate_ai_comments test cases
              
              
                Tkaixiang 714476a
              
                Fix bug preventing avengers from generating ai comments
              
              
                Tkaixiang 5573a21
              
                Fix up tests + error msgs
              
              
                Tkaixiang efc4c57
              
                Formatting
              
              
                Tkaixiang c68f331
              
                Merge branch 'master' of github.com:source-academy/backend into feat/…
              
              
                Tkaixiang aa84560
              
                some mix credo suggestions
              
              
                Tkaixiang 4537270
              
                format
              
              
                Tkaixiang 62b6437
              
                Fix credo issue
              
              
                Tkaixiang 6e769fd
              
                bug fix + credo fixes
              
              
                Tkaixiang 1bd9398
              
                Fix tests
              
              
                Tkaixiang 4c341d1
              
                format
              
              
                Tkaixiang bd6e677
              
                Merge branch 'master' into feat/add-AI-generated-comments-grading
              
              
                RichDom2185 2ad0056
              
                Merge branch 'master' into feat/add-AI-generated-comments-grading
              
              
                Tkaixiang 2947f3c
              
                Modify test.exs
              
              
                Tkaixiang 8669e3a
              
                Update lib/cadet_web/controllers/generate_ai_comments.ex
              
              
                Tkaixiang 58345d4
              
                Copilot feedback
              
              
                Tkaixiang 353b533
              
                Merge branch 'feat/add-AI-generated-comments-grading' of github.com:s…
              
              
                Tkaixiang f794a14
              
                format
              
              
                Tkaixiang a9e2b2a
              
                Work on sentry comments
              
              
                Tkaixiang 3e96d6b
              
                Fix type
              
              
                Tkaixiang 5bf1aa6
              
                Redate migrations to maintain total order
              
              
                RichDom2185 f2c02c9
              
                Add newline at EOF
              
              
                RichDom2185 0d2f0c2
              
                Fix indent
              
              
                RichDom2185 8cf09b9
              
                Fix capitalization
              
              
                RichDom2185 23541e7
              
                Remove llmApiKey from any kind of storage on FE
              
              
                Tkaixiang 9ecb1d6
              
                Remove indexes
              
              
                Tkaixiang e26b33f
              
                rm todo
              
              
                Tkaixiang 0940dd8
              
                rm todo
              
              
                Tkaixiang 654c9f4
              
                Re-format ai_comments to reference answer_id instead
              
              
                Tkaixiang af395df
              
                Abstract out + remove un-used field
              
              
                Tkaixiang f1b8154
              
                Add delimeter + bug fixes
              
              
                Tkaixiang 3450096
              
                Mix format
              
              
                Tkaixiang d02383d
              
                Switch to openAI module
              
              
                Tkaixiang e95766e
              
                rm un-used
              
              
                Tkaixiang 1091e77
              
                Merge all prompts to :prompts to preserve abstraction
              
              
                Tkaixiang 8b57933
              
                rm
              
              
                Tkaixiang 818d1d7
              
                Fix formatting
              
              
                Tkaixiang 9556c25
              
                Fix test
              
              
                Tkaixiang c9a650d
              
                Revert some dependency changes
              
              
                RichDom2185 ea3bb1f
              
                Update actions versions
              
              
                RichDom2185 d43fe02
              
                Improve encrypt + decrypt robustness
              
              
                Tkaixiang c0c434a
              
                Fix dialyzer
              
              
                Tkaixiang File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -118,3 +118,6 @@ erl_crash.dump | |
|  | ||
| # Generated lexer | ||
| /src/source_lexer.erl | ||
|  | ||
| # Ignore log files | ||
| /log | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| defmodule Cadet.AIComments do | ||
| @moduledoc """ | ||
| Handles operations related to AI comments, including creation, updates, and retrieval. | ||
| """ | ||
|  | ||
| import Ecto.Query | ||
| alias Cadet.Repo | ||
| alias Cadet.AIComments.AIComment | ||
|  | ||
| @doc """ | ||
| Creates a new AI comment log entry. | ||
| """ | ||
| def create_ai_comment(attrs \\ %{}) do | ||
| %AIComment{} | ||
| |> AIComment.changeset(attrs) | ||
| |> Repo.insert() | ||
| end | ||
|  | ||
| @doc """ | ||
| Gets an AI comment by ID. | ||
| """ | ||
| def get_ai_comment(id) do | ||
| case Repo.get(AIComment, id) do | ||
| nil -> {:error, :not_found} | ||
| comment -> {:ok, comment} | ||
| end | ||
| end | ||
|  | ||
| @doc """ | ||
| Retrieves an AI comment for a specific submission and question. | ||
| Returns `nil` if no comment exists. | ||
| """ | ||
| def get_ai_comments_for_submission(submission_id, question_id) do | ||
|         
                  Tkaixiang marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| Repo.one( | ||
| from(c in AIComment, | ||
| where: c.submission_id == ^submission_id and c.question_id == ^question_id | ||
| ) | ||
| ) | ||
| end | ||
|  | ||
| @doc """ | ||
| Retrieves the latest AI comment for a specific submission and question. | ||
| Returns `nil` if no comment exists. | ||
| """ | ||
| def get_latest_ai_comment(submission_id, question_id) do | ||
| Repo.one( | ||
| from(c in AIComment, | ||
| where: c.submission_id == ^submission_id and c.question_id == ^question_id, | ||
| order_by: [desc: c.inserted_at], | ||
| limit: 1 | ||
| ) | ||
| ) | ||
| end | ||
|  | ||
| @doc """ | ||
| Updates the final comment for a specific submission and question. | ||
| Returns the most recent comment entry for that submission/question. | ||
| """ | ||
| def update_final_comment(submission_id, question_id, final_comment) do | ||
| comment = get_latest_ai_comment(submission_id, question_id) | ||
|  | ||
| case comment do | ||
| nil -> | ||
| {:error, :not_found} | ||
|  | ||
| _ -> | ||
| comment | ||
| |> AIComment.changeset(%{final_comment: final_comment}) | ||
| |> Repo.update() | ||
| end | ||
| end | ||
|  | ||
| @doc """ | ||
| Updates an existing AI comment with new attributes. | ||
| """ | ||
| def update_ai_comment(id, attrs) do | ||
| id | ||
| |> get_ai_comment() | ||
| |> case do | ||
| {:error, :not_found} -> | ||
| {:error, :not_found} | ||
|  | ||
| {:ok, comment} -> | ||
| comment | ||
| |> AIComment.changeset(attrs) | ||
| |> Repo.update() | ||
| end | ||
| end | ||
|  | ||
| @doc """ | ||
| Updates the chosen comments for a specific submission and question. | ||
| Accepts an array of comments and replaces the existing array in the database. | ||
| """ | ||
| def update_chosen_comments(submission_id, question_id, new_comments) do | ||
| comment = get_latest_ai_comment(submission_id, question_id) | ||
|  | ||
| case comment do | ||
| nil -> | ||
| {:error, :not_found} | ||
|  | ||
| _ -> | ||
| comment | ||
| |> AIComment.changeset(%{comment_chosen: new_comments}) | ||
| |> Repo.update() | ||
| end | ||
| end | ||
| end | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| defmodule Cadet.AIComments.AIComment do | ||
| @moduledoc """ | ||
| Defines the schema and changeset for AI comments. | ||
| """ | ||
|  | ||
| use Ecto.Schema | ||
| import Ecto.Changeset | ||
|  | ||
| schema "ai_comment_logs" do | ||
| field(:raw_prompt, :string) | ||
| field(:answers_json, :string) | ||
| field(:response, :string) | ||
| field(:error, :string) | ||
| field(:comment_chosen, {:array, :string}) | ||
| field(:final_comment, :string) | ||
|         
                  Tkaixiang marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| belongs_to(:submission, Cadet.Assessments.Submission) | ||
| belongs_to(:question, Cadet.Assessments.Question) | ||
|         
                  Tkaixiang marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| timestamps() | ||
| end | ||
|  | ||
| def changeset(ai_comment, attrs) do | ||
| ai_comment | ||
| |> cast(attrs, [ | ||
| :submission_id, | ||
| :question_id, | ||
| :raw_prompt, | ||
| :answers_json, | ||
| :response, | ||
| :error, | ||
| :comment_chosen, | ||
| :final_comment | ||
| ]) | ||
| |> validate_required([:submission_id, :question_id, :raw_prompt, :answers_json]) | ||
| |> foreign_key_constraint(:submission_id) | ||
| |> foreign_key_constraint(:question_id) | ||
| end | ||
| end | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.