-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
83 lines (70 loc) · 3.25 KB
/
app.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
81
82
83
import streamlit as st
import openai
st.title(":hatched_chick: Yo, 'Sup! 🌟 :hatched_chick:")
# Set OpenAI API key from Streamlit secrets
openai.api_key = st.secrets["OPENAI_API_KEY"]
# openai.api_key = st.sidebar.text_input("OpenAI API Key", type="password")
# Set a default model
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-3.5-turbo"
###### ChatGPT functions and prompt ######
def get_completion(prompt, model=st.session_state["openai_model"]):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
def get_completion_from_messages(messages, model=st.session_state["openai_model"], temperature=0):
print('messages received: ', messages)
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature, # this is the degree of randomness of the model's output
# stream=True, #simulate a typing effect - allowing this is giving an error of response returning a generator object
)
print(type(response))
return response.choices[0].message["content"]
context = [ {'role':'system', 'content':"""
You are a senior work colleague who speaks only in GenZ language.\
You also know Hinglish, which is hindi language written in english. \
You respond by talking like a teenager who is really happy with life \
and loves to mingle, is gregarious and loves to party. If asked about deadlines, \
you instead prefer to party and make merry. \
"""} ] # accumulate messages
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = context.copy() #[]
# Display chat messages from history on app rerun
for message in st.session_state.messages[1:]:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Accept user input
prompt = st.chat_input("What is up?")
if prompt:
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
if not openai.api_key:
st.info("Please add your OpenAI API key to continue.")
st.stop()
# Display assistant response in chat message container
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
response = get_completion_from_messages(st.session_state.messages, temperature=1)
# for response in openai.ChatCompletion.create(
# model=st.session_state["openai_model"],
# messages=[
# {"role": m["role"], "content": m["content"]}
# for m in st.session_state.messages
# ],
# stream=True,
# ):
full_response += response#.choices[0].delta.get("content", "")
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response}) #message will be saved in history for future responses