File size: 1,072 Bytes
94b50d3
033d0a2
94b50d3
033d0a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 streamlit as st
from openai import OpenAI  # Import the client class

st.title("Streamlit OpenAI GPT‑4‑Turbo Demo")

# Ask user for their API key (masked input)
user_api_key = st.text_input("Enter your OpenAI API key", type="password")

# Ask user for a prompt
user_prompt = st.text_input("Enter your prompt:")

if user_api_key:
    # Instantiate the OpenAI client with the provided API key
    client = OpenAI(api_key=user_api_key)

    if user_prompt and st.button("Get Response"):
        try:
            # Use the new API call via the instantiated client
            response = client.chat.completions.create(
                model="gpt-4-turbo",  # Using the newer GPT‑4‑Turbo model
                messages=[{"role": "user", "content": user_prompt}],
                max_tokens=100
            )
            st.write("OpenAI response:")
            st.write(response.choices[0].message.content)
        except Exception as e:
            st.error(f"An error occurred: {e}")
else:
    st.info("Please enter your OpenAI API key above to get started.")