see also:
<!DOCTYPE html>
<html>
<head>
<title>ChatBot</title>
</head>
<body>
<h1>AI Director ED</h1>
<form method="POST" action="/send_message">
<input type="text" name="human_input" placeholder="Enter your message">
<button type="submit"> Send </button>
</form>
<div id="response_message"></div> <!--this displays the response from ChatGPT -->
<script>
const form = document.querySelector('form');
const response_message = document.get_ElementById('response_message');
form.addEventListener('submit', function(e) {
e.preventDefault();
const formData = new FormData(form);
fetch('/send_message', {
method:POST,
body: FormData
}
};
.then(response => response.text() )
.then(data => {
responseMessage.innerHTML = data;
}
);
form.reset();
}
);
</script>
</body>
</html>
from langchain import OpenAI, LLMChain, PromptTemplate from langchain.memory import ConversationBufferWindowMemory #as we need to store history from dotenv import find_dotenv, load_dotenv #load your API_Keys for OpenAI and ElevenLabs which you stored in env.py import requests #for http requests to OpenAI from playsound import playsound #play and audio file import os load_dotenv(find_dotenv)
ELEVEN_LABS_API_KEY = os.getenv("ELEVEN_LABS_API_KEY") #this is the one you have stored in env.py file under that element name
def get_voice_message(message):
payload = {
"text" = message,
"model_id" = "eleven_monolingual_v1",
"voice_settings" = {
"stability" = 0,
"similarity_boost" = 0
}
}
headers = {
'accept': 'audio/mpeg',
'xi_api_key': ELEVEN_LABS_API_KEY,
'Content_Type': 'application/json'
}
response = requests.post('the URL from ElevenLabs API for your selected voicing', json=payload, headers=headers)
if response.status.code == 200 and response.content:
with open('audio.mp3', 'wb') as f:
f.write(response.content)
playsound('audio.mp3')
return response.content
# work out what context prompt you want to send to ChatGPT to set up a scenario for the converstion - see flowgpt.com for assistance
#define a function to respond to human input:
def get_response_from_AI(human_input):
template = '''
you are as a role of a manager of an emergency department in a hospital, now lets play with the following requirements:
1/ your name is Gary, you are 62 years old, you have worked in the hospital for 30 years and are now planning to retire soon as you like camping
2/ the emergency department is an extremely busy and stressful environment
3/ don't be cruel, don't be aggressive, don't be overly negative, don't be too boring, be enthusiastic and dynamic;
{history}
Employee: {human_input}
Gary:
'''
prompt = PromptTemplate(
input_variables=("history","human_input"),
template=template
)
chatgpt_chain = LLMChain(
llm=OpenAI(temperature=0.2),
prompt=prompt,
verbose=True,
memory=ConversationBufferWindowMemory(k=2)
)
output = chatgpt_chain.predict(human_input=human_input)
return output
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route("/")
def home():
return render_template("index.html")
@app.route('/send_message',methods=['POST'])
def send_message():
human_input = request.form('human_input')
message=get_response_from_AI(human_input)
get_voice_message(message) #get and play the text to speech audio
return message
if __name__ == "__main__":
app.run(debug=True)
#if all this python code in the cells above are saved to a file called app.py then it can be run via python app.py