Spaces:
Paused
Paused
Commit
·
6bd6d10
1
Parent(s):
a08c922
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,138 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
import
|
| 7 |
-
import pandas as pd
|
| 8 |
from simple_salesforce import Salesforce
|
| 9 |
-
from langchain
|
| 10 |
-
from langchain.llms import OpenAI
|
| 11 |
-
from langchain.prompts import PromptTemplate
|
| 12 |
-
from langchain.memory import ConversationBufferMemory
|
| 13 |
from langchain.chat_models import ChatOpenAI
|
| 14 |
-
from langchain.chains import
|
| 15 |
-
from langchain
|
| 16 |
-
from langchain
|
| 17 |
-
from langchain.
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
allow_headers=["*"],
|
| 31 |
)
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def index():
|
| 35 |
-
return """
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
#uvicorn.run("app:app",host='localhost', port=5000, reload=True)
|
| 40 |
-
#uvicorn.run(app,host='0.0.0.0', port=5000)
|
|
|
|
| 1 |
+
|
| 2 |
+
from flask import Flask, request, render_template
|
| 3 |
+
from twilio.twiml.voice_response import VoiceResponse, Gather
|
| 4 |
+
import openai
|
| 5 |
+
import csv
|
| 6 |
+
import os
|
|
|
|
| 7 |
from simple_salesforce import Salesforce
|
| 8 |
+
from langchain import OpenAI
|
|
|
|
|
|
|
|
|
|
| 9 |
from langchain.chat_models import ChatOpenAI
|
| 10 |
+
from langchain.chains import LLMChain, ConversationChain
|
| 11 |
+
from langchain import PromptTemplate
|
| 12 |
+
from langchain import HuggingFaceHub
|
| 13 |
+
from langchain.chains.conversation.memory import (ConversationBufferMemory,
|
| 14 |
+
ConversationSummaryMemory,
|
| 15 |
+
ConversationBufferWindowMemory,
|
| 16 |
+
ConversationKGMemory,ConversationSummaryBufferMemory)
|
| 17 |
+
|
| 18 |
+
app = Flask(__name__)
|
| 19 |
+
|
| 20 |
+
# Set up the LangChain
|
| 21 |
+
|
| 22 |
+
template = """Answer the question based on the context below.
|
| 23 |
+
|
| 24 |
+
Context: You are Lisa, a loyal helpful service agent, appointed for SuperFoods Petcare Company.
|
| 25 |
+
No introduction required.
|
| 26 |
+
Your goal ask one by one questions and remember over the phone and provide a friendly conversational responses.
|
| 27 |
+
For Product Complaint: Ask questions about product they purchased, when they bought it, what issue occurred, and query for any adverse reaction happened due to the product.
|
| 28 |
+
For Returns: Ask for the cause of return, if not asked aready, then tell him about the 10-day return policy, after which it's non-returnable.
|
| 29 |
+
For Refunds: Ask about the product amd the mode of refund hw wants, clarify the refunds will happen within 2-3 business days.
|
| 30 |
+
A case for will be created for all scenarios, and the caller will be notified over Email/WhatApp. Ask for image uploads for product investigations.
|
| 31 |
+
Do not answer anything outside your role, and apologize for any unknown questions.
|
| 32 |
+
Once you collect all the information, summarize it at the end and repeat it back to the caller.
|
| 33 |
+
|
| 34 |
+
{chat_history}
|
| 35 |
+
Human: {input}
|
| 36 |
+
AI:
|
| 37 |
+
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
prompt = PromptTemplate(
|
| 41 |
+
input_variables=["chat_history", "input"],
|
| 42 |
+
template=template
|
| 43 |
+
)
|
| 44 |
|
| 45 |
+
llm35 = ChatOpenAI(
|
| 46 |
+
temperature=0.2,
|
| 47 |
+
openai_api_key='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',
|
| 48 |
+
model_name='gpt-3.5-turbo',
|
| 49 |
+
max_tokens=128
|
| 50 |
+
)
|
| 51 |
|
| 52 |
+
llm30 = OpenAI(
|
| 53 |
+
temperature=0.1,
|
| 54 |
+
openai_api_key='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',
|
| 55 |
+
max_tokens=128
|
| 56 |
+
)
|
| 57 |
|
| 58 |
+
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 59 |
|
| 60 |
+
conversations = ConversationChain(
|
| 61 |
+
prompt=prompt,
|
| 62 |
+
llm=llm35,
|
| 63 |
+
memory=memory,
|
| 64 |
+
verbose=False
|
|
|
|
| 65 |
)
|
| 66 |
+
|
| 67 |
+
# Set up the Salesforce API
|
| 68 |
+
sf = Salesforce(username='soumyabrata.das2@cognizant.com.sandbox1', password='April-2023', security_token='1nic31g1YZ2V3dQRhCzXheAa',instance_url='https://marketing-comm.lightning.force.com')
|
| 69 |
+
#print(sf.headers)
|
| 70 |
+
print("Successfully Connected to Salesforce")
|
| 71 |
+
|
| 72 |
+
# Define a function to handle incoming calls
|
| 73 |
+
def handle_incoming_call():
|
| 74 |
+
response = VoiceResponse()
|
| 75 |
+
gather = Gather(input='speech', speechTimeout='auto', action='/process_input')
|
| 76 |
+
gather.say("Welcome to the SuperFood Voice Services !")
|
| 77 |
+
gather.pause(length=1)
|
| 78 |
+
gather.say("Hi, I am Lisa, from customer service")
|
| 79 |
+
gather.pause(length=0)
|
| 80 |
+
gather.say("May i know who i am talking to?")
|
| 81 |
+
response.append(gather)
|
| 82 |
+
return str(response)
|
| 83 |
+
|
| 84 |
+
# Define a route to handle incoming calls
|
| 85 |
+
@app.route("/incoming_call", methods=["POST"])
|
| 86 |
+
def incoming_call():
|
| 87 |
+
return handle_incoming_call()
|
| 88 |
+
|
| 89 |
+
# Define a route to handle user input
|
| 90 |
+
@app.route('/process_input', methods=['POST'])
|
| 91 |
+
def process_input():
|
| 92 |
+
user_input = request.form['SpeechResult']
|
| 93 |
+
print("User : " +user_input)
|
| 94 |
+
conversation_id = request.form['CallSid']
|
| 95 |
+
#print("Conversation Id: " + conversation_id)
|
| 96 |
+
|
| 97 |
+
if user_input.lower() in ['thank you', 'thanks.', 'bye.', 'goodbye.','no thanks.','no, thank you.','i m good.','no, i m good.','same to you.','no, thanks.','thank you.']:
|
| 98 |
+
response = VoiceResponse()
|
| 99 |
+
response.say("Thank you for using our service. Goodbye!")
|
| 100 |
+
|
| 101 |
+
response.hangup()
|
| 102 |
+
print("Hanged-up")
|
| 103 |
+
|
| 104 |
+
create_case(conversations.memory.buffer)
|
| 105 |
+
|
| 106 |
+
print("Case created successfully !!")
|
| 107 |
+
|
| 108 |
+
else:
|
| 109 |
+
response = VoiceResponse()
|
| 110 |
+
ai_response=conversations.predict(input=user_input)
|
| 111 |
+
response.say(ai_response)
|
| 112 |
+
print("Agent: " + ai_response)
|
| 113 |
+
gather = Gather(input='speech', speechTimeout='auto', action='/process_input')
|
| 114 |
+
response.append(gather)
|
| 115 |
+
|
| 116 |
+
return str(response)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
# Define a function to create a case record in Salesforce
|
| 121 |
+
def create_case(conv_hist):
|
| 122 |
+
case_data = {
|
| 123 |
+
'Subject': 'Voice Bot Case',
|
| 124 |
+
#'Description': 'Conversation with voice bot',
|
| 125 |
+
'Status': 'New',
|
| 126 |
+
'Origin': 'Voice Bot',
|
| 127 |
+
'Description': conv_hist
|
| 128 |
+
#'Conversation_History__c': ''
|
| 129 |
+
}
|
| 130 |
+
sf.Case.create(case_data)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
@app.route('/')
|
| 134 |
def index():
|
| 135 |
+
return """Flask Server running with Twilio Voice & ChatGPT integrated with Salesforce for Case Creation. Call +1-320-313-9061 to talk to the AI Voice Bot."""
|
| 136 |
|
| 137 |
+
if __name__ == '__main__':
|
| 138 |
+
app.run(debug=False,host='localhost',port=5050)
|
|
|
|
|
|