Spaces:
Runtime error
Runtime error
Ilke Ileri commited on
Commit ·
39bb917
1
Parent(s): 2311595
Improve conversation quality: use full history, increase tokens to 150, better sampling
Browse files97 unmodified lines98991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521535 unmodified lines15916016116216316416516616797 unmodified linesmessages = data.get("messages", [])# Extract user messages (ignore system messages from Vapi)user_messages = [msg for msg in messages if msg.get("role") == "user"]if not user_messages:prompt = data.get("prompt", "")else:# Get the last user messageprompt = user_messages[-1].get("content", "")if not prompt:return jsonify({"error": "No prompt provided"}), 400print(f"User prompt: {prompt}")# Sales-only keyword filtersales_keywords = ['sale', 'price', 'objection', 'close', 'deal', 'customer', 'prospect','lead', 'pitch', 'negotiate', 'client', 'business', 'product', 'service','wisemate', 'revenue', 'quota', 'conversion', 'crm', 'follow up', 'meeting','appointment', 'schedule', 'calendar', 'demo', 'call', 'consultation']# Check if question is sales-relatedprompt_lower = prompt.lower()is_sales_related = any(keyword in prompt_lower for keyword in sales_keywords)# If not sales-related, return redirect messageif not is_sales_related and len(prompt.split()) > 3: # Only filter if it's a real questionredirect_message = "I'm Daniel from Wisemate, and I specialize in sales and business topics. I can help you with sales techniques, handling objections, closing deals, and Wisemate's services. How can I assist you with your sales needs?"return jsonify({"id": "chatcmpl-redirect","object": "chat.completion","created": int(__import__('time').time()),"model": MODEL_NAME,"choices": [{"index": 0,"message": {"role": "assistant","content": redirect_message},"finish_reason": "stop"}],"usage": {"prompt_tokens": 0,"completion_tokens": 0,"total_tokens": 0}}), 200# Gemma formatında promptformatted_prompt = f"<start_of_turn>user\n{prompt}<end_of_turn>\n<start_of_turn>model\n"# Model yanıtı üretinputs = tokenizer(formatted_prompt, return_tensors="pt")5 unmodified linesprint(f"Generating response...")outputs = model.generate(**inputs,max_new_tokens=50, # Drastically reduced for real-time voice (was 100)temperature=0.3, # Lower for faster, more focused responsesdo_sample=False, # Greedy decoding for speedpad_token_id=tokenizer.pad_token_id,eos_token_id=tokenizer.eos_token_id)97 unmodified lines98991001011021031041051061071081091101111121131141151161171181191201211225 unmodified lines12812913013113213313413513613713897 unmodified linesmessages = data.get("messages", [])# Build conversation history in Gemma format# Only use user and assistant messages, skip system (Vapi's system prompt is just context)conversation_parts = []for msg in messages:role = msg.get("role")content = msg.get("content", "")if role == "user":conversation_parts.append(f"<start_of_turn>user\n{content}<end_of_turn>")elif role == "assistant":conversation_parts.append(f"<start_of_turn>model\n{content}<end_of_turn>")# Skip system messages - Vapi's system prompt is too long for context# Add the model turn startconversation_parts.append("<start_of_turn>model\n")formatted_prompt = "\n".join(conversation_parts)print(f"Formatted prompt length: {len(formatted_prompt)} chars")print(f"Last user message: {[m['content'] for m in messages if m.get('role')=='user'][-1] if messages else 'none'}")# Model yanıtı üretinputs = tokenizer(formatted_prompt, return_tensors="pt")5 unmodified linesprint(f"Generating response...")outputs = model.generate(**inputs,max_new_tokens=150, # Increased for complete appointment booking responsestemperature=0.7, # Higher for more natural conversationdo_sample=True, # Enable sampling for varietytop_p=0.9,repetition_penalty=1.1,pad_token_id=tokenizer.pad_token_id,eos_token_id=tokenizer.eos_token_id)
app.py+23-52