File size: 1,177 Bytes
a1bc6ed
b8bf724
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import streamlit as st
import openai

# Load your OpenAI API key from an environment variable
openai.api_key = os.getenv("OPENAI_API_KEY")

def get_openai_response(user_input):
    """
    Sends user input to OpenAI's Chat API and returns the model's response.
    """
    try:
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",  # Use the model suited for chat applications
            messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": user_input},
            ]
        )
        # Extract the text from the last response in the chat
        return response.choices[0].message['content'].strip() if response.choices else "No response from the model."
    except Exception as e:
        return f"An error occurred: {str(e)}"

# Streamlit app layout
st.title("Your Advanced Streamlit Chatbot")
user_input = st.text_input("What would you like to ask?")
if st.button("Submit"):
    chatbot_response = get_openai_response(user_input) if user_input else "Please enter a question or message to get a response."
    st.write(f"Chatbot: {chatbot_response}")