Spaces:
Sleeping
Sleeping
Quincy Hsieh commited on
Commit ·
03514f5
1
Parent(s): d2c4c08
Re-structure folder for prompt engineering
Browse filesFiles changed (3)
- README.md+49-3
- app.py+5-11
- prompts/rag_prompt.txtAdded+10
142 unmodified lines14314414514614714814915015115215315415515615715815987 unmodified lines247248249250251252253254255256257258259260261262263264142 unmodified linesCombine retrieved context with the user's question and generate an answer.1. **Build prompt** — Create a structured prompt with system instructions, retrieved context, and the user's question2. **Call LLM API** — Send the prompt to the Hugging Face Inference API (Zephyr-7B)3. **Return response** — The LLM generates an answer grounded in the provided context```pythonfrom huggingface_hub import InferenceClientclient = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=HF_TOKEN)response = client.text_generation(prompt, max_new_tokens=512)```### Step 6: API Endpoint (`/query`)The FastAPI endpoint ties everything together for the evaluation system.87 unmodified lines# Clone the repositorygit clone https://huggingface.co/spaces/YOUR_USERNAME/Example-App-Hackathon-Gustave-Eiffel-2026cd Example-App-Hackathon-Gustave-Eiffel-2026# Install dependenciespip install -r requirements.txt# Set your HF tokenexport HF_TOKEN="hf_your_token_here"# Run the applicationpython app.py# Server starts at http://localhost:7860```### Deploy to Hugging Face Spaces1. Create a new Space on huggingface.co (select Gradio SDK)142 unmodified lines14314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817987 unmodified lines267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310142 unmodified linesCombine retrieved context with the user's question and generate an answer.1. **Build prompt** — Load the template from [`prompts/rag_prompt.txt`](prompts/rag_prompt.txt), inject retrieved context and the user's question2. **Call LLM API** — Send the prompt to the Hugging Face Inference API (Zephyr-7B)3. **Return response** — The LLM generates an answer grounded in the provided contextThe prompt template (`prompts/rag_prompt.txt`):```<|system|>You are a helpful assistant. Answer the user's question based ONLY on the provided context.If the context does not contain enough information to answer, say "I don't have enough information to answer this question."Always be concise and factual.</s><|user|>Context:{context}Question: {question}</s><|assistant|>```The template is loaded once at startup and filled at query time:```pythonRAG_PROMPT_TEMPLATE = Path("prompts/rag_prompt.txt").read_text(encoding="utf-8")# At query time:prompt = RAG_PROMPT_TEMPLATE.format(context=context_text, question=user_query)response = client.text_generation(prompt, max_new_tokens=512)```> **Tip:** Edit `prompts/rag_prompt.txt` to tune the model's behaviour (tone, language, output format) without touching application code.### Step 6: API Endpoint (`/query`)The FastAPI endpoint ties everything together for the evaluation system.87 unmodified lines# Clone the repositorygit clone https://huggingface.co/spaces/YOUR_USERNAME/Example-App-Hackathon-Gustave-Eiffel-2026cd Example-App-Hackathon-Gustave-Eiffel-2026```#### Set Up a Python Virtual EnvironmentUsing a virtual environment isolates project dependencies from your global Python installation.```bash# Create the virtual environment (Python 3.11+ required)python -m venv ~/.venv/hackathon-eiffel# Activate it# macOS / Linuxsource ~/.venv/hackathon-eiffel/bin/activate# Windows (PowerShell)~\.venv\hackathon-eiffel\Scripts\Activate.ps1# Windows (Command Prompt)~\.venv\hackathon-eiffel\Scripts\activate.bat```Once your virtual environment is active, install dependencies and run the app:```bash# Install dependenciespip install -r requirements.txt# Set your HF token# macOS / Linuxexport HF_TOKEN="hf_your_token_here"# Windows (PowerShell)$env:HF_TOKEN = "hf_your_token_here"# Run the applicationpython app.py# Server starts at http://localhost:7860```> **Tip:** To deactivate the virtual environment when you are done, run `deactivate` (venv) or `conda deactivate` (conda).### Deploy to Hugging Face Spaces1. Create a new Space on huggingface.co (select Gradio SDK)
48 unmodified lines495051525354176 unmodified lines23123223323423523623723823924024124224324424524624748 unmodified lines# HF_TOKEN is set as a Space secret for authenticated Inference API callsHF_TOKEN = os.environ.get("HF_TOKEN")# ---------------------------------------------------------------------------# Step 1: Initialize the Embedding Model# ---------------------------------------------------------------------------176 unmodified linesf"[Source: {ctx['source']}]\n{ctx['text']}" for ctx in contexts)prompt = f"""<|system|>You are a helpful assistant. Answer the user's question based ONLY on the provided context.If the context does not contain enough information to answer, say "I don't have enough information to answer this question."Always be concise and factual.</s><|user|>Context:{context_text}Question: {query}</s><|assistant|>"""return prompt48 unmodified lines49505152535455565758176 unmodified lines23523623723823924024148 unmodified lines# HF_TOKEN is set as a Space secret for authenticated Inference API callsHF_TOKEN = os.environ.get("HF_TOKEN")# Prompt template loaded from file so it can be edited without touching application code_PROMPT_TEMPLATE_PATH = Path(__file__).parent / "prompts" / "rag_prompt.txt"RAG_PROMPT_TEMPLATE = _PROMPT_TEMPLATE_PATH.read_text(encoding="utf-8")# ---------------------------------------------------------------------------# Step 1: Initialize the Embedding Model# ---------------------------------------------------------------------------176 unmodified linesf"[Source: {ctx['source']}]\n{ctx['text']}" for ctx in contexts)prompt = RAG_PROMPT_TEMPLATE.format(context=context_text, question=query)return prompt
12345678910<|system|>You are a helpful assistant. Answer the user's question based ONLY on the provided context.If the context does not contain enough information to answer, say "I don't have enough information to answer this question."Always be concise and factual.</s><|user|>Context:{context}
Question: {question}</s><|assistant|>README.md+49-3
app.py+5-11
prompts/rag_prompt.txtAdded+10