Spaces:
Sleeping
Sleeping
Create generator.py
Browse files- rag_pipeline/generator.py +73 -0
rag_pipeline/generator.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Final answer generation using Anthropic API.
|
| 3 |
+
Returns answer + the exact prompt sent to the model for full transparency.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
from typing import List, Tuple
|
| 8 |
+
import anthropic
|
| 9 |
+
|
| 10 |
+
from .reranker import RerankResult
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
SYSTEM_PROMPT = """You are a precise scientific assistant. Answer questions using ONLY
|
| 14 |
+
the provided context excerpts from the paper. Follow these rules:
|
| 15 |
+
|
| 16 |
+
1. Cite the specific context excerpt (by number) that supports each claim.
|
| 17 |
+
2. If the context does not contain enough information, say so explicitly.
|
| 18 |
+
3. Do not add knowledge from outside the provided context.
|
| 19 |
+
4. Be concise and precise — this is a scientific document.
|
| 20 |
+
5. Format your answer clearly with citations like [Excerpt 1], [Excerpt 2].
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def build_prompt(query: str, kept_results: List[RerankResult]) -> str:
|
| 25 |
+
context_blocks = []
|
| 26 |
+
for i, r in enumerate(kept_results, 1):
|
| 27 |
+
context_blocks.append(f"[Excerpt {i}]\n{r.text}")
|
| 28 |
+
|
| 29 |
+
context_str = "\n\n".join(context_blocks)
|
| 30 |
+
|
| 31 |
+
return f"""Here are the relevant excerpts retrieved from the scientific paper:
|
| 32 |
+
|
| 33 |
+
{context_str}
|
| 34 |
+
|
| 35 |
+
---
|
| 36 |
+
|
| 37 |
+
Question: {query}
|
| 38 |
+
|
| 39 |
+
Answer based strictly on the excerpts above. Cite which excerpt(s) support each point."""
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def generate_answer(
|
| 43 |
+
query: str,
|
| 44 |
+
kept_results: List[RerankResult],
|
| 45 |
+
model: str = "claude-haiku-4-5-20251001",
|
| 46 |
+
api_key: str = None,
|
| 47 |
+
) -> Tuple[str, str]:
|
| 48 |
+
"""
|
| 49 |
+
Returns (answer, prompt_sent_to_model).
|
| 50 |
+
Shows the prompt for transparency.
|
| 51 |
+
"""
|
| 52 |
+
if api_key:
|
| 53 |
+
os.environ["ANTHROPIC_API_KEY"] = api_key
|
| 54 |
+
|
| 55 |
+
key = os.environ.get("ANTHROPIC_API_KEY")
|
| 56 |
+
if not key:
|
| 57 |
+
return "❌ No API key provided.", ""
|
| 58 |
+
|
| 59 |
+
prompt = build_prompt(query, kept_results)
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
client = anthropic.Anthropic(api_key=key)
|
| 63 |
+
message = client.messages.create(
|
| 64 |
+
model=model,
|
| 65 |
+
max_tokens=1024,
|
| 66 |
+
system=SYSTEM_PROMPT,
|
| 67 |
+
messages=[{"role": "user", "content": prompt}],
|
| 68 |
+
)
|
| 69 |
+
answer = message.content[0].text
|
| 70 |
+
except Exception as e:
|
| 71 |
+
answer = f"❌ API error: {e}"
|
| 72 |
+
|
| 73 |
+
return answer, prompt
|