Spaces:
Running
Running
File size: 22,152 Bytes
fc3bf2a 14f16a2 fc3bf2a 14f16a2 fc3bf2a 14f16a2 fc3bf2a f74bbfd fc3bf2a 33d38c1 0ee86b0 fc3bf2a 34d9afb fc3bf2a 7b988b8 33d38c1 7b988b8 7a064ce 7b988b8 fc3bf2a f74bbfd 5a78d0a f74bbfd 34d9afb fc3bf2a f74bbfd fc3bf2a 34d9afb fc3bf2a 34d9afb fc3bf2a 33d38c1 34d9afb f74bbfd 34d9afb 33d38c1 fc3bf2a f74bbfd fc3bf2a f74bbfd fc3bf2a 7b988b8 33d38c1 7b988b8 7a064ce 33d38c1 7b988b8 33d38c1 7b988b8 33d38c1 7b988b8 fc3bf2a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | <!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,'&')
.replace(/</g,'<')
.replace(/>/g,'>');
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>
|