Streamlit finds the wrong openai_api_key

I created a test file to test loading the openai api key using dotenv:

from dotenv import load_dotenv
import os

load_dotenv()
print(os.environ["OPENAI_API_KEY"])

This code gets the api key that is in the .env file found in the same directory.

I use the same method in the following code to read in the api key

from dotenv import load_dotenv
import os

from llama_index.callbacks import LlamaDebugHandler, CallbackManager
from llama_index.chat_engine.types import ChatMode
from llama_index.llms import OpenAI

load_dotenv()
import streamlit as st
import pinecone
from llama_index import VectorStoreIndex, ServiceContext
from llama_index.vector_stores import PineconeVectorStore
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") 
print(OPENAI_API_KEY)
print("***Streamlit LlamaIndex Documentation Helper***")


OpenAI.api_key = os.environ["OPENAI_API_KEY"]
print(OpenAI.api_key)

@st.cache_resource(show_spinner=False)
def get_index() -> VectorStoreIndex:
    pinecone.init(
        api_key=os.environ["PINECONE_API_KEY"],
        environment=os.environ["PINECONE_ENVIRONMENT"],
    )
    pinecone_index = pinecone.Index(index_name="llamaindex-documentation-helper")
    vector_store = PineconeVectorStore(pinecone_index=pinecone_index)

    llama_debug = LlamaDebugHandler(print_trace_on_end=True)
    callback_manager = CallbackManager(handlers=[llama_debug])
    service_context = ServiceContext.from_defaults(callback_manager=callback_manager)

    return VectorStoreIndex.from_vector_store(
        vector_store=vector_store, service_context=service_context
    )


index = get_index()
if "chat_engine" not in st.session_state.keys():
    st.session_state.chat_engine = index.as_chat_engine(
        chat_mode=ChatMode.CONTEXT, verbose=True
    )


st.set_page_config(
    page_title="Chat with LlamaIndex docs, powered by LlamaIndex",
    page_icon="🦙",
    layout="centered",
    initial_sidebar_state="auto",
    menu_items=None,
)

st.title("Chat with LlamaIndex docs 💬🦙")

if "messages" not in st.session_state.keys():
    st.session_state.messages = [
        {
            "role": "assistant",
            "content": "Ask me a question about LlamaIndex's open source python library?",
        }
    ]


if prompt := st.text_input("Your question"):
    st.session_state.messages.append({"role": "user", "content": prompt})

for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.write(message["content"])

if st.session_state.messages[-1]["role"] != "assistant":
    with st.chat_message("assistant"):
        with st.spinner("Thinking..."):
            response = st.session_state.chat_engine.chat(message=prompt)
            st.write(response.response)
            message = {"role": "assistant", "content": response.response}
            st.session_state.messages.append(message)

```.

When I run the command: streamlit run main.py  the wrong openai api key is found.  I have been searching for it, but cannot find the key found in the error message anywhere.

I cleared the browser cache but I get the same key whenever I run streamlit.

Hey @Edward_Cheadle,

Thanks for sharing this question.

Just to clarify, you’re saying that os.environ["OPENAI_API_KEY"] and
OpenAI.api_key = os.environ["OPENAI_API_KEY"] are loading different API keys when run in the same folder?

The two programs load different OPENAI_API_KEY keys even though they are in the same directory with the same .env file.

I created another user and copied the two programs and .env files to the new user’s system. Once I copied the programs, and run the programs, they both pick up the same key and the code works fine.

When I run streamlit it seems to have a key either cached some place or it does some searching for a key differently than when I don’t run streamlit.

I cleared the browser’s cache and searched for the key found in the error message, but no luck. I have been playing with streamlit and openai for a couple of months so I could have some hidden files lying around, but so far I have not been able to find them, nor can I figure out how streamlit is searching for the key.

The first short program I run with $ python test.py and the print command prints the correct api key.

In the second program, I use the command: $ streamlit run main.py. I was experimenting with loading the keys, which is why there are two instances of the os.environ command. both print statememts print out the wrong key. The program works the same without the using either os.environ command.

I was trying to determine another issue.

Hi Caroline, I found the problem. I thought i had checked the directory /home/echeadle/.streamlit for the secrets.toml file. I looked again, found the file and deleted the key.

Earlier, before asking, I found Streamlit’s Secrets management documentation. Somehow, I missed the file that contained the secret I was having issues with.

Thanks for responding, It gave me incentive to check around again, and I stumbled on the file, I thought I had searched for.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.