Spaces:
Sleeping
Sleeping
Commit ·
126332e
1
Parent(s): b609489
OpenRouter: log when key missing; try more env names; doc HF secret OPENROUTER_API_KEY
Browse files
DEPLOY.md
CHANGED
|
@@ -90,7 +90,8 @@ git push hf main
|
|
| 90 |
|
| 91 |
- **SDK**: Gradio (see `README.md` → `sdk: gradio`, `app_file: app.py`).
|
| 92 |
- **Secrets**: In the Space **Settings → Repository secrets**, set:
|
| 93 |
-
- **`gemini_api`**: your Gemini API key (
|
|
|
|
| 94 |
- **Hardware**: Default CPU is enough; GPU is optional for faster embedding if you change the app later.
|
| 95 |
|
| 96 |
---
|
|
|
|
| 90 |
|
| 91 |
- **SDK**: Gradio (see `README.md` → `sdk: gradio`, `app_file: app.py`).
|
| 92 |
- **Secrets**: In the Space **Settings → Repository secrets**, set:
|
| 93 |
+
- **`gemini_api`**: your Gemini API key (fallback when OpenRouter is not used).
|
| 94 |
+
- **`OPENROUTER_API_KEY`** *(recommended)*: OpenRouter API key — used first for faster answers and to avoid Gemini rate limits. Get one at [openrouter.ai/keys](https://openrouter.ai/keys).
|
| 95 |
- **Hardware**: Default CPU is enough; GPU is optional for faster embedding if you change the app later.
|
| 96 |
|
| 97 |
---
|
README.md
CHANGED
|
@@ -65,8 +65,9 @@ Open the local Gradio URL printed in the terminal.
|
|
| 65 |
## Configuration
|
| 66 |
|
| 67 |
### Environment variables
|
| 68 |
-
- **`
|
| 69 |
-
- **`
|
|
|
|
| 70 |
|
| 71 |
### Rate limiting behavior (important)
|
| 72 |
Gemini can return rate limits (HTTP 429). The RAG engine implements:
|
|
@@ -116,8 +117,9 @@ CarsRUS/
|
|
| 116 |
|
| 117 |
## Troubleshooting
|
| 118 |
|
| 119 |
-
### “Configuration Error
|
| 120 |
-
- Set
|
|
|
|
| 121 |
|
| 122 |
### Rate limit / 429 errors
|
| 123 |
- Wait a bit and retry (the app also retries automatically).
|
|
|
|
| 65 |
## Configuration
|
| 66 |
|
| 67 |
### Environment variables
|
| 68 |
+
- **`OPENROUTER_API_KEY`** *(recommended on HF)*: OpenRouter API key — tried first for faster answers and to avoid Gemini rate limits. On Hugging Face, add as Repository secret. Local: set in `.env` as `openRouter_API_KEY` or `OPENROUTER_API_KEY`.
|
| 69 |
+
- **`gemini_api`**: Gemini API key — used when OpenRouter is not set or fails. Required if you don’t use OpenRouter.
|
| 70 |
+
- **`GEMINI_REQUEST_DELAY`** *(optional)*: minimum seconds between Gemini requests per process (default: `0.5`).
|
| 71 |
|
| 72 |
### Rate limiting behavior (important)
|
| 73 |
Gemini can return rate limits (HTTP 429). The RAG engine implements:
|
|
|
|
| 117 |
|
| 118 |
## Troubleshooting
|
| 119 |
|
| 120 |
+
### “Configuration Error” / no API key
|
| 121 |
+
- Set at least one: **OPENROUTER_API_KEY** (recommended on HF: Settings → Repository secrets) or **gemini_api** in your environment or HF Space secrets.
|
| 122 |
+
- If you see “OpenRouter key not set” in logs and get Gemini rate limits, add **OPENROUTER_API_KEY** in the Space secrets so the app uses OpenRouter first.
|
| 123 |
|
| 124 |
### Rate limit / 429 errors
|
| 125 |
- Wait a bit and retry (the app also retries automatically).
|
app.py
CHANGED
|
@@ -65,9 +65,9 @@ def chat_function(message: str, history: list) -> str:
|
|
| 65 |
if not api_key and not openrouter_key:
|
| 66 |
yield """❌ **Configuration Error**
|
| 67 |
|
| 68 |
-
Set at least one API key:
|
| 69 |
-
• **OpenRouter** (faster): secret
|
| 70 |
-
• **Gemini** (fallback): secret
|
| 71 |
return
|
| 72 |
|
| 73 |
if not engine or not agent_graph:
|
|
|
|
| 65 |
if not api_key and not openrouter_key:
|
| 66 |
yield """❌ **Configuration Error**
|
| 67 |
|
| 68 |
+
Set at least one API key (Space → Settings → Repository secrets):
|
| 69 |
+
• **OpenRouter** (faster, avoids Gemini rate limits): add secret **OPENROUTER_API_KEY** — [openrouter.ai/keys](https://openrouter.ai/keys)
|
| 70 |
+
• **Gemini** (fallback): secret **gemini_api** — [Google AI Studio](https://aistudio.google.com/apikey)"""
|
| 71 |
return
|
| 72 |
|
| 73 |
if not engine or not agent_graph:
|
rag_engine.py
CHANGED
|
@@ -636,13 +636,18 @@ class RAGEngine:
|
|
| 636 |
self.last_request_time = time.time()
|
| 637 |
|
| 638 |
def _get_openrouter_key(self) -> Optional[str]:
|
| 639 |
-
"""OpenRouter API key from env
|
| 640 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 641 |
|
| 642 |
def _call_openrouter(self, system_prompt: str, prompt: str, timeout_seconds: int = 28) -> Optional[str]:
|
| 643 |
"""Call OpenRouter API for a fast response. Returns text or None on failure."""
|
| 644 |
key = self._get_openrouter_key()
|
| 645 |
if not key or not key.strip():
|
|
|
|
| 646 |
return None
|
| 647 |
url = "https://openrouter.ai/api/v1/chat/completions"
|
| 648 |
# Prefer fast models on OpenRouter
|
|
|
|
| 636 |
self.last_request_time = time.time()
|
| 637 |
|
| 638 |
def _get_openrouter_key(self) -> Optional[str]:
|
| 639 |
+
"""OpenRouter API key from env. HF Spaces: add Secret OPENROUTER_API_KEY. Local: .env openRouter_API_KEY."""
|
| 640 |
+
for name in ("OPENROUTER_API_KEY", "openRouter_API_KEY", "OPENROUTER_APIKEY", "OPENROUTER_KEY"):
|
| 641 |
+
v = os.environ.get(name)
|
| 642 |
+
if v and str(v).strip():
|
| 643 |
+
return str(v).strip()
|
| 644 |
+
return None
|
| 645 |
|
| 646 |
def _call_openrouter(self, system_prompt: str, prompt: str, timeout_seconds: int = 28) -> Optional[str]:
|
| 647 |
"""Call OpenRouter API for a fast response. Returns text or None on failure."""
|
| 648 |
key = self._get_openrouter_key()
|
| 649 |
if not key or not key.strip():
|
| 650 |
+
PIPELINE_LOG.info("OpenRouter key not set - add Secret OPENROUTER_API_KEY in HF Space settings. Using Gemini.")
|
| 651 |
return None
|
| 652 |
url = "https://openrouter.ai/api/v1/chat/completions"
|
| 653 |
# Prefer fast models on OpenRouter
|