Created
July 7, 2023 18:06
-
-
Save EnkrateiaLucca/8de12686b63d4c54f7f9227402558538 to your computer and use it in GitHub Desktop.
ChatGPT like app in 38 lines of code with streamlit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openai | |
import streamlit as st | |
import os | |
st.title("ChatGPT Like APP") | |
openai.api_key = os.environ["OPENAI_API_KEY"] | |
#if "openai_model" not in st.session_state: | |
st.session_state["openai_model"] = st.sidebar.selectbox("Select your openai model!",["gpt-3.5-turbo", "gpt-4"]) | |
if "messages" not in st.session_state: | |
st.session_state.messages = [] | |
for message in st.session_state.messages: | |
with st.chat_message(message["role"]): | |
st.markdown(message["content"]) | |
if prompt := st.chat_input("What is up?"): | |
st.session_state.messages.append({"role": "user", "content": prompt}) | |
with st.chat_message("user"): | |
st.markdown(prompt) | |
with st.chat_message("assistant"): | |
message_placeholder = st.empty() | |
full_response = "" | |
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}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment