Spaces:
Sleeping
Sleeping
Anna Zhou commited on
Commit ·
57d0b77
1
Parent(s): 45ddb8a
Sanitize LLM API keys and handle provider errors
Browse files- app/app.py +11 -7
- rag/pipeline.py +45 -14
app/app.py
CHANGED
|
@@ -7,7 +7,7 @@ from pathlib import Path
|
|
| 7 |
import streamlit as st
|
| 8 |
|
| 9 |
sys.path.insert(0, str(Path(__file__).parent.parent))
|
| 10 |
-
from rag.pipeline import run_pipeline
|
| 11 |
from ingest.geo_metadata import geographic_metadata, normalize_country
|
| 12 |
sys.path.insert(0, str(Path(__file__).parent))
|
| 13 |
from sheets_logger import log_query
|
|
@@ -569,12 +569,16 @@ with main_col:
|
|
| 569 |
chunks = []
|
| 570 |
else:
|
| 571 |
with st.spinner("Searching case studies and generating response..."):
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 578 |
|
| 579 |
# Save to history
|
| 580 |
st.session_state.history.insert(0, {
|
|
|
|
| 7 |
import streamlit as st
|
| 8 |
|
| 9 |
sys.path.insert(0, str(Path(__file__).parent.parent))
|
| 10 |
+
from rag.pipeline import RAGProviderError, run_pipeline
|
| 11 |
from ingest.geo_metadata import geographic_metadata, normalize_country
|
| 12 |
sys.path.insert(0, str(Path(__file__).parent))
|
| 13 |
from sheets_logger import log_query
|
|
|
|
| 569 |
chunks = []
|
| 570 |
else:
|
| 571 |
with st.spinner("Searching case studies and generating response..."):
|
| 572 |
+
try:
|
| 573 |
+
answer, chunks = run_pipeline(
|
| 574 |
+
query,
|
| 575 |
+
questionnaire_answers=answers if answers else None,
|
| 576 |
+
model=model_choice,
|
| 577 |
+
return_chunks=True,
|
| 578 |
+
)
|
| 579 |
+
except RAGProviderError as exc:
|
| 580 |
+
st.error(str(exc))
|
| 581 |
+
st.stop()
|
| 582 |
|
| 583 |
# Save to history
|
| 584 |
st.session_state.history.insert(0, {
|
rag/pipeline.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import re
|
| 3 |
import requests
|
| 4 |
import anthropic
|
|
|
|
| 5 |
from openai import OpenAI
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from rag.retriever import MRRetriever
|
|
@@ -21,6 +22,14 @@ _OVERVIEW_RE = re.compile(
|
|
| 21 |
)
|
| 22 |
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def query_ollama(prompt: str) -> str:
|
| 25 |
response = requests.post(
|
| 26 |
OLLAMA_URL,
|
|
@@ -32,23 +41,45 @@ def query_ollama(prompt: str) -> str:
|
|
| 32 |
|
| 33 |
|
| 34 |
def query_claude(prompt: str, max_tokens: int = 4096) -> str:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
def query_openai(prompt: str, max_tokens: int = 2048) -> str:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
def run_pipeline(
|
|
|
|
| 2 |
import re
|
| 3 |
import requests
|
| 4 |
import anthropic
|
| 5 |
+
from openai import APIConnectionError, APIError, AuthenticationError, RateLimitError
|
| 6 |
from openai import OpenAI
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from rag.retriever import MRRetriever
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
|
| 25 |
+
class RAGProviderError(RuntimeError):
|
| 26 |
+
"""Raised when the configured LLM provider cannot return an answer."""
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def _env_value(name: str) -> str:
|
| 30 |
+
return (os.getenv(name) or "").strip()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
def query_ollama(prompt: str) -> str:
|
| 34 |
response = requests.post(
|
| 35 |
OLLAMA_URL,
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
def query_claude(prompt: str, max_tokens: int = 4096) -> str:
|
| 44 |
+
try:
|
| 45 |
+
client = anthropic.Anthropic(api_key=_env_value("ANTHROPIC_API_KEY"))
|
| 46 |
+
message = client.messages.create(
|
| 47 |
+
model="claude-sonnet-4-6",
|
| 48 |
+
max_tokens=max_tokens,
|
| 49 |
+
messages=[{"role": "user", "content": prompt}],
|
| 50 |
+
)
|
| 51 |
+
return message.content[0].text.strip()
|
| 52 |
+
except Exception as exc:
|
| 53 |
+
raise RAGProviderError(
|
| 54 |
+
"The Anthropic API could not generate an answer right now. "
|
| 55 |
+
"Please check the Anthropic billing/API key configuration."
|
| 56 |
+
) from exc
|
| 57 |
|
| 58 |
|
| 59 |
def query_openai(prompt: str, max_tokens: int = 2048) -> str:
|
| 60 |
+
try:
|
| 61 |
+
client = OpenAI(api_key=_env_value("OPENAI_API_KEY"))
|
| 62 |
+
response = client.chat.completions.create(
|
| 63 |
+
model=_env_value("OPENAI_MODEL") or "gpt-4o-mini",
|
| 64 |
+
max_completion_tokens=max_tokens,
|
| 65 |
+
messages=[{"role": "user", "content": prompt}],
|
| 66 |
+
)
|
| 67 |
+
return response.choices[0].message.content.strip()
|
| 68 |
+
except AuthenticationError as exc:
|
| 69 |
+
raise RAGProviderError(
|
| 70 |
+
"The OpenAI API key was rejected. Please update the OPENAI_API_KEY secret "
|
| 71 |
+
"in Hugging Face and try again."
|
| 72 |
+
) from exc
|
| 73 |
+
except RateLimitError as exc:
|
| 74 |
+
raise RAGProviderError(
|
| 75 |
+
"The OpenAI API is rate-limited or out of quota right now. Please check "
|
| 76 |
+
"OpenAI billing/limits and try again."
|
| 77 |
+
) from exc
|
| 78 |
+
except (APIConnectionError, APIError) as exc:
|
| 79 |
+
raise RAGProviderError(
|
| 80 |
+
"The OpenAI API could not be reached from Hugging Face right now. "
|
| 81 |
+
"Please try again in a moment."
|
| 82 |
+
) from exc
|
| 83 |
|
| 84 |
|
| 85 |
def run_pipeline(
|