Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 75 additions & 1 deletion serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,72 @@
_use_rerank = False
_stats = {"start_time": 0, "queries": 0, "avg_latency_ms": 0, "_latency_sum": 0}

HTML = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Knowledge Search</title>
<style>
body {
font-family: sans-serif;
margin: 20px;
}
.result {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
}
.score {
font-weight: bold;
}
.route {
font-style: italic;
}
</style>
</head>
<body>
<h1>Knowledge Search</h1>
<form id="search-form">
<input type="text" id="query" name="q" placeholder="Enter your search query">
<button type="submit">Search</button>
</form>
<div id="results"></div>

<script>
const form = document.getElementById('search-form');
const resultsDiv = document.getElementById('results');

form.addEventListener('submit', async (event) => {
event.preventDefault();
const query = document.getElementById('query').value;

const response = await fetch(`/search?q=${query}`);
const data = await response.json();

resultsDiv.innerHTML = '';
if (data.results && data.results.length > 0) {
data.results.forEach(result => {
const resultDiv = document.createElement('div');
resultDiv.className = 'result';
resultDiv.innerHTML = `
<div class="score">Score: ${result.signal_score.toFixed(3)}</div>
<div class="route">Route: ${result.route}</div>
<div>Title: ${result.title}</div>
<div>Core Insight: ${result.core_insight}</div>
`;
resultsDiv.appendChild(resultDiv);
});
} else {
resultsDiv.innerHTML = '<p>No results found.</p>';
}
});
</script>
</body>
</html>
"""


def _reload():
"""Load/reload embeddings from DB."""
Expand Down Expand Up @@ -66,9 +132,17 @@ def do_GET(self):
elif path == "/reload":
_reload()
self._json_response({"status": "reloaded", "items": len(_rows)})
elif path == "/":
self._html_response()
else:
self._json_response({"error": "not found"}, status=404)

def _html_response(self):
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
self.wfile.write(HTML.encode())

def _handle_search(self, params):
q = params.get("q", [""])[0]
if not q:
Expand Down Expand Up @@ -162,4 +236,4 @@ def main():


if __name__ == "__main__":
main()
main()