Find answers from the community

Updated 3 months ago

#RAGS Help Please - setting keys without

#RAGS Help Please - setting keys without using streamlit.
I want to deploy somewhere other than streamlit. The process for getting streamlit to simply use os.getenv for api keys instead of a .toml file (get's exposed) or streamlit hosting (no thanks) is driving me a bit crazy. Where, oh where should I configure this?

please really need help here.
- you are the expert online at the moment πŸ˜‰
r
N
L
23 comments
Hey @NickkNYC ,

Could not understand the issue. You are unable to add openai keys in streamlit?
deploying Rags (https://github.com/run-llama/rags) - I want to deploy to Azure App Services. I can deploy the app fine. Runs. The issue is the OpenAI and Metaphor keys. Streamlit is intent on having you a) deploy to the streamlit cloud b) use a .toml file.
a) - i want to host this with the rest of my stuff
b) .toml file get's exposed when deploying

In my Azure App Service instance I have environment variable set for both Metaphor and OpenAI, but streamlit is not using them.

I want to know how I can force using environment variables - if there is one place in the code where i can fetch the keys and then stuff them into st.secrets I would be happy, I think.

@ravitheja is this clearer?
Once you clone the rags repository, you can change the path to which keys are located here - https://github.com/run-llama/rags/blob/4bec27023950c078fe0d559e1720d757d0024c93/core/builder_config.py
This is for OpenAI key and similarly you can modify the path for metaphor keys as well and then deploy the app
that should work right?
# set OpenAI Key - use Streamlit secrets os.environ["OPENAI_API_KEY"] = st.secrets.openai_key are you saying I should do this? os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") my expectation is that I would need to set the st.secrets.openai_key to the correct key value which would be os.getenv("OPENAI_API_KEY")`
Okay in that case you can probably comment this line.
@ravitheja - which line?
os.environ["OPENAI_API_KEY"] = st.secrets.openai_key
and replace it with this?
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
No. If your OPENAI_API_KEY is already in environment variable you don't need this line
No love - this is running the code that Jerry dropped 18 hours ago.
Attachments
image.png
image.png
image.png
This is what it says in the docs - there seems to be some sort of hard dependency on the .streamlit/secrets.toml
Attachment
image.png
also note that .streamlit is in .gitignore
Attachment
image.png
@Logan M - would you be so kind as to give a look here
Since we recently added web search with metaphor, it's also using st.secrets to check for the metadata key. core/agent_builder/loader.py:L51

You'll either need to change this in the code or figure out how to make secrets.toml work πŸ˜…

If secrets.toml doesn't work for you, do a ctrl-f for st.secrets and remove it's usage
In the past, I've managed individual keys by adding a page/input for the user to input their keys, and storing that in st.session_state (not sure how in-depth your streamlit knowledge is these days lol)
ok thanks - streamlit is a dark art 😜
@Logan M ok - I think I understand where I need to dig in...
I am happy to use os.getenv("OPENAI_API_KEY") and os.getenv("METAPHOR_KEY") and stuff it into st.session_state -- the part I get stuck on is the relationship between if "metaphor_key" in st.secrets: and st.session_state - where/how do you add/set a st.secrets... I have been using Chainlit (https://docs.chainlit.io/get-started/overview) and at least that is a bit easier to comprehend - but at the end of the day - the collision of abstractions is problematic... and I personally think it hurts LlamaIndex when the UI frameworks interfere with the "LI way"
anywho... I guess the answer is here

import streamlit as st # Check if 'key' already exists in session_state # If not, then initialize it if 'key' not in st.session_state: st.session_state['key'] = 'value' # Session State also supports the attribute based syntax if 'key' not in st.session_state: st.session_state.key = 'value'
Yup, I was saying to just avoid using st.secrets altogether, looks like you found the secret sauce
Ah success - thanks for the assist @Logan M and @ravitheja - now I see I was overthinking it 😜 - will get a PR together tomorrow after some more testing
Add a reply
Sign up and join the conversation on Discord