Quincy Hsieh commited on
Commit
c8361c5
Β·
1 Parent(s): 1974292

Fix error LLM API call failed: 404 Client Error: Not Found for url...

Browse files
Files changed (3) hide show
  1. README.md +15 -9
  2. app.py +5 -2
  3. prompts/rag_prompt.txt +5 -6
README.md CHANGED
@@ -59,7 +59,7 @@ This application demonstrates how to build a production-ready RAG system within
59
  β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
60
  β”‚ β”‚ HF Inference β”‚ β”‚ Sentence β”‚ β”‚
61
  β”‚ β”‚ API (LLM) β”‚ β”‚ Transformers β”‚ β”‚
62
- β”‚ β”‚ Zephyr-7B β”‚ β”‚ (Embeddings) β”‚ β”‚
63
  β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
64
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
65
  ```
@@ -144,22 +144,20 @@ results = collection.query(
144
  Combine retrieved context with the user's question and generate an answer.
145
 
146
  1. **Build prompt** β€” Load the template from [`prompts/rag_prompt.txt`](prompts/rag_prompt.txt), inject retrieved context and the user's question
147
- 2. **Call LLM API** β€” Send the prompt to the Hugging Face Inference API (Zephyr-7B)
148
  3. **Return response** β€” The LLM generates an answer grounded in the provided context
149
 
150
  The prompt template (`prompts/rag_prompt.txt`):
151
 
152
  ```
153
- <|system|>
154
- You are a helpful assistant. Answer the user's question based ONLY on the provided context.
155
  If the context does not contain enough information to answer, say "I don't have enough information to answer this question."
156
- Always be concise and factual.</s>
157
- <|user|>
158
  Context:
159
  {context}
160
 
161
- Question: {question}</s>
162
- <|assistant|>
163
  ```
164
 
165
  The template is loaded once at startup and filled at query time:
@@ -337,7 +335,7 @@ curl -X POST http://localhost:7860/ingest \
337
  |---|---|---|
338
  | `HF_TOKEN` | (Space Secret) | Hugging Face API token for Inference API |
339
  | `EMBEDDING_MODEL_NAME` | `sentence-transformers/all-MiniLM-L6-v2` | Model for text embeddings |
340
- | `LLM_MODEL_NAME` | `HuggingFaceH4/zephyr-7b-beta` | LLM for answer generation |
341
  | `CHROMA_PERSIST_DIR` | `./chroma_db` | ChromaDB storage path |
342
  | `CHUNK_SIZE` | `512` | Text chunk size in characters |
343
  | `CHUNK_OVERLAP` | `50` | Overlap between chunks |
@@ -380,6 +378,14 @@ The `/query` endpoint is designed to be called by external RAG evaluation framew
380
 
381
  ## Troubleshooting
382
 
 
 
 
 
 
 
 
 
383
  ### SSL Certificate Verification Error
384
 
385
  If you see an error like the one below when running `python app.py`, your machine is most likely behind a **corporate proxy that performs SSL inspection** and has its own root CA that Python does not trust by default.
 
59
  β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
60
  β”‚ β”‚ HF Inference β”‚ β”‚ Sentence β”‚ β”‚
61
  β”‚ β”‚ API (LLM) β”‚ β”‚ Transformers β”‚ β”‚
62
+ β”‚ β”‚ Mistral-7B β”‚ β”‚ (Embeddings) β”‚ β”‚
63
  β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
64
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
65
  ```
 
144
  Combine retrieved context with the user's question and generate an answer.
145
 
146
  1. **Build prompt** β€” Load the template from [`prompts/rag_prompt.txt`](prompts/rag_prompt.txt), inject retrieved context and the user's question
147
+ 2. **Call LLM API** β€” Send the prompt to the Hugging Face Inference API (Mistral-7B-Instruct-v0.3)
148
  3. **Return response** β€” The LLM generates an answer grounded in the provided context
149
 
150
  The prompt template (`prompts/rag_prompt.txt`):
151
 
152
  ```
153
+ <s>[INST] You are a helpful assistant. Answer the user's question based ONLY on the provided context.
 
154
  If the context does not contain enough information to answer, say "I don't have enough information to answer this question."
155
+ Always be concise and factual.
156
+
157
  Context:
158
  {context}
159
 
160
+ Question: {question} [/INST]
 
161
  ```
162
 
