-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot2.py
80 lines (61 loc) · 2.18 KB
/
chatbot2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# To run in terminal:
# streamlit run chatbot.py
# To close: control C
# right click in terminal window
# Example use of URL embedding
# https://brennanantone-testchatbot2-chatbot2-aqz2vq.streamlit.app/?username=Noshir&userid=789
# username is for display on the webpage
# userid is for storage of messages in a database
# Import the libraries
import openai
import streamlit as st
# import streamlit_chat
from streamlit_chat import message
openai.api_key = st.secrets['OPENAI_SECRET']
#openai.api_key = OPENAI_SECRET
def get_param(param_name):
# Get parameter from a url
query_params = st.experimental_get_query_params()
try:
return query_params[param_name][0]
except:
st.write('Parameter is missing')
return False
def generate_response(prompt):
# Get OpenAI response to prompt
completions = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
ai_message = completions.choices[0].text
return ai_message
# Creating the chatbot interface
username = get_param('username')
userid = get_param('userid') # Get from url
my_title = "Vero: An AI teammate for " + str(username)
st.title(my_title)
st.write('This version of Vero has been personalized with knowledge and speech patterns designed to assist you on ' +
'brainstorming and creative thinking tasks.')
# Storing the chat
if 'generated' not in st.session_state:
st.session_state['generated'] = []
if 'past' not in st.session_state:
st.session_state['past'] = []
def get_text():
# Gets text input by the user
input_text = st.text_input("You: ", "Hello Vero, are you ready to collaborate?", key="input")
return input_text
user_input = get_text()
if user_input:
output = generate_response(user_input)
# store the output
st.session_state.past.append(user_input)
st.session_state.generated.append(output)
if st.session_state['generated']:
for i in range(len(st.session_state['generated']) - 1, -1, -1):
message(st.session_state["generated"][i], key=str(i))
message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')