@@ -27,9 +27,6 @@ class ShortAnswerAIEvalXBlock(AIEvalXBlock):
27
27
28
28
ATTACHMENT_PARALLEL_DOWNLOADS = 5
29
29
30
- USER_KEY = "USER"
31
- LLM_KEY = "LLM"
32
-
33
30
display_name = String (
34
31
display_name = _ ("Display Name" ),
35
32
help = _ ("Name of the component in the studio" ),
@@ -90,10 +87,14 @@ class ShortAnswerAIEvalXBlock(AIEvalXBlock):
90
87
resettable_editor = False ,
91
88
)
92
89
90
+ # XXX: Deprecated.
93
91
messages = Dict (
94
- help = _ ("Dictionary with chat messages" ),
95
92
scope = Scope .user_state ,
96
- default = {USER_KEY : [], LLM_KEY : []},
93
+ )
94
+
95
+ sessions = List (
96
+ scope = Scope .user_state ,
97
+ default = [[]],
97
98
)
98
99
99
100
editable_fields = AIEvalXBlock .editable_fields + (
@@ -105,6 +106,22 @@ class ShortAnswerAIEvalXBlock(AIEvalXBlock):
105
106
"attachment_urls" ,
106
107
)
107
108
109
+ def __init__ (self , * args , ** kwargs ):
110
+ super ().__init__ (* args , ** kwargs )
111
+ if self .messages :
112
+ for user_msg , assistant_msg in zip (self .messages ["USER" ],
113
+ self .messages ["LLM" ]):
114
+ self .sessions [- 1 ].append ({
115
+ "source" : "user" ,
116
+ "content" : user_msg or "." ,
117
+ })
118
+ self .sessions [- 1 ].append ({
119
+ "source" : "llm" ,
120
+ "content" : assistant_msg ,
121
+ })
122
+ self .messages = {}
123
+ self .save ()
124
+
108
125
def validate_field_data (self , validation , data ):
109
126
"""
110
127
Validate fields
@@ -154,7 +171,7 @@ def student_view(self, context=None):
154
171
155
172
js_data = {
156
173
"question" : self .question ,
157
- "messages" : self .messages ,
174
+ "messages" : self .sessions [ - 1 ] ,
158
175
"max_responses" : self .max_responses ,
159
176
"marked_html" : marked_html ,
160
177
}
@@ -202,10 +219,15 @@ def get_response(self, data, suffix=""): # pylint: disable=unused-argument
202
219
# add previous messages
203
220
# the first AI role is 'system' which defines the LLM's personnality and behavior.
204
221
# subsequent roles are 'assistant' and 'user'
205
- for user_msg , assistant_msg in zip (self .messages [self .USER_KEY ],
206
- self .messages [self .LLM_KEY ]):
207
- messages .append ({"content" : user_msg or "." , "role" : "user" })
208
- messages .append ({"content" : assistant_msg , "role" : "assistant" })
222
+ for message in self .sessions [- 1 ]:
223
+ if message ["source" ] == "user" :
224
+ role = "user"
225
+ else :
226
+ role = "assistant"
227
+ messages .append ({
228
+ "role" : role ,
229
+ "content" : message ["content" ] or "." ,
230
+ })
209
231
messages .append ({"role" : "user" , "content" : user_submission })
210
232
211
233
try :
@@ -218,8 +240,14 @@ def get_response(self, data, suffix=""): # pylint: disable=unused-argument
218
240
raise JsonHandlerError (500 , "A probem occured. Please retry." ) from e
219
241
220
242
if response :
221
- self .messages [self .USER_KEY ].append (user_submission )
222
- self .messages [self .LLM_KEY ].append (response )
243
+ self .sessions [- 1 ].append ({
244
+ "source" : "user" ,
245
+ "content" : user_submission ,
246
+ })
247
+ self .sessions [- 1 ].append ({
248
+ "source" : "llm" ,
249
+ "content" : response ,
250
+ })
223
251
return {"response" : response }
224
252
225
253
raise JsonHandlerError (500 , "A probem occured. The LLM sent an empty response." )
@@ -231,7 +259,7 @@ def reset(self, data, suffix=""):
231
259
"""
232
260
if not self .allow_reset :
233
261
raise JsonHandlerError (403 , "Reset is disabled." )
234
- self .messages = { self . USER_KEY : [], self . LLM_KEY : []}
262
+ self .sessions . append ([])
235
263
return {}
236
264
237
265
@staticmethod
0 commit comments