-
Notifications
You must be signed in to change notification settings - Fork 12
/
app_streamlit.py
36 lines (35 loc) · 939 Bytes
/
app_streamlit.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
import openai
import os
import time
import streamlit as st
openai.api_key = st.secrets["OPENAI_API_KEY"]
#INPUT INTERFACE & SUBMIT BUTTON
st.title('Stream LLM responses')
prompt = st.text_input(label="Prompt: ")
submit_button_exist = st.button('Submit')
#CALL TO API
start_time = time.time()
max_response_length = 200
delay_time = 0.01
response = openai.ChatCompletion.create(
# CHATPG GPT API REQQUEST
model='gpt-3.5-turbo',
messages=[
{'role': 'user', 'content': f'{prompt}'}
],
max_tokens=max_response_length,
temperature=0,
stream=True, # this time, we set stream=True
)
#PRINTING OUTPUT
answer=''
if submit_button_exist is True:
c = st.empty()
for event in response:
c.write(answer)
event_time = time.time() - start_time
event_text = event['choices'][0]['delta']
answer += event_text.get('content', '')
time.sleep(delay_time)
else:
st.write("")