1515
1616import re
1717import textwrap
18- import warnings
1918from typing import List
2019
2120from nemoguardrails .actions .llm .utils import (
@@ -386,38 +385,6 @@ def indent(text: str, n_spaces: int) -> str:
386385 return textwrap .indent (text , " " * n_spaces )
387386
388387
389- def user_assistant_sequence_nemollm (events : List [dict ]) -> str :
390- """Filter that turns an array of events into a sequence of user/assistant messages.
391-
392- The output will look like:
393- ```
394- <extra_id_1>User
395- hi
396- <extra_id_1>Assistant
397- Hello there!
398- <extra_id_1>User
399- What can you do?
400- <extra_id_1>Assistant
401- I can help with many things.
402- ```
403- """
404-
405- warnings .warn (
406- "user_assistant_sequence_nemollm is deprecated and will be removed in a future release." ,
407- DeprecationWarning ,
408- stacklevel = 2 ,
409- )
410- history_items = []
411- for event in events :
412- if event ["type" ] == "UserMessage" :
413- # Convert text to string regardless of type (handles both text and multimodal)
414- history_items .append ("<extra_id_1>User\n " + str (event ["text" ]))
415- elif event ["type" ] == "StartUtteranceBotAction" :
416- history_items .append ("<extra_id_1>Assistant\n " + event ["script" ])
417-
418- return "\n " .join (history_items )
419-
420-
421388def _previous_line (lines : List [str ], i : int ):
422389 """Returns the previous lines, skipping comments."""
423390
@@ -427,61 +394,6 @@ def _previous_line(lines: List[str], i: int):
427394 return lines [i ]
428395
429396
430- def to_messages_nemollm (colang_history : str ) -> str :
431- """Filter that given a history in colang format, returns a messages string
432- in the chat format used by NeMo LLM models."""
433-
434- warnings .warn (
435- "to_messages_nemollm is deprecated and will be removed in a future release." ,
436- DeprecationWarning ,
437- stacklevel = 2 ,
438- )
439- messages = []
440-
441- # For now, we use a simple heuristic. The line `user "xxx"` gets translated to
442- # a message from the user, and the rest gets translated to messages from the assistant.
443- lines = colang_history .split ("\n " )
444-
445- bot_lines = []
446- for i , line in enumerate (lines ):
447- if line .startswith ('user "' ):
448- # If we have bot lines in the buffer, we first add a bot message.
449- if bot_lines :
450- messages .append ({"type" : "assistant" , "content" : "\n " .join (bot_lines )})
451- bot_lines = []
452-
453- messages .append ({"type" : "user" , "content" : line [6 :- 1 ]})
454-
455- elif line .strip () == "" :
456- # On empty lines, we also reset the bot buffer.
457- if bot_lines :
458- messages .append ({"type" : "assistant" , "content" : "\n " .join (bot_lines )})
459- bot_lines = []
460- else :
461- if i > 0 and _previous_line (lines , i ).startswith ('user "' ):
462- if not line .strip ().startswith ("#" ):
463- line = "User intent: " + line .strip ()
464- elif line .startswith ("user " ):
465- line = "User intent: " + line [5 :].strip ()
466- elif line .startswith ("bot " ):
467- line = "Bot intent: " + line [4 :].strip ()
468- elif line .startswith (' "' ):
469- line = "Bot message: " + line [2 :].strip ()
470- bot_lines .append (line )
471-
472- # Check if there is a last message from the bot.
473- if bot_lines :
474- messages .append ({"type" : "bot" , "content" : "\n " .join (bot_lines )})
475-
476- messages_string = ""
477- for m in messages :
478- if m ["type" ] == "assistant" or m ["type" ] == "bot" :
479- messages_string += "<extra_id_1>Assistant\n " + m ["content" ] + "\n "
480- elif m ["type" ] == "user" :
481- messages_string += "<extra_id_1>User\n " + m ["content" ] + "\n "
482- return messages_string
483-
484-
485397def remove_trailing_new_line (s : str ):
486398 if s .endswith ("\n " ):
487399 s = s [:- 1 ]
0 commit comments