Skip to content

Commit b256f45

Browse files
committed
Refactor: Use st.secrets for all API keys instead of os.environ.get()
1 parent df96a19 commit b256f45

26 files changed

+962
-6893
lines changed

agents/rag/query_analyst_agent.py

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -293,30 +293,9 @@ def _fallback_analysis(self, query: str) -> Dict[str, Any]:
293293
}
294294

295295
def _get_gemini_api_key(self) -> Optional[str]:
296-
"""Get Gemini API key from environment or secrets."""
297-
import os
298-
from pathlib import Path
299-
300-
# Try environment variable first
301-
api_key = os.environ.get('GEMINI_API_KEY')
302-
if api_key:
303-
return api_key
304-
305-
# Try Streamlit secrets
306-
try:
307-
secrets_path = Path.home() / '.streamlit' / 'secrets.toml'
308-
if secrets_path.exists():
309-
with open(secrets_path, 'r') as f:
310-
for line in f:
311-
if line.startswith('GEMINI_API_KEY'):
312-
# Parse: GEMINI_API_KEY = "value" or GEMINI_API_KEY="value"
313-
api_key = line.split('=', 1)[1].strip().strip('"').strip("'")
314-
if api_key:
315-
return api_key
316-
except Exception as e:
317-
logger.warning(f"Could not read secrets.toml: {e}")
318-
319-
return None
296+
"""Get Gemini API key from Streamlit secrets."""
297+
from utils.config import get_gemini_api_key
298+
return get_gemini_api_key()
320299

321300
def validate_task(self, task: Dict[str, Any]) -> bool:
322301
"""Validate that task has required query field."""

agents/rag/writer_agent.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -386,27 +386,9 @@ def _determine_style(self, intent: str) -> str:
386386
return style_map.get(intent, 'technical')
387387

388388
def _get_gemini_api_key(self) -> Optional[str]:
389-
"""Get Gemini API key."""
390-
import os
391-
from pathlib import Path
392-
393-
api_key = os.environ.get('GEMINI_API_KEY')
394-
if api_key:
395-
return api_key
396-
397-
try:
398-
secrets_path = Path.home() / '.streamlit' / 'secrets.toml'
399-
if secrets_path.exists():
400-
with open(secrets_path, 'r') as f:
401-
for line in f:
402-
if line.startswith('GEMINI_API_KEY'):
403-
api_key = line.split('=', 1)[1].strip().strip('"').strip("'")
404-
if api_key:
405-
return api_key
406-
except Exception as e:
407-
logger.warning(f"Could not read secrets: {e}")
408-
409-
return None
389+
"""Get Gemini API key from Streamlit secrets."""
390+
from utils.config import get_gemini_api_key
391+
return get_gemini_api_key()
410392

411393
def validate_task(self, task: Dict[str, Any]) -> bool:
412394
"""Validate task has required fields."""

apps/rag_management_app.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,11 +1816,12 @@ def system_settings_page():
18161816

18171817
# LangSmith Tracing Status
18181818
st.markdown("### 🔍 LangSmith Tracing")
1819-
langsmith_enabled = os.environ.get('LANGCHAIN_TRACING_V2', 'false').lower() == 'true'
1819+
from utils.config import get_langchain_tracing_enabled, get_langchain_project
1820+
langsmith_enabled = get_langchain_tracing_enabled()
18201821

18211822
if langsmith_enabled:
18221823
st.success("✅ LangSmith Tracing Enabled")
1823-
st.info(f"**Project:** {os.environ.get('LANGCHAIN_PROJECT', 'default')}")
1824+
st.info(f"**Project:** {get_langchain_project()}")
18241825
st.markdown("🔗 View traces at: [https://smith.langchain.com/](https://smith.langchain.com/)")
18251826
else:
18261827
st.warning("⚠️ LangSmith Tracing Disabled")

0 commit comments

Comments
 (0)