Spaces:
Sleeping
Sleeping
Update src/App.jsx
Browse files- src/App.jsx +56 -195
src/App.jsx
CHANGED
|
@@ -1,14 +1,10 @@
|
|
| 1 |
-
// src/App.jsx
|
| 2 |
import React, { useState } from "react";
|
| 3 |
import { sendChat } from "./api";
|
| 4 |
-
import "./App.css";
|
| 5 |
|
| 6 |
function App() {
|
| 7 |
const [messages, setMessages] = useState([
|
| 8 |
-
{
|
| 9 |
-
role: "assistant",
|
| 10 |
-
text: "Hi, I'm your Prolog + LLM assistant. Ask me something!",
|
| 11 |
-
},
|
| 12 |
]);
|
| 13 |
const [input, setInput] = useState("");
|
| 14 |
const [strictMode, setStrictMode] = useState(true);
|
|
@@ -17,37 +13,37 @@ function App() {
|
|
| 17 |
|
| 18 |
async function handleSend(e) {
|
| 19 |
if (e) e.preventDefault();
|
| 20 |
-
|
| 21 |
-
if (!trimmed || loading) return;
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
const userMsg = { role: "user", text: trimmed };
|
| 26 |
-
const currentMessages = [...messages, userMsg];
|
| 27 |
|
| 28 |
-
setMessages(
|
| 29 |
setInput("");
|
|
|
|
| 30 |
setLoading(true);
|
| 31 |
|
| 32 |
try {
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
role: m.role === "user" ? "user" : "assistant",
|
| 36 |
content: m.text,
|
| 37 |
}));
|
| 38 |
|
| 39 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
const assistantMsg = {
|
| 42 |
role: "assistant",
|
| 43 |
-
text:
|
| 44 |
-
|
| 45 |
};
|
| 46 |
|
| 47 |
-
setMessages(
|
| 48 |
} catch (err) {
|
| 49 |
-
|
| 50 |
-
setError(err.message || "Something went wrong");
|
| 51 |
} finally {
|
| 52 |
setLoading(false);
|
| 53 |
}
|
|
@@ -55,189 +51,54 @@ function App() {
|
|
| 55 |
|
| 56 |
return (
|
| 57 |
<div className="app-root">
|
| 58 |
-
<header className="
|
| 59 |
-
<
|
| 60 |
-
|
| 61 |
-
<
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
<input
|
| 69 |
-
type="checkbox"
|
| 70 |
-
checked={strictMode}
|
| 71 |
-
onChange={(e) => setStrictMode(e.target.checked)}
|
| 72 |
-
/>
|
| 73 |
-
<span>Strict KB mode</span>
|
| 74 |
-
</label>
|
| 75 |
-
</div>
|
| 76 |
</header>
|
| 77 |
|
| 78 |
-
<main className="
|
| 79 |
-
<
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
{loading && (
|
| 85 |
-
<div className="chat-message assistant">
|
| 86 |
-
<div className="bubble typing">
|
| 87 |
-
<span className="dot" />
|
| 88 |
-
<span className="dot" />
|
| 89 |
-
<span className="dot" />
|
| 90 |
-
</div>
|
| 91 |
-
</div>
|
| 92 |
-
)}
|
| 93 |
-
</div>
|
| 94 |
-
|
| 95 |
-
<form className="chat-input-row" onSubmit={handleSend}>
|
| 96 |
-
<textarea
|
| 97 |
-
rows={2}
|
| 98 |
-
placeholder="Ask me anything…"
|
| 99 |
-
value={input}
|
| 100 |
-
onChange={(e) => setInput(e.target.value)}
|
| 101 |
-
disabled={loading}
|
| 102 |
-
/>
|
| 103 |
-
<button type="submit" disabled={loading || !input.trim()}>
|
| 104 |
-
{loading ? "Thinking…" : "Send"}
|
| 105 |
-
</button>
|
| 106 |
-
</form>
|
| 107 |
-
|
| 108 |
-
{error && <div className="error-banner">Error: {error}</div>}
|
| 109 |
-
</section>
|
| 110 |
-
|
| 111 |
-
<section className="info-pane">
|
| 112 |
-
<h2>About</h2>
|
| 113 |
-
<p>
|
| 114 |
-
This UI sends your message to{" "}
|
| 115 |
-
<code>/chat</code> on the backend Space. The backend:
|
| 116 |
-
</p>
|
| 117 |
-
<ol>
|
| 118 |
-
<li>Queries Prolog for relevant facts</li>
|
| 119 |
-
<li>Builds a system prompt (strict or normal)</li>
|
| 120 |
-
<li>Calls the LLM once (no streaming)</li>
|
| 121 |
-
<li>Returns the answer + Prolog query metadata</li>
|
| 122 |
-
</ol>
|
| 123 |
-
<p>
|
| 124 |
-
Click{" "}
|
| 125 |
-
<span className="inline-tag">View Prolog info</span> on a reply to
|
| 126 |
-
inspect the query details from <code>prolog_query_info</code>.
|
| 127 |
-
</p>
|
| 128 |
-
</section>
|
| 129 |
-
</main>
|
| 130 |
-
</div>
|
| 131 |
-
);
|
| 132 |
-
}
|
| 133 |
-
|
| 134 |
-
// -----------------
|
| 135 |
-
// Message component
|
| 136 |
-
// -----------------
|
| 137 |
-
function ChatMessage({ message }) {
|
| 138 |
-
const isUser = message.role === "user";
|
| 139 |
-
const [showDebug, setShowDebug] = useState(false);
|
| 140 |
-
|
| 141 |
-
return (
|
| 142 |
-
<div className={`chat-message ${isUser ? "user" : "assistant"}`}>
|
| 143 |
-
<div className="avatar">{isUser ? "🧑" : "🤖"}</div>
|
| 144 |
-
<div className="message-content">
|
| 145 |
-
<div className="bubble">
|
| 146 |
-
<MarkdownText text={message.text} />
|
| 147 |
</div>
|
| 148 |
|
| 149 |
-
{
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
</button>
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
{!isUser && showDebug && message.prologInfo && (
|
| 160 |
-
<div className="debug-panel">
|
| 161 |
-
<DebugPrologInfo info={message.prologInfo} />
|
| 162 |
-
</div>
|
| 163 |
-
)}
|
| 164 |
-
</div>
|
| 165 |
</div>
|
| 166 |
);
|
| 167 |
}
|
| 168 |
|
| 169 |
-
|
| 170 |
-
// Simple Markdown-ish render
|
| 171 |
-
// ---------------------
|
| 172 |
-
function MarkdownText({ text }) {
|
| 173 |
-
// Very small markdown: **bold** and line breaks
|
| 174 |
-
const parts = text.split(/\n/g);
|
| 175 |
-
return (
|
| 176 |
-
<>
|
| 177 |
-
{parts.map((line, i) => (
|
| 178 |
-
<p key={i}>
|
| 179 |
-
{line.split(/(\*\*[^*]+\*\*)/g).map((chunk, j) => {
|
| 180 |
-
if (chunk.startsWith("**") && chunk.endsWith("**")) {
|
| 181 |
-
return (
|
| 182 |
-
<strong key={j}>{chunk.slice(2, chunk.length - 2)}</strong>
|
| 183 |
-
);
|
| 184 |
-
}
|
| 185 |
-
return <span key={j}>{chunk}</span>;
|
| 186 |
-
})}
|
| 187 |
-
</p>
|
| 188 |
-
))}
|
| 189 |
-
</>
|
| 190 |
-
);
|
| 191 |
-
}
|
| 192 |
-
|
| 193 |
-
// ---------------------
|
| 194 |
-
// Prolog debug viewer
|
| 195 |
-
// ---------------------
|
| 196 |
-
function DebugPrologInfo({ info }) {
|
| 197 |
-
const {
|
| 198 |
-
total_results,
|
| 199 |
-
predicates_queried = [],
|
| 200 |
-
queries_executed = [],
|
| 201 |
-
llm_analysis,
|
| 202 |
-
} = info;
|
| 203 |
-
|
| 204 |
return (
|
| 205 |
-
<div className=
|
| 206 |
-
<div className="
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
{predicates_queried.length > 0
|
| 214 |
-
? predicates_queried.join(", ")
|
| 215 |
-
: "None"}
|
| 216 |
-
</p>
|
| 217 |
-
</div>
|
| 218 |
-
|
| 219 |
-
{queries_executed.length > 0 && (
|
| 220 |
-
<div className="debug-section">
|
| 221 |
-
<h4>Queries executed</h4>
|
| 222 |
-
<ul>
|
| 223 |
-
{queries_executed.slice(0, 10).map((q, i) => (
|
| 224 |
-
<li key={i}>
|
| 225 |
-
<code>{q.query}</code> — {q.results_count} result
|
| 226 |
-
{q.results_count === 1 ? "" : "s"}
|
| 227 |
-
</li>
|
| 228 |
-
))}
|
| 229 |
-
</ul>
|
| 230 |
-
{queries_executed.length > 10 && (
|
| 231 |
-
<p>… and {queries_executed.length - 10} more</p>
|
| 232 |
-
)}
|
| 233 |
-
</div>
|
| 234 |
-
)}
|
| 235 |
-
|
| 236 |
-
{llm_analysis && (
|
| 237 |
-
<div className="debug-section">
|
| 238 |
-
<h4>LLM-generated queries (raw)</h4>
|
| 239 |
-
<pre className="llm-analysis">{llm_analysis}</pre>
|
| 240 |
-
</div>
|
| 241 |
)}
|
| 242 |
</div>
|
| 243 |
);
|
|
|
|
|
|
|
| 1 |
import React, { useState } from "react";
|
| 2 |
import { sendChat } from "./api";
|
| 3 |
+
import "./App.css";
|
| 4 |
|
| 5 |
function App() {
|
| 6 |
const [messages, setMessages] = useState([
|
| 7 |
+
{ role: "assistant", text: "Hello! Ask me anything." }
|
|
|
|
|
|
|
|
|
|
| 8 |
]);
|
| 9 |
const [input, setInput] = useState("");
|
| 10 |
const [strictMode, setStrictMode] = useState(true);
|
|
|
|
| 13 |
|
| 14 |
async function handleSend(e) {
|
| 15 |
if (e) e.preventDefault();
|
| 16 |
+
if (!input.trim() || loading) return;
|
|
|
|
| 17 |
|
| 18 |
+
const userMsg = { role: "user", text: input.trim() };
|
| 19 |
+
const newMessages = [...messages, userMsg];
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
setMessages(newMessages);
|
| 22 |
setInput("");
|
| 23 |
+
setError(null);
|
| 24 |
setLoading(true);
|
| 25 |
|
| 26 |
try {
|
| 27 |
+
const historyForBackend = newMessages.map((m) => ({
|
| 28 |
+
role: m.role,
|
|
|
|
| 29 |
content: m.text,
|
| 30 |
}));
|
| 31 |
|
| 32 |
+
const response = await sendChat(
|
| 33 |
+
userMsg.text,
|
| 34 |
+
strictMode,
|
| 35 |
+
historyForBackend
|
| 36 |
+
);
|
| 37 |
|
| 38 |
const assistantMsg = {
|
| 39 |
role: "assistant",
|
| 40 |
+
text: response.reply,
|
| 41 |
+
debug: response.prolog_query_info
|
| 42 |
};
|
| 43 |
|
| 44 |
+
setMessages([...newMessages, assistantMsg]);
|
| 45 |
} catch (err) {
|
| 46 |
+
setError(err.message || "Unknown error");
|
|
|
|
| 47 |
} finally {
|
| 48 |
setLoading(false);
|
| 49 |
}
|
|
|
|
| 51 |
|
| 52 |
return (
|
| 53 |
<div className="app-root">
|
| 54 |
+
<header className="header">
|
| 55 |
+
<h1>🧠 Prolog AI Chatbot</h1>
|
| 56 |
+
<label style={{ fontSize: "0.9rem" }}>
|
| 57 |
+
<input
|
| 58 |
+
type="checkbox"
|
| 59 |
+
checked={strictMode}
|
| 60 |
+
onChange={(e) => setStrictMode(e.target.checked)}
|
| 61 |
+
/>{" "}
|
| 62 |
+
Strict Mode
|
| 63 |
+
</label>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
</header>
|
| 65 |
|
| 66 |
+
<main className="main">
|
| 67 |
+
<div className="messages">
|
| 68 |
+
{messages.map((msg, idx) => (
|
| 69 |
+
<Message key={idx} message={msg} />
|
| 70 |
+
))}
|
| 71 |
+
{loading && <div className="assistant bubble">Thinking…</div>}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
</div>
|
| 73 |
|
| 74 |
+
{error && <div className="error">Error: {error}</div>}
|
| 75 |
+
|
| 76 |
+
<form className="input-row" onSubmit={handleSend}>
|
| 77 |
+
<textarea
|
| 78 |
+
placeholder="Type your question..."
|
| 79 |
+
value={input}
|
| 80 |
+
onChange={(e) => setInput(e.target.value)}
|
| 81 |
+
disabled={loading}
|
| 82 |
+
/>
|
| 83 |
+
<button type="submit" disabled={loading || !input.trim()}>
|
| 84 |
+
Send
|
| 85 |
</button>
|
| 86 |
+
</form>
|
| 87 |
+
</main>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
</div>
|
| 89 |
);
|
| 90 |
}
|
| 91 |
|
| 92 |
+
function Message({ message }) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
return (
|
| 94 |
+
<div className={`message ${message.role}`}>
|
| 95 |
+
<div className="bubble">{message.text}</div>
|
| 96 |
+
|
| 97 |
+
{message.role === "assistant" && message.debug && (
|
| 98 |
+
<details className="debug">
|
| 99 |
+
<summary>Show Prolog Debug</summary>
|
| 100 |
+
<pre>{JSON.stringify(message.debug, null, 2)}</pre>
|
| 101 |
+
</details>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
)}
|
| 103 |
</div>
|
| 104 |
);
|