Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aiproductguy committed Nov 10, 2024
1 parent b09113c commit 8604365
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions notebooks/streamlit-app-lightrag.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,31 @@ def get_api_key():
"""Securely retrieve OpenAI API key."""
# Check environment variable first
env_key = os.getenv("OPENAI_API_KEY")
if env_key:
if env_key and env_key.startswith("sk-"):
return env_key
elif env_key:
st.error("❌ Invalid OpenAI API key format in environment variable. Key should start with 'sk-'")
return None

# Then try secrets if environment variable not found
try:
secrets_exist = hasattr(st, "secrets") and isinstance(st.secrets, dict)
if secrets_exist and "OPENAI_API_KEY" in st.secrets:
os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_API_KEY"]
return os.environ["OPENAI_API_KEY"]
except Exception:
pass
if secrets_exist:
if "OPENAI_API_KEY" in st.secrets:
secret_key = st.secrets["OPENAI_API_KEY"]
if secret_key.startswith("sk-"):
os.environ["OPENAI_API_KEY"] = secret_key
return secret_key
else:
st.error("❌ Invalid OpenAI API key format in Streamlit secrets. Key should start with 'sk-'")
else:
st.error("❌ OPENAI_API_KEY not found in Streamlit secrets")
else:
st.error("❌ Streamlit secrets not configured")
except Exception as e:
st.error(f"❌ Error accessing Streamlit secrets: {str(e)}")

# If no key found, show input form
# If no valid key found
return None

def init_rag_secure(api_key: str):
Expand Down

0 comments on commit 8604365

Please sign in to comment.