163
  The template is loaded once at startup and filled at query time:
 
335
  |---|---|---|
336
  | `HF_TOKEN` | (Space Secret) | Hugging Face API token for Inference API |
337
  | `EMBEDDING_MODEL_NAME` | `sentence-transformers/all-MiniLM-L6-v2` | Model for text embeddings |
338
+ | `LLM_MODEL_NAME` | `mistralai/Mistral-7B-Instruct-v0.3` | LLM for answer generation |
339
  | `CHROMA_PERSIST_DIR` | `./chroma_db` | ChromaDB storage path |
340
  | `CHUNK_SIZE` | `512` | Text chunk size in characters |
341
  | `CHUNK_OVERLAP` | `50` | Overlap between chunks |
 
378
 
379
  ## Troubleshooting
380
 
381
+ ### ChromaDB Telemetry Error
382
+
383
+ If you see `capture() takes 1 positional argument but 3 were given` in the logs, it is a version incompatibility between ChromaDB and the `posthog` library. Telemetry is disabled at startup (`anonymized_telemetry=False`) so this error should no longer appear. If it persists after updating dependencies, pin `posthog<3.0` in `requirements.txt`.
384
+
385
+ ### LLM 404 β€” Model Not Found
386
+
387
+ If the `/query` endpoint logs a `404 Not Found` for the Inference API URL, the configured model has been removed from the free serverless tier. Update `LLM_MODEL_NAME` in `app.py` to an available model and adjust `prompts/rag_prompt.txt` to match its instruction format. The current default (`mistralai/Mistral-7B-Instruct-v0.3`) uses the `<s>[INST] … [/INST]` format.
388
+
389
  ### SSL Certificate Verification Error
390
 
391
  If you see an error like the one below when running `python app.py`, your machine is most likely behind a **corporate proxy that performs SSL inspection** and has its own root CA that Python does not trust by default.
app.py CHANGED
@@ -39,7 +39,7 @@ logger = logging.getLogger(__name__)
39
  # ---------------------------------------------------------------------------
40
 
41
  EMBEDDING_MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
42
- LLM_MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
43
  CHROMA_PERSIST_DIR = "./chroma_db"
44
  COLLECTION_NAME = "rag_documents"
45
  CHUNK_SIZE = 512
@@ -81,7 +81,10 @@ logger.info("Embedding model loaded successfully.")
81
  # because it requires no external database service.
82
 
83
  logger.info(f"Initializing ChromaDB at: {CHROMA_PERSIST_DIR}")
84
- chroma_client = chromadb.PersistentClient(path=CHROMA_PERSIST_DIR)
 
 
 
85
  collection = chroma_client.get_or_create_collection(
86
  name=COLLECTION_NAME,
87
  metadata={"hnsw:space": "cosine"},
 
39
  # ---------------------------------------------------------------------------
40
 
41
  EMBEDDING_MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
42
+ LLM_MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.3"
43
  CHROMA_PERSIST_DIR = "./chroma_db"
44
  COLLECTION_NAME = "rag_documents"
45
  CHUNK_SIZE = 512
 
81
  # because it requires no external database service.
82
 
83
  logger.info(f"Initializing ChromaDB at: {CHROMA_PERSIST_DIR}")
84
+ chroma_client = chromadb.PersistentClient(
85
+ path=CHROMA_PERSIST_DIR,
86
+ settings=Settings(anonymized_telemetry=False),
87
+ )
88
  collection = chroma_client.get_or_create_collection(
89
  name=COLLECTION_NAME,
90
  metadata={"hnsw:space": "cosine"},
prompts/rag_prompt.txt CHANGED
@@ -1,10 +1,9 @@
1
- <|system|>
2
- You are a helpful assistant. Answer the user's question based ONLY on the provided context.
3
  If the context does not contain enough information to answer, say "I don't have enough information to answer this question."
4
- Always be concise and factual.</s>
5
- <|user|>
6
  Context:
7
  {context}
8
 
9
- Question: {question}</s>
10
- <|assistant|>
 
1
+ <s>[INST] You are a helpful assistant. Answer the user's question based ONLY on the provided context.
 
2
  If the context does not contain enough information to answer, say "I don't have enough information to answer this question."
3
+ Always be concise and factual.
4
+
5
  Context:
6
  {context}
7
 
8
+ Question: {question} [/INST]
9
+