@@ -27,8 +27,6 @@ class ShortAnswerAIEvalXBlock(AIEvalXBlock):
27
27
Short Answer Xblock.
28
28
"""
29
29
30
- USER_KEY = "USER"
31
- LLM_KEY = "LLM"
32
30
ATTACHMENT_PARALLEL_DOWNLOADS = 5
33
31
34
32
display_name = String (
@@ -84,19 +82,23 @@ class ShortAnswerAIEvalXBlock(AIEvalXBlock):
84
82
default = False ,
85
83
)
86
84
87
- messages = Dict (
88
- help = _ ("Dictionary with chat messages" ),
89
- scope = Scope .user_state ,
90
- default = {USER_KEY : [], LLM_KEY : []},
91
- )
92
-
93
85
attachment_urls = List (
94
86
display_name = _ ("Attachment URLs" ),
95
87
help = _ ("Attachments to include with the evaluation prompt" ),
96
88
scope = Scope .settings ,
97
89
resettable_editor = False ,
98
90
)
99
91
92
+ # XXX: Deprecated.
93
+ messages = Dict (
94
+ scope = Scope .user_state ,
95
+ )
96
+
97
+ sessions = List (
98
+ scope = Scope .user_state ,
99
+ default = [[]],
100
+ )
101
+
100
102
editable_fields = AIEvalXBlock .editable_fields + (
101
103
"question" ,
102
104
"evaluation_prompt" ,
@@ -106,6 +108,22 @@ class ShortAnswerAIEvalXBlock(AIEvalXBlock):
106
108
"attachment_urls" ,
107
109
)
108
110
111
+ def __init__ (self , * args , ** kwargs ):
112
+ super ().__init__ (* args , ** kwargs )
113
+ if self .messages :
114
+ for user_msg , assistant_msg in zip (self .messages ["USER" ],
115
+ self .messages ["LLM" ]):
116
+ self .sessions [- 1 ].append ({
117
+ "source" : "user" ,
118
+ "content" : user_msg or "." ,
119
+ })
120
+ self .sessions [- 1 ].append ({
121
+ "source" : "llm" ,
122
+ "content" : assistant_msg ,
123
+ })
124
+ self .messages = {}
125
+ self .save ()
126
+
109
127
def validate_field_data (self , validation , data ):
110
128
"""
111
129
Validate fields
@@ -155,7 +173,7 @@ def student_view(self, context=None):
155
173
156
174
js_data = {
157
175
"question" : self .question ,
158
- "messages" : self .messages ,
176
+ "messages" : self .sessions [ - 1 ] ,
159
177
"max_responses" : self .max_responses ,
160
178
"marked_html" : marked_html ,
161
179
}
@@ -224,14 +242,19 @@ def get_response(self, data, suffix=""): # pylint: disable=unused-argument
224
242
# add previous messages
225
243
# the first AI role is 'system' which defines the LLM's personnality and behavior.
226
244
# subsequent roles are 'assistant' and 'user'
227
- for user_msg , assistant_msg in zip (self .messages [self .USER_KEY ],
228
- self .messages [self .LLM_KEY ]):
229
- messages .append ({"content" : user_msg or "." , "role" : "user" })
230
- messages .append ({"content" : assistant_msg , "role" : "assistant" })
245
+ for message in self .sessions [- 1 ]:
246
+ if message ["source" ] == "user" :
247
+ role = "user"
248
+ else :
249
+ role = "assistant"
250
+ messages .append ({
251
+ "role" : role ,
252
+ "content" : message ["content" ] or "." ,
253
+ })
231
254
messages .append ({"role" : "user" , "content" : user_submission })
232
255
233
256
try :
234
- text = self .get_llm_response (messages , tag = current_tag )
257
+ response = self .get_llm_response (messages , tag = current_tag )
235
258
except Exception as e :
236
259
logger .error (
237
260
f"Failed while making LLM request using model { self .model } . Error: { e } " ,
@@ -241,10 +264,16 @@ def get_response(self, data, suffix=""): # pylint: disable=unused-argument
241
264
raise JsonHandlerError (500 , str (e )) from e
242
265
raise JsonHandlerError (500 , "A probem occurred. Please retry." ) from e
243
266
244
- if text :
245
- self .messages [self .USER_KEY ].append (user_submission )
246
- self .messages [self .LLM_KEY ].append (text )
247
- return {"response" : text }
267
+ if response :
268
+ self .sessions [- 1 ].append ({
269
+ "source" : "user" ,
270
+ "content" : user_submission ,
271
+ })
272
+ self .sessions [- 1 ].append ({
273
+ "source" : "llm" ,
274
+ "content" : response ,
275
+ })
276
+ return {"response" : response }
248
277
249
278
raise JsonHandlerError (500 , "A probem occurred. The LLM sent an empty response." )
250
279
@@ -255,8 +284,8 @@ def reset(self, data, suffix=""):
255
284
"""
256
285
if not self .allow_reset :
257
286
raise JsonHandlerError (403 , "Reset is disabled." )
258
- self .messages = {self .USER_KEY : [], self .LLM_KEY : []}
259
287
self .thread_map = {}
288
+ self .sessions .append ([])
260
289
return {}
261
290
262
291
@staticmethod
0 commit comments