@@ -5,6 +5,8 @@ defmodule CadetWeb.AICodeAnalysisController do
55 require Logger
66
77 alias Cadet . { Assessments , AIComments , Courses }
8+ alias CadetWeb.AICodeAnalysisController
9+ alias CadetWeb.AICommentsHelpers
810
911 # For logging outputs to both database and file
1012 defp save_comment ( answer_id , raw_prompt , answers_json , response , error \\ nil ) do
@@ -84,7 +86,7 @@ defmodule CadetWeb.AICodeAnalysisController do
8486 with { answer_id_parsed , "" } <- Integer . parse ( answer_id ) ,
8587 { :ok , course } <- Courses . get_course_config ( course_id ) ,
8688 { :ok } <- ensure_llm_enabled ( course ) ,
87- { :ok , key } <- decrypt_llm_api_key ( course . llm_api_key ) ,
89+ { :ok , key } <- AICommentsHelpers . decrypt_llm_api_key ( course . llm_api_key ) ,
8890 { :ok } <-
8991 check_llm_grading_parameters (
9092 key ,
@@ -123,7 +125,7 @@ defmodule CadetWeb.AICodeAnalysisController do
123125 { :decrypt_error , err } ->
124126 conn
125127 |> put_status ( :internal_server_error )
126- |> text ( "Failed to decrypt LLM API key: #{ inspect ( err ) } " )
128+ |> text ( "Failed to decrypt LLM API key" )
127129
128130 # Errors for check_llm_grading_parameters
129131 { :parameter_error , error_msg } ->
@@ -152,25 +154,25 @@ defmodule CadetWeb.AICodeAnalysisController do
152154 "\n \n " <>
153155 ( assessment_prompt || "" ) <>
154156 "\n \n " <>
155- """
156- **Additional Instructions for this Question:**
157- #{ answer . question . question [ "llm_prompt" ] || "N/A" }
157+ """
158+ **Additional Instructions for this Question:**
159+ #{ answer . question . question [ "llm_prompt" ] || "N/A" }
158160
159- **Question:**
160- ```
161- #{ answer . question . question [ "content" ] || "N/A" }
162- ```
161+ **Question:**
162+ ```
163+ #{ answer . question . question [ "content" ] || "N/A" }
164+ ```
163165
164- **Model Solution:**
165- ```
166- #{ answer . question . question [ "solution" ] || "N/A" }
167- ```
166+ **Model Solution:**
167+ ```
168+ #{ answer . question . question [ "solution" ] || "N/A" }
169+ ```
168170
169- **Autograding Status:** #{ answer . autograding_status || "N/A" }
170- **Autograding Results:** #{ format_autograding_results ( answer . autograding_results ) }
171+ **Autograding Status:** #{ answer . autograding_status || "N/A" }
172+ **Autograding Results:** #{ format_autograding_results ( answer . autograding_results ) }
171173
172- The student answer will be given below as part of the User Prompt.
173- """
174+ The student answer will be given below as part of the User Prompt.
175+ """
174176 end
175177
176178 defp format_autograding_results ( nil ) , do: "N/A"
@@ -365,35 +367,5 @@ defmodule CadetWeb.AICodeAnalysisController do
365367 }
366368 end
367369
368- defp decrypt_llm_api_key ( nil ) , do: nil
369370
370- defp decrypt_llm_api_key ( encrypted_key ) do
371- case Application . get_env ( :openai , :encryption_key ) do
372- secret when is_binary ( secret ) and byte_size ( secret ) >= 16 ->
373- key = binary_part ( secret , 0 , min ( 32 , byte_size ( secret ) ) )
374-
375- case Base . decode64 ( encrypted_key ) do
376- { :ok , decoded } ->
377- iv = binary_part ( decoded , 0 , 16 )
378- tag = binary_part ( decoded , 16 , 16 )
379- ciphertext = binary_part ( decoded , 32 , byte_size ( decoded ) - 32 )
380-
381- case :crypto . crypto_one_time_aead ( :aes_gcm , key , iv , ciphertext , "" , tag , false ) do
382- plain_text when is_binary ( plain_text ) -> { :ok , plain_text }
383- _ -> { :decrypt_error , :decryption_failed }
384- end
385-
386- _ ->
387- Logger . error (
388- "Failed to decode encrypted key, is it a valid AES-256 key of 16, 24 or 32 bytes?"
389- )
390-
391- { :decrypt_error , :decryption_failed }
392- end
393-
394- _ ->
395- Logger . error ( "Encryption key not configured" )
396- { :decrypt_error , :invalid_encryption_key }
397- end
398- end
399371end
0 commit comments