Spaces:
Sleeping
Sleeping
Quincy Hsieh commited on
Commit ·
03514f5
1
Parent(s): d2c4c08
Re-structure folder for prompt engineering
Browse files- README.md +49 -3
- app.py +5 -11
- prompts/rag_prompt.txt +10 -0
README.md
CHANGED
|
@@ -143,17 +143,37 @@ results = collection.query(
|
|
| 143 |
|
| 144 |
Combine retrieved context with the user's question and generate an answer.
|
| 145 |
|
| 146 |
-
1. **Build prompt** —
|
| 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 |
```python
|
| 151 |
-
|
| 152 |
|
| 153 |
-
|
|
|
|
| 154 |
response = client.text_generation(prompt, max_new_tokens=512)
|
| 155 |
```
|
| 156 |
|
|
|
|
|
|
|
| 157 |
### Step 6: API Endpoint (`/query`)
|
| 158 |
|
| 159 |
The FastAPI endpoint ties everything together for the evaluation system.
|
|
@@ -247,18 +267,44 @@ System health check.
|
|
| 247 |
# Clone the repository
|
| 248 |
git clone https://huggingface.co/spaces/YOUR_USERNAME/Example-App-Hackathon-Gustave-Eiffel-2026
|
| 249 |
cd Example-App-Hackathon-Gustave-Eiffel-2026
|
|
|
|
|
|
|
|
|
|
| 250 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
# Install dependencies
|
| 252 |
pip install -r requirements.txt
|
| 253 |
|
| 254 |
# Set your HF token
|
|
|
|
| 255 |
export HF_TOKEN="hf_your_token_here"
|
|
|
|
|
|
|
| 256 |
|
| 257 |
# Run the application
|
| 258 |
python app.py
|
| 259 |
# Server starts at http://localhost:7860
|
| 260 |
```
|
| 261 |
|
|
|
|
|
|
|
| 262 |
### Deploy to Hugging Face Spaces
|
| 263 |
|
| 264 |
1. Create a new Space on huggingface.co (select Gradio SDK)
|
|
|
|
| 143 |
|
| 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:
|
| 166 |
+
|
| 167 |
```python
|
| 168 |
+
RAG_PROMPT_TEMPLATE = Path("prompts/rag_prompt.txt").read_text(encoding="utf-8")
|
| 169 |
|
| 170 |
+
# At query time:
|
| 171 |
+
prompt = RAG_PROMPT_TEMPLATE.format(context=context_text, question=user_query)
|
| 172 |
response = client.text_generation(prompt, max_new_tokens=512)
|
| 173 |
```
|
| 174 |
|
| 175 |
+
> **Tip:** Edit `prompts/rag_prompt.txt` to tune the model's behaviour (tone, language, output format) without touching application code.
|
| 176 |
+
|
| 177 |
### Step 6: API Endpoint (`/query`)
|
| 178 |
|
| 179 |
The FastAPI endpoint ties everything together for the evaluation system.
|
|
|
|
| 267 |
# Clone the repository
|
| 268 |
git clone https://huggingface.co/spaces/YOUR_USERNAME/Example-App-Hackathon-Gustave-Eiffel-2026
|
| 269 |
cd Example-App-Hackathon-Gustave-Eiffel-2026
|
| 270 |
+
```
|
| 271 |
+
|
| 272 |
+
#### Set Up a Python Virtual Environment
|
| 273 |
|
| 274 |
+
Using a virtual environment isolates project dependencies from your global Python installation.
|
| 275 |
+
|
| 276 |
+
```bash
|
| 277 |
+
# Create the virtual environment (Python 3.11+ required)
|
| 278 |
+
python -m venv ~/.venv/hackathon-eiffel
|
| 279 |
+
|
| 280 |
+
# Activate it
|
| 281 |
+
# macOS / Linux
|
| 282 |
+
source ~/.venv/hackathon-eiffel/bin/activate
|
| 283 |
+
# Windows (PowerShell)
|
| 284 |
+
~\.venv\hackathon-eiffel\Scripts\Activate.ps1
|
| 285 |
+
# Windows (Command Prompt)
|
| 286 |
+
~\.venv\hackathon-eiffel\Scripts\activate.bat
|
| 287 |
+
```
|
| 288 |
+
|
| 289 |
+
Once your virtual environment is active, install dependencies and run the app:
|
| 290 |
+
|
| 291 |
+
```bash
|
| 292 |
# Install dependencies
|
| 293 |
pip install -r requirements.txt
|
| 294 |
|
| 295 |
# Set your HF token
|
| 296 |
+
# macOS / Linux
|
| 297 |
export HF_TOKEN="hf_your_token_here"
|
| 298 |
+
# Windows (PowerShell)
|
| 299 |
+
$env:HF_TOKEN = "hf_your_token_here"
|
| 300 |
|
| 301 |
# Run the application
|
| 302 |
python app.py
|
| 303 |
# Server starts at http://localhost:7860
|
| 304 |
```
|
| 305 |
|
| 306 |
+
> **Tip:** To deactivate the virtual environment when you are done, run `deactivate` (venv) or `conda deactivate` (conda).
|
| 307 |
+
|
| 308 |
### Deploy to Hugging Face Spaces
|
| 309 |
|
| 310 |
1. Create a new Space on huggingface.co (select Gradio SDK)
|
app.py
CHANGED
|
@@ -49,6 +49,10 @@ TOP_K_RESULTS = 3
|
|
| 49 |
# HF_TOKEN is set as a Space secret for authenticated Inference API calls
|
| 50 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# ---------------------------------------------------------------------------
|
| 53 |
# Step 1: Initialize the Embedding Model
|
| 54 |
# ---------------------------------------------------------------------------
|
|
@@ -231,17 +235,7 @@ def build_rag_prompt(query: str, contexts: list[dict]) -> str:
|
|
| 231 |
f"[Source: {ctx['source']}]\n{ctx['text']}" for ctx in contexts
|
| 232 |
)
|
| 233 |
|
| 234 |
-
prompt =
|
| 235 |
-
You are a helpful assistant. Answer the user's question based ONLY on the provided context.
|
| 236 |
-
If the context does not contain enough information to answer, say "I don't have enough information to answer this question."
|
| 237 |
-
Always be concise and factual.</s>
|
| 238 |
-
<|user|>
|
| 239 |
-
Context:
|
| 240 |
-
{context_text}
|
| 241 |
-
|
| 242 |
-
Question: {query}</s>
|
| 243 |
-
<|assistant|>
|
| 244 |
-
"""
|
| 245 |
return prompt
|
| 246 |
|
| 247 |
|
|
|
|
| 49 |
# HF_TOKEN is set as a Space secret for authenticated Inference API calls
|
| 50 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 51 |
|
| 52 |
+
# Prompt template loaded from file so it can be edited without touching application code
|
| 53 |
+
_PROMPT_TEMPLATE_PATH = Path(__file__).parent / "prompts" / "rag_prompt.txt"
|
| 54 |
+
RAG_PROMPT_TEMPLATE = _PROMPT_TEMPLATE_PATH.read_text(encoding="utf-8")
|
| 55 |
+
|
| 56 |
# ---------------------------------------------------------------------------
|
| 57 |
# Step 1: Initialize the Embedding Model
|
| 58 |
# ---------------------------------------------------------------------------
|
|
|
|
| 235 |
f"[Source: {ctx['source']}]\n{ctx['text']}" for ctx in contexts
|
| 236 |
)
|
| 237 |
|
| 238 |
+
prompt = RAG_PROMPT_TEMPLATE.format(context=context_text, question=query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
return prompt
|
| 240 |
|
| 241 |
|
prompts/rag_prompt.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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|>
|