@@ -400,6 +400,43 @@ def parse_conversation(messages):
400400 initial_query = "\n " .join (conversation )
401401 return system_prompt , initial_query , optillm_approach
402402
403+ def tagged_conversation_to_messages (response_text ):
404+ """Convert a tagged conversation string or list of strings into a list of messages.
405+
406+ Args:
407+ response_text: Either a string containing "User:" and "Assistant:" tags,
408+ or a list of such strings.
409+
410+ Returns:
411+ If input is a string: A list of message dictionaries.
412+ If input is a list: A list of lists of message dictionaries.
413+ """
414+ def process_single_response (text ):
415+ messages = []
416+ # Split on "User:" or "Assistant:" while keeping the delimiter
417+ parts = re .split (r'(?=(User:|Assistant:))' , text .strip ())
418+ # Remove empty strings
419+ parts = [p for p in parts if p .strip ()]
420+
421+ for part in parts :
422+ part = part .strip ()
423+ if part .startswith ('User:' ):
424+ messages .append ({
425+ 'role' : 'user' ,
426+ 'content' : part [5 :].strip ()
427+ })
428+ elif part .startswith ('Assistant:' ):
429+ messages .append ({
430+ 'role' : 'assistant' ,
431+ 'content' : part [10 :].strip ()
432+ })
433+ return messages
434+
435+ if isinstance (response_text , list ):
436+ return [process_single_response (text ) for text in response_text ]
437+ else :
438+ return process_single_response (response_text )
439+
403440def extract_optillm_approach (content ):
404441 match = re .search (r'<optillm_approach>(.*?)</optillm_approach>' , content )
405442 if match :
@@ -500,6 +537,15 @@ def proxy():
500537 logger .error (f"Error processing request: { str (e )} " )
501538 return jsonify ({"error" : str (e )}), 500
502539
540+ # Convert tagged conversation to messages format if needed
541+ if isinstance (response , list ):
542+ response = [msg [- 1 ]['content' ] if isinstance (msg , list ) and msg else msg
543+ for msg in tagged_conversation_to_messages (response )]
544+ else :
545+ messages = tagged_conversation_to_messages (response )
546+ if messages : # Only take the last message if we have any
547+ response = messages [- 1 ]['content' ]
548+
503549 if stream :
504550 return Response (generate_streaming_response (response , model ), content_type = 'text/event-stream' )
505551 else :
0 commit comments