-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatgpt_ai_streaming_blogs.py
44 lines (36 loc) · 1.61 KB
/
chatgpt_ai_streaming_blogs.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
import streamlit_pills
import streamlit
import openai
import streamlit as st
from streamlit_pills import pills
# Get API key
openai.api_key = "Your Open AI API key"
st.subheader("AI Assistant : Streamlit + OpenAI: `stream` *argument*")
selected = pills("", ["NO Streaming", "Streaming"], ["🎈", "🌈"])
user_input = st.text_input("You: ",placeholder = "Ask me anything ...", key="input")
if st.button("Submit", type="primary"):
st.markdown("----")
res_box = st.empty()
if selected == "Streaming":
report = []
for resp in openai.Completion.create(model='text-davinci-003',
prompt=user_input,
max_tokens=120,
temperature = 0.5,
stream = True):
# join method to concatenate the elements of the list
# into a single string,
# then strip out any empty strings
report.append(resp.choices[0].text)
result = "".join(report).strip()
result = result.replace("\n", "")
res_box.markdown(f'*{result}*')
else:
completions = openai.Completion.create(model='text-davinci-003',
prompt=user_input,
max_tokens=120,
temperature = 0.5,
stream = False)
result = completions.choices[0].text
res_box.write(result)
st.markdown("----")