@@ -144,7 +144,9 @@ def _create_response(self, data: Any, model: str) -> MockLLMResponse:
144
144
if isinstance (data , str ):
145
145
return MockLLMResponse (data , model = model )
146
146
elif isinstance (data , dict ):
147
- content = data .get ("content" , str (data ))
147
+ # For extract responses, convert dict to JSON string for content
148
+ import json
149
+ content = json .dumps (data )
148
150
return MockLLMResponse (content , data = data , model = model )
149
151
else :
150
152
return MockLLMResponse (str (data ), data = data , model = model )
@@ -247,4 +249,60 @@ def get_usage_stats(self) -> Dict[str, int]:
247
249
"total_prompt_tokens" : total_prompt_tokens ,
248
250
"total_completion_tokens" : total_completion_tokens ,
249
251
"total_tokens" : total_prompt_tokens + total_completion_tokens
250
- }
252
+ }
253
+
254
+ def create_response (
255
+ self ,
256
+ * ,
257
+ messages : list [dict [str , str ]],
258
+ model : Optional [str ] = None ,
259
+ function_name : Optional [str ] = None ,
260
+ ** kwargs
261
+ ) -> MockLLMResponse :
262
+ """Create a response using the same interface as the real LLMClient"""
263
+ # Use function_name to determine response type if available
264
+ if function_name :
265
+ response_type = function_name .lower ()
266
+ else :
267
+ # Fall back to content-based detection
268
+ content = str (messages ).lower ()
269
+ response_type = self ._determine_response_type (content )
270
+
271
+ # Track the call
272
+ self .call_count += 1
273
+ self .last_messages = messages
274
+ self .last_model = model or self .default_model
275
+ self .last_kwargs = kwargs
276
+
277
+ # Store call in history
278
+ call_info = {
279
+ "messages" : messages ,
280
+ "model" : self .last_model ,
281
+ "kwargs" : kwargs ,
282
+ "function_name" : function_name ,
283
+ "timestamp" : asyncio .get_event_loop ().time ()
284
+ }
285
+ self .call_history .append (call_info )
286
+
287
+ # Simulate failure if configured
288
+ if self .should_fail :
289
+ raise Exception (self .failure_message )
290
+
291
+ # Check for custom responses first
292
+ if response_type in self .custom_responses :
293
+ response_data = self .custom_responses [response_type ]
294
+ if callable (response_data ):
295
+ response_data = response_data (messages , ** kwargs )
296
+ return self ._create_response (response_data , model = self .last_model )
297
+
298
+ # Use default response mapping
299
+ response_generator = self .response_mapping .get (response_type , self ._default_response )
300
+ response_data = response_generator (messages , ** kwargs )
301
+
302
+ response = self ._create_response (response_data , model = self .last_model )
303
+
304
+ # Call metrics callback if set
305
+ if self .metrics_callback :
306
+ self .metrics_callback (response , 100 , response_type ) # 100ms mock inference time
307
+
308
+ return response
0 commit comments