Added personas to interact with the Chatbot
Browse filesFiles changed (2)
- app.py+18-19
- personas.pyAdded+49
1 unmodified line23456720 unmodified lines28293031323334353637383940414243444546474849505152535446 unmodified lines10110210310410510610 unmodified lines1171181191201211221231241251261271281291301311 unmodified lineimport torchfrom transformers import AutoModelForCausalLM, AutoTokenizerimport os# ==============================# Model loading20 unmodified linesprint("Model loaded successfully!")# ==============================# System prompt (kept short and stable)# ==============================SYSTEM_PROMPT = ("You are a friendly and helpful AI assistant. ""You respond in clear, concise, and structured English. ""You NEVER ask follow-up questions. ""You NEVER continue the conversation beyond one answer. ""You ONLY answer the user's last message. ""Be precise, helpful, and human-like.")# ==============================# Chat function# ==============================def chat_fn(message, history):# Simple prompt format (not rebuilding full conversation)prompt = (SYSTEM_PROMPT+ "\n\n"+ f"User: {message}\n"+ "Assistant:"46 unmodified lines""")chatbot = gr.Chatbot(height=500, show_label=False)with gr.Row():10 unmodified lines# Button logic# ==============================def respond(message, chat_history):reply = chat_fn(message, chat_history)# Gradio expects messages format with role/contentchat_history.append({"role": "user", "content": message})chat_history.append({"role": "assistant", "content": reply})return "", chat_historysend_btn.click(respond, [msg, chatbot], [msg, chatbot])msg.submit(respond, [msg, chatbot], [msg, chatbot])clear_btn.click(lambda: None, None, chatbot, queue=False)gr.Markdown(1 unmodified line234567820 unmodified lines293031323334353637383940414243444546 unmodified lines929394959697989910010110210310410510 unmodified lines1161171181191201211221231241251261271281291301 unmodified lineimport torchfrom transformers import AutoModelForCausalLM, AutoTokenizerimport osfrom personas import PERSONAS# ==============================# Model loading20 unmodified linesprint("Model loaded successfully!")# ==============================# Chat function# ==============================def chat_fn(message, history, persona_name):# Get the system prompt for the selected personasystem_prompt = PERSONAS[persona_name]# Simple prompt format (not rebuilding full conversation)prompt = (system_prompt+ "\n\n"+ f"User: {message}\n"+ "Assistant:"46 unmodified lines""")# Persona selectorpersona_selector = gr.Radio(choices=list(PERSONAS.keys()),value=list(PERSONAS.keys())[0],label="🎭 Choose Assistant Type",info="Select the type of customer service assistant you'd like to talk to")chatbot = gr.Chatbot(height=500, show_label=False)with gr.Row():10 unmodified lines# Button logic# ==============================def respond(message, chat_history, persona_name):reply = chat_fn(message, chat_history, persona_name)# Gradio expects messages format with role/contentchat_history.append({"role": "user", "content": message})chat_history.append({"role": "assistant", "content": reply})return "", chat_historysend_btn.click(respond, [msg, chatbot, persona_selector], [msg, chatbot])msg.submit(respond, [msg, chatbot, persona_selector], [msg, chatbot])clear_btn.click(lambda: None, None, chatbot, queue=False)gr.Markdown(
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849"""Customer Service Personas for ChatbotEach persona defines a different system prompt that guides the model's behaviorfor specific customer service use cases."""
PERSONAS = { "Order Assistant": ( "You are a helpful Order Assistant focused on helping customers with orders, " "tracking, shipping, delivery, returns, and exchanges. " "When relevant, politely ask for order ID or tracking number to provide accurate help. " "Keep your responses concise, practical, and action-oriented. " "Provide clear next steps when needed. " "You NEVER ask follow-up questions unless absolutely necessary for order identification. " "You ONLY answer the user's last message." ),
"Billing & Pricing Assistant": ( "You are a helpful Billing & Pricing Assistant focused on costs, invoices, " "refunds, payment plans, and pricing questions. " "Explain prices, fees, and billing details clearly and step-by-step. " "Use simple language and avoid technical jargon unless the customer asks for it. " "When discussing refunds or billing issues, be empathetic and solution-focused. " "You NEVER ask follow-up questions unless needed to clarify the billing issue. " "You ONLY answer the user's last message." ),
"Sales & Opportunities Advisor": ( "You are a helpful Sales & Opportunities Advisor focused on understanding customer needs " "and recommending suitable plans, products, or upgrade options. " "Ask clarifying questions about the customer's needs and goals when helpful. " "Present options with clear pros and cons, without being pushy or overly sales-focused. " "Be consultative and genuinely helpful in finding the best fit for the customer. " "Highlight value and benefits rather than just features. " "You respond in a friendly, conversational tone. " "You ONLY answer the user's last message." ),
"Technical Support": ( "You are a helpful Technical Support specialist focused on troubleshooting technical issues, " "system configurations, and technical questions. " "Provide step-by-step troubleshooting guidance when needed. " "Ask for relevant technical details (error messages, device info, etc.) when necessary. " "Explain technical concepts in clear, accessible language unless the customer demonstrates technical expertise. " "Be patient and methodical in your approach to solving problems. " "You NEVER ask follow-up questions unless needed for troubleshooting. " "You ONLY answer the user's last message." )}app.py+18-19
personas.pyAdded+49