tech-trans-rag / static /index.html
phaethon-order's picture
Implement retrieval text and embedding utilities, evaluation scripts, and regression tests
34d9afb
Raw History Blame Contribute Delete
22.2 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Technology Transactions Assistant (RAG model query interface)</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Rounded', 'SF Pro Display', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: #001122;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 20px;
color: #fff;
}
.header-section {
width: 100%;
max-width: 1200px;
background: #333;
border-radius: 20px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.5);
text-align: center;
}
.header-title {
color: #fff;
font-size: 2.5rem;
font-weight: 500;
text-decoration: underline;
text-shadow: 0 4px 8px rgba(0, 0, 0, 0.5);
margin: 10px;
}
.header-subtitle {
color: #ccc;
font-size: 0.85rem;
font-weight: 400;
font-style: italic;
margin: 5px;
}
.header-subtitle a.upload-link {
color: #87CEFA;
text-decoration: none;
}
.header-subtitle a.upload-link:hover {
color: #ffbf00;
text-decoration: underline;
}
.banner-image {
display: none;
}
.main-container {
display: flex;
gap: 18px;
width: 100%;
max-width: 1200px;
height: 60vh;
align-items: stretch;
}
.left-container {
flex: 1;
background: #333;
border-radius: 20px;
padding: 32px;
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.5);
display: flex;
flex-direction: column;
justify-content: flex-start;
gap: 10px;
min-height: 0;
}
.right-container {
flex: 1;
background: #333;
border-radius: 20px;
padding: 32px;
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.5);
display: flex;
flex-direction: column;
min-height: 0;
position: relative;
}
.left-container h1 {
display: none;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #fff;
font-size: 0.95rem;
letter-spacing: 0.5px;
}
.char-count {
float: right;
font-size: 0.85rem;
color: #ccc;
font-weight: normal;
text-transform: none;
letter-spacing: normal;
}
input[type="text"],
textarea {
width: 100%;
padding: 12px 16px;
border: 2px solid #555;
border-radius: 10px;
font-size: 1rem;
font-family: inherit;
transition: all 0.3s ease;
background: #222;
color: #fff;
}
input[type="text"]:focus,
textarea:focus {
outline: none;
border-color: #cc0000;
box-shadow: 0 0 0 3px rgba(204, 0, 0, 0.1);
}
textarea {
resize: vertical;
min-height: 120px;
}
.button-row {
display: flex;
gap: 12px;
margin-top: 10px;
}
.button-row .submit-btn,
.button-row .cancel-btn {
flex: 1;
padding: 12px 18px;
border-radius: 10px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.2s ease;
letter-spacing: 1px;
}
.submit-btn {
background: #cfdff0;
color: #0b2233;
border: 1px solid rgba(255,255,255,0.06);
}
.submit-btn:hover {
background: #b9cee3;
transform: translateY(-2px);
box-shadow: 0 8px 18px rgba(11,34,51,0.12);
}
.submit-btn:active {
transform: translateY(0);
}
.cancel-btn {
background: rgba(255,102,102,0.45);
color: #fff;
border: none;
}
.cancel-btn:hover {
background: rgba(255,102,102,0.60);
color: #fff;
}
.output-section {
background: #222;
border: 2px solid #555;
border-radius: 10px;
padding: 20px;
flex: 1;
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: flex-start;
white-space: pre-wrap;
word-wrap: break-word;
font-family: 'Consolas', 'Monaco', monospace;
font-size: 0.95rem;
line-height: 1.6;
color: #fff;
overflow-y: auto;
margin-bottom: 0;
}
/* New formatting for Response and Results sections */
.section-title {
font-family: inherit;
font-weight: 700;
font-size: 1.05rem;
text-decoration: underline;
margin: 2px 0 8px 0;
color: #f7fafc;
}
.response-block {
width: 100%;
margin-bottom: 14px;
}
.response-text {
white-space: pre-wrap;
line-height: 1.55;
color: #e6eef6;
}
.citations {
margin-top: 10px;
}
.citations ul, .results-section ol {
margin-top: 6px;
padding-left: 20px;
}
.citations li {
margin-bottom: 4px;
}
.results-section {
width: 100%;
border-top: 1px solid #444;
margin-top: 8px;
padding-top: 8px;
font-size: 0.9rem; /* slightly smaller */
}
.meta-line {
color: #cbd5e1;
font-size: 0.85rem;
}
.output-section.empty {
align-items: center;
justify-content: center;
text-align: center;
}
.output-placeholder {
color: #ccc;
font-style: italic;
display: block;
}
.loading {
display: none;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
align-items: center;
justify-content: center;
text-align: center;
color: #ffd700;
background: #222;
border-radius: 10px;
flex-direction: column;
gap: 12px;
}
.loading.active {
display: flex;
}
.spinner {
display: inline-block;
width: 20px;
height: 20px;
border: 3px solid rgba(255, 215, 0, 0.3);
border-radius: 50%;
border-top-color: #ffd700;
animation: spin 1s ease-in-out infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
@media (max-width: 768px) {
.main-container {
flex-direction: column;
height: auto;
}
}
</style>
</head>
<body>
<div class="header-section">
<h1 class="header-title">Technology Transactions Assistant</h1>
<p class="header-subtitle">A RAG model that restricts frontier-level foundation models to technology transactions use cases in the legal domain.</p>
<p class="header-subtitle">To add to the corpus, visit <a href="/upload" class="upload-link">/upload</a>. To view/edit the corpus, visit <a href="/manifest" class="upload-link">/manifest</a>. To view/edit embeddedings, visit <a href="/chunks" class="upload-link">/chunks</a>. To view database records directly, visit <a href="/db-viewer" class="upload-link">/db-viewer</a>.</p>
</div>
<div class="main-container">
<div class="left-container">
<h1>TechTrans Query</h1>
<form id="queryForm">
<div class="form-group">
<label for="query">
Query
<span class="char-count"><span id="queryCount">0</span>/1000</span>
</label>
<textarea
id="query"
name="query"
maxlength="1000"
placeholder="Enter query here (e.g., 'Show an example of a confidentiality clause,' 'Define the term Affiliate,' etc.)"
required
></textarea>
</div>
<div class="form-group">
<label for="docType">Document Type (optional)
<span class="char-count"><span id="docTypeCount">0</span>/200</span>
</label>
<input type="text" id="docType" name="docType" maxlength="200" placeholder="e.g., License Agreement, Terms of Use..." />
</div>
<div class="form-group">
<label for="jurisdiction">Jurisdiction (optional)
<span class="char-count"><span id="jurisdictionCount">0</span>/200</span>
</label>
<input type="text" id="jurisdiction" name="jurisdiction" maxlength="200" placeholder="e.g., US-CA, US-DE, FRG..." />
</div>
<div class="form-group">
<label for="topK">Top K Chunks <span class="char-count" style="float:none;">(max 20)</span></label>
<input type="number" id="topK" name="topK" value="5" min="1" max="20" />
</div>
<div class="button-row">
<button type="button" class="cancel-btn" id="cancelBtn">Clear</button>
<button type="submit" class="submit-btn">Search</button>
</div>
</form>
</div>
<div class="right-container">
<label for="response">Query Results</label>
<div id="response" class="output-section empty">
<span class="output-placeholder">Response will appear here...</span>
</div>
<div class="loading" id="loading">
<div class="spinner"></div>
<p>Running hybrid search...</p>
</div>
</div>
</div>
<div id="dangerZone" style="margin-top:28px; font-size:0.7rem; text-align:center; max-width:1200px; width:100%; opacity:0.65;">
<details>
<summary style="cursor:pointer;">Admin Only</summary>
<div style="margin-top:10px;">
<button id="resetBtn" style="background:#660000; color:#fff; border:1px solid #aa2222; padding:8px 14px; border-radius:6px; cursor:pointer; font-size:0.75rem; letter-spacing:0.5px;">Reset / Wipe Corpus</button>
<p style="margin-top:8px; line-height:1.3;">This sends a POST to <code>/api/reset</code> and irreversibly deletes all documents, uploaded files, chunks, and embeddings from Neon. Use only in development.</p>
</div>
</details>
</div>
<script>
// Character count for inputs
function updateCharCount(inputId, counterId) {
const input = document.getElementById(inputId);
const counter = document.getElementById(counterId);
input.addEventListener('input', () => counter.textContent = input.value.length);
counter.textContent = input.value.length;
}
updateCharCount('query', 'queryCount');
updateCharCount('docType', 'docTypeCount');
updateCharCount('jurisdiction', 'jurisdictionCount');
document.getElementById('cancelBtn').addEventListener('click', () => {
document.getElementById('query').value = '';
document.getElementById('docType').value = '';
document.getElementById('jurisdiction').value = '';
document.getElementById('topK').value = '5';
document.getElementById('queryCount').textContent = '0';
document.getElementById('docTypeCount').textContent = '0';
document.getElementById('jurisdictionCount').textContent = '0';
const responseDiv = document.getElementById('response');
responseDiv.innerHTML = '<span class="output-placeholder">Response will appear here...</span>';
responseDiv.classList.add('empty');
});
document.getElementById('queryForm').addEventListener('submit', async (e) => {
e.preventDefault();
const q = document.getElementById('query').value.trim();
const topK = parseInt(document.getElementById('topK').value, 10) || 5;
if (!q) return;
const filters = {};
const dt = document.getElementById('docType').value.trim();
const jur = document.getElementById('jurisdiction').value.trim();
if (dt) filters.doc_type = dt;
if (jur) filters.jurisdiction = jur;
const body = { query: q, top_k: topK, filters };
const responseDiv = document.getElementById('response');
const loadingDiv = document.getElementById('loading');
responseDiv.innerHTML = '';
responseDiv.classList.remove('empty');
loadingDiv.classList.add('active');
try {
const resp = await fetch('/api/query', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
});
const data = await resp.json().catch(() => ({}));
loadingDiv.classList.remove('active');
const escape = (str) => String(str ?? '')
.replace(/&/g,'&amp;')
.replace(/</g,'&lt;')
.replace(/>/g,'&gt;');
if (!resp.ok) {
const msg = data.error || resp.statusText || 'Request failed';
responseDiv.innerHTML = `<span style="color:#e53e3e;">Error: ${escape(msg)}</span>`;
return;
}
if (data.error) {
responseDiv.innerHTML = `<span style="color:#e53e3e;">Error: ${escape(data.error)}</span>`;
return;
}
const parts = [];
// Response section (from LLM)
let answerText = '';
let sources = [];
if (typeof data.response === 'string') {
answerText = data.response;
} else if (data.response && typeof data.response === 'object') {
if (typeof data.response.response === 'string') {
answerText = data.response.response;
}
if (Array.isArray(data.response.sources)) {
sources = data.response.sources;
}
}
if (!answerText && typeof data.answer === 'string') {
answerText = data.answer;
}
if (!sources.length && Array.isArray(data.sources)) {
sources = data.sources;
}
if (answerText || sources.length) {
let citationsHtml = '';
if (sources.length) {
const citeItems = sources.map((s) => {
const docId = s.doc_id ?? s.id ?? '';
const docType = s.doc_type ? ` (${escape(s.doc_type)})` : '';
const juris = s.jurisdiction ? ` [${escape(s.jurisdiction)}]` : '';
const chunks = Array.isArray(s.chunks) ? s.chunks : [];
const chunkItems = chunks.map((ch) => {
const sn = (ch.section_number != null && ch.section_number !== '') ? ` §${escape(ch.section_number)}` : '';
const st = ch.section_title ? ` — ${escape(ch.section_title)}` : '';
return `<li>chunk_id=${escape(ch.chunk_id)}${sn}${st}</li>`;
}).join('');
const inner = chunkItems ? `<ul>${chunkItems}</ul>` : '';
return `<li>Doc ${escape(String(docId))}${docType}${juris}${inner}</li>`;
}).join('');
citationsHtml = `<div class="citations"><em>Citations</em><ul>${citeItems}</ul></div>`;
}
parts.push(`
<div class="response-block">
<div class="section-title">Response</div>
<div class="response-text">${escape(answerText)}</div>
${citationsHtml}
</div>
`);
}
// Results section (hybrid retrieval hits)
const results = Array.isArray(data.results) ? data.results : [];
let resultsHtml = '';
if (!results.length) {
resultsHtml = '<em>No chunks matched the query and filters.</em>';
} else {
const list = results.map((r, idx) => {
const dist = (typeof r.distance === 'number') ? r.distance.toFixed(4) : 'n/a';
const txt = escape((r.text || '').slice(0, 500));
const doc = r.document || {};
const title = r.title || doc.title || '';
const dtype = r.doc_type || doc.doc_type || '';
const juris = r.jurisdiction || doc.jurisdiction || '';
const section = r.section_number ? ` §${escape(r.section_number)}` : '';
const heading = r.section_title ? ` — ${escape(r.section_title)}` : '';
const ranks = `vector=${escape(r.vector_rank ?? 'n/a')}, lexical=${escape(r.lexical_rank ?? 'n/a')}`;
return `<li style="margin-bottom:12px; line-height:1.35;">
<div><strong>#${idx+1}</strong> chunk_id=${escape(r.chunk_id)}${section}${heading}</div>
<div style="color:#bbb; font-size:0.7rem;">${escape(r.selection_reason || 'hybrid')} · ${ranks} · cosine distance=${dist}</div>
<div style="color:#ccc; font-size:0.7rem; margin-top:2px; font-style:italic;">${escape(title)} ${dtype ? '('+escape(dtype)+')' : ''} ${juris ? '['+escape(juris)+']' : ''}</div>
<div style="color:#ccc; font-size:0.85rem; margin-top:4px;">${txt}${r.text && r.text.length > 500 ? '…' : ''}</div>
</li>`;
}).join('');
resultsHtml = `<ol>${list}</ol>`;
}
parts.push(`
<div class="results-section">
<div class="section-title">Results</div>
${resultsHtml}
</div>
`);
responseDiv.innerHTML = parts.join('\n');
} catch (err) {
loadingDiv.classList.remove('active');
responseDiv.innerHTML = `<span style="color:#e53e3e;">Error: ${err.message}</span>`;
}
});
// Reset handler (optional password protected)
const resetBtn = document.getElementById('resetBtn');
if (resetBtn) {
resetBtn.addEventListener('click', async () => {
if (!confirm('This will DELETE all documents, files, chunks, and embeddings from Neon. Continue?')) return;
let password = null;
try {
password = prompt('Enter reset password (leave blank if not configured):');
} catch (e) { password = null; }
resetBtn.disabled = true;
resetBtn.textContent = 'Resetting...';
try {
const resp = await fetch('/api/reset', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ confirm: true, password })
});
const data = await resp.json().catch(() => ({}));
if (!resp.ok) {
const reason = data.error || resp.statusText || 'Unknown error';
if (resp.status === 403) {
alert('Reset denied: incorrect or missing password.');
} else {
alert('Reset failed: ' + reason);
}
} else {
alert('Reset complete. Files deleted: ' + (data.files_deleted ?? 'n/a'));
location.reload();
}
} catch (e) {
alert('Reset error: ' + e.message);
} finally {
resetBtn.disabled = false;
resetBtn.textContent = 'Reset / Wipe Corpus';
}
});
}
</script>
</body>
</html>