-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (41 loc) · 1.57 KB
/
main.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
# Import Libraries
import openai
import streamlit as st
from streamlit_chat import message
# Open API key
openai.api_key = st.secrets["OPEN_API_KEY"]
# Generating responses from the api
def generate_response(prompt):
completions = openai.Completion.create(
engine = "text-davinci-003",
prompt = prompt,
max_tokens = 1024,
n=1,
stop=None,
temperature=0.5
)
messages = completions.choices[0].text
return messages
# Creating the chatbot interfaces
st.title("Chatbot : Coding Craft + OpenAI ")
# Storing the input
if 'generated' not in st.session_state:
st.session_state['generated'] = []
if 'past' not in st.session_state:
st.session_state['past'] = []
# Creating a function that returns the user's input from a text input field
def get_text():
input_text = st.text_input("You : ", "Hello, Coders, how are you?", key = "input")
return input_text
# We will generate response using the 'generate response' function and store into variable called output
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)
# Finally we display the chat history
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')