Py.Cafe

maartenbreddels/

streamlit-openai-secrets

OpenAI and Secrets Demo Using Streamlit

DocsPricing
  • app.py
  • requirements.txt
  • secret-enter-value.webp
app.py
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
31
32
33
34
import streamlit as st
import pycafe

reason = """We need an OpenAI API key to generate text.

Go to [OpenAI](https://platform.openai.com/account/api-keys) to get one.

Or read [this](https://www.rebelmouse.com/openai-account-set-up) article for
more information.

Or read more [about secrets on PyCafe](/docs/secrets)
"""

api_key = pycafe.get_secret("OPENAI_API_KEY", reason=reason)

from openai import OpenAI
client = OpenAI(api_key=api_key)

question = "How cool would it be if we could run streamlit and dash in the browser?"

completion = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a concise and friendly assistant."},
        {
            "role": "user",
            "content": question,
        }
    ]
)
st.markdown(f"**{question}**")
with st.spinner():
    st.write(completion.choices[0].message.content)