@@ -6,6 +6,7 @@ defmodule CadetWeb.ChatController do
66 use PhoenixSwagger
77
88 alias Cadet.Chatbot . { Conversation , LlmConversations }
9+ @ max_content_size 1000
910
1011 def init_chat ( conn , % { "section" => section , "initialContext" => initialContext } ) do
1112 user = conn . assigns . current_user
@@ -21,7 +22,8 @@ defmodule CadetWeb.ChatController do
2122 "conversation_init.json" ,
2223 % {
2324 conversation_id: conversation . id ,
24- last_message: conversation . messages |> List . last ( )
25+ last_message: conversation . messages |> List . last ( ) ,
26+ max_content_size: @ max_content_size
2527 }
2628 )
2729
@@ -51,13 +53,15 @@ defmodule CadetWeb.ChatController do
5153 response ( 200 , "OK" )
5254 response ( 400 , "Missing or invalid parameter(s)" )
5355 response ( 401 , "Unauthorized" )
56+ response ( 422 , "Message exceeds the maximum allowed length" )
5457 response ( 500 , "When OpenAI API returns an error" )
5558 end
5659
5760 def chat ( conn , % { "conversationId" => conversation_id , "message" => user_message } ) do
5861 user = conn . assigns . current_user
5962
60- with { :ok , conversation } <-
63+ with true <- String . length ( user_message ) <= @ max_content_size || { :error , :message_too_long } ,
64+ { :ok , conversation } <-
6165 LlmConversations . get_conversation_for_user ( user . id , conversation_id ) ,
6266 { :ok , updated_conversation } <-
6367 LlmConversations . add_message ( conversation , "user" , user_message ) ,
@@ -85,6 +89,13 @@ defmodule CadetWeb.ChatController do
8589 send_resp ( conn , 500 , error_message )
8690 end
8791 else
92+ { :error , :message_too_long } ->
93+ send_resp (
94+ conn ,
95+ :unprocessable_entity ,
96+ "Message exceeds the maximum allowed length of #{ @ max_content_size } "
97+ )
98+
8899 { :error , { :not_found , error_message } } ->
89100 send_resp ( conn , :not_found , error_message )
90101
@@ -107,4 +118,6 @@ defmodule CadetWeb.ChatController do
107118
108119 conversation . prepend_context ++ messages_payload
109120 end
121+
122+ def max_content_length , do: @ max_content_size
110123end
0 commit comments