ragbench-rag-eval / app /222222-main - Copy.py
Renangi's picture
Initial commit without secrets
c8dfbc0
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
from ragbench_eval.pipeline import RagBenchExperiment
app = FastAPI(title="RAGBench RAG Evaluation API")
class RunRequest(BaseModel):
domain: str
k: int = 3
max_examples: int = 20
split: str = "test"
@app.post("/run_domain")
def run_domain(req: RunRequest):
exp = RagBenchExperiment(
k=req.k,
max_examples=req.max_examples,
split=req.split,
)
result = exp.run_domain(req.domain)
return result
@app.get("/health")
def health():
return {"status": "ok"}
@app.get("/")
def root():
return {
"message": "RAGBench RAG Evaluation API is running.",
"endpoints": {
"health": "/health",
"docs": "/docs",
"ui": "/ui",
"run_domain": "/run_domain (POST)",
},
}
# ------------- NEW: simple frontend at /ui -----------------
@app.get("/ui", response_class=HTMLResponse)
def ui():
html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>RAGBench RAG Evaluation UI</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
margin: 0;
padding: 0;
background: #f5f7fa;
color: #111827;
}
.wrapper {
max-width: 960px;
margin: 2rem auto;
padding: 1.5rem;
background: #ffffff;
border-radius: 0.75rem;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.06);
}
h1 {
margin-top: 0;
font-size: 1.6rem;
}
.row {
display: flex;
flex-wrap: wrap;
gap: 1rem;
margin-bottom: 1rem;
}
.field {
flex: 1 1 180px;
min-width: 160px;
}
label {
display: block;
font-size: 0.85rem;
font-weight: 600;
margin-bottom: 0.25rem;
}
select, input {
width: 100%;
padding: 0.45rem 0.55rem;
border-radius: 0.375rem;
border: 1px solid #d1d5db;
font-size: 0.9rem;
box-sizing: border-box;
}
button {
padding: 0.55rem 1.2rem;
border-radius: 999px;
border: none;
background: #2563eb;
color: #ffffff;
font-weight: 600;
font-size: 0.95rem;
cursor: pointer;
}
button:disabled {
opacity: 0.6;
cursor: default;
}
.actions {
margin-top: 0.5rem;
margin-bottom: 1rem;
}
.status {
font-size: 0.85rem;
margin-bottom: 0.5rem;
color: #4b5563;
}
pre {
background: #0b1020;
color: #e5e7eb;
padding: 1rem;
border-radius: 0.75rem;
overflow: auto;
max-height: 480px;
font-size: 0.8rem;
}
code {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}
@media (max-width: 640px) {
.wrapper {
margin: 0.5rem;
border-radius: 0.5rem;
}
}
</style>
</head>
<body>
<div class="wrapper">
<h1>RAGBench RAG Evaluation</h1>
<p style="font-size:0.9rem; color:#4b5563;">
Use this UI to call <code>POST /run_domain</code> and inspect the metrics
for a given domain. The backend uses the RAGBench dataset and your configured LLMs.
</p>
<div class="row">
<div class="field">
<label for="domain">Domain</label>
<select id="domain">
<option value="biomedical">Biomedical</option>
<option value="general_knowledge">General Knowledge</option>
<option value="legal">Legal</option>
<option value="customer_support">Customer Support</option>
<option value="finance">Finance</option>
</select>
</div>
<div class="field">
<label for="k">Top-k documents</label>
<input id="k" type="number" value="3" min="1" />
</div>
<div class="field">
<label for="max_examples">Max examples</label>
<input id="max_examples" type="number" value="5" min="1" />
</div>
<div class="field">
<label for="split">Dataset split</label>
<input id="split" type="text" value="test" />
</div>
</div>
<div class="actions">
<button id="runBtn" onclick="runDomain()">Run Domain Evaluation</button>
</div>
<div class="status" id="status"></div>
<pre><code id="output">{}</code></pre>
</div>
<script>
async function runDomain() {
const domainEl = document.getElementById("domain");
const kEl = document.getElementById("k");
const maxExamplesEl = document.getElementById("max_examples");
const splitEl = document.getElementById("split");
const statusEl = document.getElementById("status");
const outputEl = document.getElementById("output");
const btn = document.getElementById("runBtn");
const domain = domainEl.value;
const k = parseInt(kEl.value || "3", 10);
const maxExamples = parseInt(maxExamplesEl.value || "5", 10);
const split = splitEl.value || "test";
const payload = {
domain: domain,
k: k,
max_examples: maxExamples,
split: split
};
statusEl.textContent = "Running evaluation...";
btn.disabled = true;
outputEl.textContent = "{}";
try {
const res = await fetch("/run_domain", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
const data = await res.json();
if (!res.ok) {
statusEl.textContent = "Error " + res.status;
} else {
statusEl.textContent = "Done.";
}
outputEl.textContent = JSON.stringify(data, null, 2);
} catch (err) {
statusEl.textContent = "Request failed: " + err;
outputEl.textContent = "{}";
} finally {
btn.disabled = false;
}
}
</script>
</body>
</html>
"""
return HTMLResponse(content=html)