Add openai integration
This commit is contained in:
		
							
								
								
									
										1
									
								
								.streamlit/secrets.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.streamlit/secrets.toml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
OPENAI_API_KEY = "sk-oaiwrapper-QpaWSUO8TLerRMJVB7RhT3BlbkFJGSjSx21NfCI6qozxXw2Z"
 | 
			
		||||
							
								
								
									
										29
									
								
								app.py
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								app.py
									
									
									
									
									
								
							@@ -1,23 +1,14 @@
 | 
			
		||||
import streamlit as st
 | 
			
		||||
import random
 | 
			
		||||
import time
 | 
			
		||||
from openai import OpenAI
 | 
			
		||||
 | 
			
		||||
st.title("ChatGPT-like clone")
 | 
			
		||||
 | 
			
		||||
# Streamed response emulator
 | 
			
		||||
def response_generator():
 | 
			
		||||
    response = random.choice(
 | 
			
		||||
        [
 | 
			
		||||
            "Hello there! How can I assist you today?",
 | 
			
		||||
            "Hi, human! Is there anything I can help you with?",
 | 
			
		||||
            "Do you need help?",
 | 
			
		||||
        ]
 | 
			
		||||
    )
 | 
			
		||||
    for word in response.split():
 | 
			
		||||
        yield word + " "
 | 
			
		||||
        time.sleep(0.05)
 | 
			
		||||
# Set OpenAI API key from Streamlit secrets
 | 
			
		||||
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
st.title("Simple chat")
 | 
			
		||||
# Set a default model
 | 
			
		||||
if "openai_model" not in st.session_state:
 | 
			
		||||
    st.session_state["openai_model"] = "gpt-4o"
 | 
			
		||||
 | 
			
		||||
# Initialize chat history
 | 
			
		||||
if "messages" not in st.session_state:
 | 
			
		||||
@@ -35,9 +26,3 @@ if prompt := st.chat_input("What is up?"):
 | 
			
		||||
    # Display user message in chat message container
 | 
			
		||||
    with st.chat_message("user"):
 | 
			
		||||
        st.markdown(prompt)
 | 
			
		||||
 | 
			
		||||
    # Display assistant response in chat message container
 | 
			
		||||
    with st.chat_message("assistant"):
 | 
			
		||||
        response = st.write_stream(response_generator())
 | 
			
		||||
    # Add assistant response to chat history
 | 
			
		||||
    st.session_state.messages.append({"role": "assistant", "content": response})
 | 
			
		||||
		Reference in New Issue
	
	Block a user