What is the python script to use for deploying secrets like open ai api keys in my script so the key doesn’t get disabled?
And please don’t just point me to another link, please post the script here thanks.
What is the python script to use for deploying secrets like open ai api keys in my script so the key doesn’t get disabled?
And please don’t just point me to another link, please post the script here thanks.
Hi @amritg,
Thanks for sharing this question! One question for you, are you asking about secrets management for a deployed app on Streamlit Community Cloud?
If yes, then we do have a very detailed blog + docs on how to accomplish this. It’s good practice to make sure your keys are secure.
Here’s an implementation by one of our community members: Struggling With Setting OpenAI API Using Streamlit Secrets - #5 by nhtkid
Example on how to save you API key in the secrets file under .streamlit/secrets.toml
OPENAI_API_KEY="sk-proj-xxxxxxxxxxxxxxxxxxx"
Example script that shows how to read in the API key:
import streamlit as st
from openai import OpenAI
# Access the OpenAI API key from the secrets
api_key = st.secrets["OPENAI_API_KEY"]
# Set up the OpenAI API client
client = OpenAI(api_key=api_key)
def generate_completion(prompt):
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": [
{
"type": "text",
"text": "You are an helpful assistant\n"
}
]
},
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
}
]
},
],
max_tokens=100
)
return response.choices[0].message.content
# A simple Streamlit UI
st.title("OpenAI GPT-3.5 Turbo Demo")
prompt = st.text_input("Enter a prompt:")
if st.button("Generate"):
if prompt:
completion = generate_completion(prompt)
st.markdown(completion)
else:
st.write("Please enter a prompt.")
When you’re ready to deploy the app, follow this detailed guide (with images) on how to add the keys to a deployed app.
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.