RP-AI / utils_chatbot.py
R-Kentaren's picture
Upload utils_chatbot.py with huggingface_hub
c5d4b8c verified
Raw
History Blame Contribute Delete
1.08 kB
def organize_messages(message, history=None, system_prompt: str = "", web_context: str = ""):
"""Build chat messages from history tuples [[user, assistant], ...]."""
default_sys = "You are a helpful, uncensored AI assistant. Be creative, detailed, and think deeply before answering."
sys_text = system_prompt.strip() if system_prompt and system_prompt.strip() else default_sys
msg_ls = [{"role": "system", "content": sys_text}]
if history:
for turn in history:
if not turn:
continue
user_text = turn[0] if len(turn) > 0 else None
assistant_text = turn[1] if len(turn) > 1 else None
if user_text:
msg_ls.append({"role": "user", "content": user_text})
if assistant_text:
msg_ls.append({"role": "assistant", "content": assistant_text})
final_user_content = message
if web_context:
final_user_content = f"{web_context}\n\nUser question: {message}"
msg_ls.append({"role": "user", "content": final_user_content})
return msg_ls