diff --git a/db.py b/db.py index 1b84acc..38515de 100644 --- a/db.py +++ b/db.py @@ -123,6 +123,7 @@ def init_db(): CREATE TABLE IF NOT EXISTS messages ( id INTEGER PRIMARY KEY AUTOINCREMENT, conversation_id TEXT NOT NULL, role TEXT NOT NULL, content TEXT NOT NULL, created_at TEXT NOT NULL, + perplexity REAL, FOREIGN KEY (conversation_id) REFERENCES conversations(id) ON DELETE CASCADE ) """) @@ -164,6 +165,11 @@ def init_db(): except Exception: pass + try: + conn.execute("ALTER TABLE messages ADD COLUMN perplexity REAL") + except Exception: + pass + if not conn.execute("SELECT id FROM profile WHERE id = 1").fetchone(): now = datetime.now(timezone.utc).isoformat() conn.execute("INSERT INTO profile (id, content, updated_at) VALUES (1, ?, ?)", (DEFAULT_PROFILE, now)) diff --git a/routers/chat.py b/routers/chat.py index fcfd581..f03c996 100644 --- a/routers/chat.py +++ b/routers/chat.py @@ -114,8 +114,8 @@ async def chat(request: Request): else: db.execute("UPDATE conversations SET updated_at = ? WHERE id = ?", (now, conv_id)) - db.execute("INSERT INTO messages (conversation_id, role, content, created_at) VALUES (?, ?, ?, ?)", - (conv_id, "user", encrypt_text(user_message), now)) + db.execute("INSERT INTO messages (conversation_id, role, content, created_at, perplexity) VALUES (?, ?, ?, ?, ?)", + (conv_id, "user", encrypt_text(user_message), now, None)) db.commit() raw_rows = db.execute( @@ -218,8 +218,8 @@ async def chat(request: Request): saved_msg = remember_response + "\n\n" + saved_msg db2 = get_db() - db2.execute("INSERT INTO messages (conversation_id, role, content, created_at) VALUES (?, ?, ?, ?)", - (conv_id, "assistant", encrypt_text(saved_msg), datetime.now(timezone.utc).isoformat())) + db2.execute("INSERT INTO messages (conversation_id, role, content, created_at, perplexity) VALUES (?, ?, ?, ?, ?)", + (conv_id, "assistant", encrypt_text(saved_msg), datetime.now(timezone.utc).isoformat(), round(perplexity, 2))) db2.commit() db2.close() @@ -240,8 +240,8 @@ async def chat(request: Request): saved_msg = remember_response + "\n\n" + saved_msg db2 = get_db() - db2.execute("INSERT INTO messages (conversation_id, role, content, created_at) VALUES (?, ?, ?, ?)", - (conv_id, "assistant", encrypt_text(saved_msg), datetime.now(timezone.utc).isoformat())) + db2.execute("INSERT INTO messages (conversation_id, role, content, created_at, perplexity) VALUES (?, ?, ?, ?, ?)", + (conv_id, "assistant", encrypt_text(saved_msg), datetime.now(timezone.utc).isoformat(), round(perplexity, 2))) db2.commit() db2.close() diff --git a/routers/completions.py b/routers/completions.py index 3686873..8c197c4 100644 --- a/routers/completions.py +++ b/routers/completions.py @@ -132,8 +132,8 @@ async def chat_completions(request: Request): content = msg.get("content", "") if role in ("user", "assistant"): db.execute( - "INSERT INTO messages (conversation_id, role, content, created_at) VALUES (?, ?, ?, ?)", - (conv_id, role, encrypt_text(content), now), + "INSERT INTO messages (conversation_id, role, content, created_at, perplexity) VALUES (?, ?, ?, ?, ?)", + (conv_id, role, encrypt_text(content), now, None), ) db.commit() @@ -195,8 +195,8 @@ async def _stream_chat(payload: dict, model: str, conv_id: str, request: Request if assistant_msg: db = get_db() db.execute( - "INSERT INTO messages (conversation_id, role, content, created_at) VALUES (?, ?, ?, ?)", - (conv_id, "assistant", encrypt_text(assistant_msg), datetime.now(timezone.utc).isoformat()), + "INSERT INTO messages (conversation_id, role, content, created_at, perplexity) VALUES (?, ?, ?, ?, ?)", + (conv_id, "assistant", encrypt_text(assistant_msg), datetime.now(timezone.utc).isoformat(), None), ) db.commit() db.close() @@ -240,8 +240,8 @@ async def _blocking_chat(payload: dict, model: str, conv_id: str, request: Reque if assistant_msg: db = get_db() db.execute( - "INSERT INTO messages (conversation_id, role, content, created_at) VALUES (?, ?, ?, ?)", - (conv_id, "assistant", encrypt_text(assistant_msg), datetime.now(timezone.utc).isoformat()), + "INSERT INTO messages (conversation_id, role, content, created_at, perplexity) VALUES (?, ?, ?, ?, ?)", + (conv_id, "assistant", encrypt_text(assistant_msg), datetime.now(timezone.utc).isoformat(), None), ) db.commit() db.close() diff --git a/routers/search_route.py b/routers/search_route.py index 0dac8ed..2dfaca3 100644 --- a/routers/search_route.py +++ b/routers/search_route.py @@ -46,8 +46,8 @@ async def explicit_search(request: Request): else: db.execute("UPDATE conversations SET updated_at = ? WHERE id = ?", (now, conv_id)) - db.execute("INSERT INTO messages (conversation_id, role, content, created_at) VALUES (?, ?, ?, ?)", - (conv_id, "user", encrypt_text(query), now)) + db.execute("INSERT INTO messages (conversation_id, role, content, created_at, perplexity) VALUES (?, ?, ?, ?, ?)", + (conv_id, "user", encrypt_text(query), now, None)) db.commit() db.close() @@ -60,8 +60,8 @@ async def explicit_search(request: Request): error_msg = "No search results found." yield f"data: {json.dumps({'token': error_msg, 'conversation_id': conv_id})}\n\n" db2 = get_db() - db2.execute("INSERT INTO messages (conversation_id, role, content, created_at) VALUES (?, ?, ?, ?)", - (conv_id, "assistant", encrypt_text(error_msg), datetime.now(timezone.utc).isoformat())) + db2.execute("INSERT INTO messages (conversation_id, role, content, created_at, perplexity) VALUES (?, ?, ?, ?, ?)", + (conv_id, "assistant", encrypt_text(error_msg), datetime.now(timezone.utc).isoformat(), None)) db2.commit() db2.close() yield f"data: {json.dumps({'done': True, 'conversation_id': conv_id})}\n\n" @@ -102,8 +102,8 @@ async def explicit_search(request: Request): saved_msg = f"{summary}\n\n---\n*🔍 Web search results*" db2 = get_db() - db2.execute("INSERT INTO messages (conversation_id, role, content, created_at) VALUES (?, ?, ?, ?)", - (conv_id, "assistant", encrypt_text(saved_msg), datetime.now(timezone.utc).isoformat())) + db2.execute("INSERT INTO messages (conversation_id, role, content, created_at, perplexity) VALUES (?, ?, ?, ?, ?)", + (conv_id, "assistant", encrypt_text(saved_msg), datetime.now(timezone.utc).isoformat(), None)) db2.commit() db2.close() diff --git a/templates/index.html b/templates/index.html index 4e4f680..27c38a2 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1154,7 +1154,7 @@ async function loadConversation(convId) { const container = document.getElementById('chatContainer'); container.innerHTML = ''; conversationHistory = []; - data.messages.forEach(msg => { appendMessage(msg.role, msg.content, false); conversationHistory.push({ role: msg.role, content: msg.content }); }); + data.messages.forEach(msg => { appendMessage(msg.role, msg.content, false, false, null, msg.perplexity); conversationHistory.push({ role: msg.role, content: msg.content }); }); scrollToLatest(); await loadConversations(); } catch(e) {} @@ -1500,7 +1500,7 @@ function setStreamingState(streaming) { } } -function appendMessage(role, content, animate, isSearch = false, afterEl = null) { +function appendMessage(role, content, animate, isSearch = false, afterEl = null, perplexity = null) { const container = document.getElementById('chatContainer'); const div = document.createElement('div'); div.className = 'message ' + role + (isSearch && role === 'assistant' ? ' search-result' : ''); @@ -1511,7 +1511,17 @@ function appendMessage(role, content, animate, isSearch = false, afterEl = null) const timeStr = months[now.getMonth()] + ' ' + String(now.getDate()).padStart(2,'0') + ', ' + now.getFullYear() + ' ' + String(now.getHours()).padStart(2,'0') + ':' + String(now.getMinutes()).padStart(2,'0') + ':' + String(now.getSeconds()).padStart(2,'0') + '.' + String(Math.floor(ms / 10)).padStart(2,'0'); const textHtml = content ? renderMarkdown(content) : ''; const copyIcon = role === 'user' && content ? `📋` : ''; - div.innerHTML = `