HI @mathcatsand how can I do it in the context of streamlit_chat
. Here is the code:
import openai
import streamlit as st
from streamlit_chat import message
openai.api_key='API_KEY'
def generate_response(prompt):
completion=openai.Completion.create(
engine='text-davinci-003',
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.6,
)
message=completion.choices[0].text
return message
st.title("ChatGPT3.5 BOT")
if 'generated' not in st.session_state:
st.session_state['generated'] = []
if 'past' not in st.session_state:
st.session_state['past'] = []
def inputchange():
inp={
"inputs": {
"past_user_inputs": st.session_state.past,
"generated_responses": st.session_state.generated,
"text": st.session_state.input,
},
}
output=generate_response(inp['inputs']['text'])
# append user_input and output to state
st.session_state['past'].append(st.session_state.input)
st.session_state['generated'].append(output)
# If responses have been generated by the model
if st.session_state['generated']:
# Reverse iteration through the list
for i in range(len(st.session_state['generated']) - 1, -1, -1):
# message from streamlit_chat
message(st.session_state['past'][::-1][i], is_user=True, key=str(i) + '_user', )
message(st.session_state['generated'][::-1][i], key=str(i),avatar_style="adventurer",seed=123,)
user_input = st.text_input("Input Message: ", "", key="input", on_change=inputchange)