Spaces:
Sleeping
Sleeping
Download src/api.js from DeepThoughtAI/prolog_llm: direct link, hf CLI and curl.
- Browser
- Download file 11.7 kB
-
https://huggingface.co/spaces/DeepThoughtAI/prolog_llm/resolve/740d566ecde8ea401b433ecff0e1a52db7da7ebf/src/api.js
- Command line
-
hf download hf://spaces/DeepThoughtAI/prolog_llm@740d566ecde8ea401b433ecff0e1a52db7da7ebf/src/api.js
-
curl -L -o api.js https://huggingface.co/spaces/DeepThoughtAI/prolog_llm/resolve/740d566ecde8ea401b433ecff0e1a52db7da7ebf/src/api.js
11.7 kB
| // src/api.js | |
| export const API_BASE = "https://deepthoughtai-prolog-gateway.hf.space"; | |
| export async function login(username, password) { | |
| const res = await fetch(`${API_BASE}/login`, { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ username, password }), | |
| }); | |
| let json; | |
| const text = await res.text().catch(() => ""); | |
| try { | |
| json = text ? JSON.parse(text) : {}; | |
| } catch { | |
| json = {}; | |
| } | |
| // HTTP not OK | |
| if (!res.ok) { | |
| const msg = | |
| (json && json.error) || | |
| text || | |
| `Login failed with status ${res.status}`; | |
| throw new Error(msg); | |
| } | |
| // HTTP OK but backend says ok: false | |
| if (!json.ok || !json.token) { | |
| throw new Error(json.error || "Login failed"); | |
| } | |
| return json; // { ok, token } | |
| } | |
| export async function sendChat(message, strictMode = true, history = [], token, sessionId = null) { | |
| const res = await fetch(`${API_BASE}/chat`, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: `Bearer ${token}`, // 🟢 send JWT | |
| }, | |
| body: JSON.stringify({ | |
| message, | |
| strict_mode: strictMode, | |
| history, | |
| session_id: sessionId, | |
| }), | |
| }); | |
| if (!res.ok) { | |
| const text = await res.text().catch(() => ""); | |
| throw new Error(`HTTP ${res.status}: ${text || "Request failed"}`); | |
| } | |
| const json = await res.json(); | |
| if (!json.ok) { | |
| throw new Error(json.error || "Chat failed"); | |
| } | |
| return json; | |
| } | |
| export async function streamChat(message, strictMode, history, token, onToken, sessionId = null) { | |
| const res = await fetch(`${API_BASE}/chat/stream`, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| "Authorization": `Bearer ${token}` | |
| }, | |
| body: JSON.stringify({ | |
| message, | |
| strict_mode: strictMode, | |
| history, | |
| session_id: sessionId, | |
| }), | |
| }); | |
| if (!res.body) { | |
| throw new Error("Streaming not supported by server"); | |
| } | |
| const reader = res.body.getReader(); | |
| const decoder = new TextDecoder(); | |
| while (true) { | |
| const { value, done } = await reader.read(); | |
| if (done) break; | |
| const chunk = decoder.decode(value); | |
| onToken(chunk); | |
| } | |
| } | |
| export async function uploadPrologFile(prologCode, token) { | |
| const res = await fetch(`${API_BASE}/prolog/upload`, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: `Bearer ${token}`, | |
| }, | |
| body: JSON.stringify({ prolog_code: prologCode }), | |
| }); | |
| if (!res.ok) { | |
| const text = await res.text().catch(() => ""); | |
| throw new Error(`HTTP ${res.status}: ${text || "Upload failed"}`); | |
| } | |
| const json = await res.json(); | |
| if (!json.ok) { | |
| throw new Error(json.error || "Upload failed"); | |
| } | |
| return json; | |
| } | |
| export async function discoverMath(question, token, sessionId = null, nLemmas = 5, maxSamples = 250, stage = true) { | |
| const res = await fetch(`${API_BASE}/math/discover`, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: `Bearer ${token}`, | |
| }, | |
| body: JSON.stringify({ | |
| question, | |
| n_lemmas: nLemmas, | |
| max_samples: maxSamples, | |
| session_id: sessionId, | |
| stage, | |
| }), | |
| }); | |
| if (!res.ok) { | |
| const text = await res.text().catch(() => ""); | |
| throw new Error(`HTTP ${res.status}: ${text || "Request failed"}`); | |
| } | |
| const json = await res.json(); | |
| if (!json.ok) { | |
| throw new Error(json.error || "Math discovery failed"); | |
| } | |
| return json; | |
| } | |
| export async function listSessionLemmas(sessionId, token) { | |
| const res = await fetch(`${API_BASE}/session/lemmas`, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: `Bearer ${token}`, | |
| }, | |
| body: JSON.stringify({ session_id: sessionId }), | |
| }); | |
| if (!res.ok) { | |
| const text = await res.text().catch(() => ""); | |
| throw new Error(`HTTP ${res.status}: ${text || "Request failed"}`); | |
| } | |
| const json = await res.json(); | |
| if (!json.ok) { | |
| throw new Error(json.error || "List session lemmas failed"); | |
| } | |
| return json; | |
| } | |
| export async function listSessionDocs(sessionId, token) { | |
| const res = await fetch(`${API_BASE}/session/docs`, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: `Bearer ${token}`, | |
| }, | |
| body: JSON.stringify({ session_id: sessionId }), | |
| }); | |
| if (!res.ok) { | |
| const text = await res.text().catch(() => ""); | |
| throw new Error(`HTTP ${res.status}: ${text || "Request failed"}`); | |
| } | |
| const json = await res.json(); | |
| if (!json.ok) { | |
| throw new Error(json.error || "List session docs failed"); | |
| } | |
| return json; | |
| } | |
| export async function clearSession(sessionId, token) { | |
| const res = await fetch(`${API_BASE}/session/clear`, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: `Bearer ${token}`, | |
| }, | |
| body: JSON.stringify({ session_id: sessionId }), | |
| }); | |
| if (!res.ok) { | |
| const text = await res.text().catch(() => ""); | |
| throw new Error(`HTTP ${res.status}: ${text || "Request failed"}`); | |
| } | |
| const json = await res.json(); | |
| if (!json.ok) { | |
| throw new Error(json.error || "Clear session failed"); | |
| } | |
| return json; | |
| } | |
| export async function ingestArxivToSession(sessionId, token, arxivIdOrUrl, maxParagraphs = 200, skipMarkitdown = false) { | |
| const res = await fetch(`${API_BASE}/ingest/arxiv`, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: `Bearer ${token}`, | |
| }, | |
| body: JSON.stringify({ | |
| session_id: sessionId, | |
| arxiv_id: arxivIdOrUrl, | |
| max_paragraphs: maxParagraphs, | |
| skip_markitdown: skipMarkitdown, | |
| }), | |
| }); | |
| if (!res.ok) { | |
| const text = await res.text().catch(() => ""); | |
| throw new Error(`HTTP ${res.status}: ${text || "Request failed"}`); | |
| } | |
| const json = await res.json(); | |
| if (!json.ok) { | |
| throw new Error(json.error || "Ingest arXiv failed"); | |
| } | |
| return json; | |
| } | |
| export async function ingestStoryToSession(sessionId, token, url, storyId = "story", title = "", maxParagraphs = 200) { | |
| const res = await fetch(`${API_BASE}/ingest/story`, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: `Bearer ${token}`, | |
| }, | |
| body: JSON.stringify({ | |
| session_id: sessionId, | |
| url, | |
| story_id: storyId, | |
| title, | |
| max_paragraphs: maxParagraphs, | |
| }), | |
| }); | |
| if (!res.ok) { | |
| const text = await res.text().catch(() => ""); | |
| throw new Error(`HTTP ${res.status}: ${text || "Request failed"}`); | |
| } | |
| const json = await res.json(); | |
| if (!json.ok) { | |
| throw new Error(json.error || "Ingest story failed"); | |
| } | |
| return json; | |
| } | |
| export async function rollbackSessionPromotion(sessionId, token, deleteFile = false) { | |
| const res = await fetch(`${API_BASE}/session/rollback`, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: `Bearer ${token}`, | |
| }, | |
| body: JSON.stringify({ session_id: sessionId, delete_file: deleteFile }), | |
| }); | |
| if (!res.ok) { | |
| const text = await res.text().catch(() => ""); | |
| throw new Error(`HTTP ${res.status}: ${text || "Request failed"}`); | |
| } | |
| const json = await res.json(); | |
| if (!json.ok) { | |
| throw new Error(json.error || "Rollback failed"); | |
| } | |
| return json; | |
| } | |
| export async function exportSessionLemmas(sessionId, token, note = null) { | |
| const res = await fetch(`${API_BASE}/session/export`, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: `Bearer ${token}`, | |
| }, | |
| body: JSON.stringify({ session_id: sessionId, note }), | |
| }); | |
| if (!res.ok) { | |
| const text = await res.text().catch(() => ""); | |
| throw new Error(`HTTP ${res.status}: ${text || "Request failed"}`); | |
| } | |
| const json = await res.json(); | |
| if (!json.ok) { | |
| throw new Error(json.error || "Export session lemmas failed"); | |
| } | |
| return json; | |
| } | |
| export async function promoteSessionLemmas(sessionId, token, note = null, gateBenchmarks = true) { | |
| const res = await fetch(`${API_BASE}/session/promote`, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: `Bearer ${token}`, | |
| }, | |
| body: JSON.stringify({ | |
| session_id: sessionId, | |
| note, | |
| gate_benchmarks: gateBenchmarks, | |
| }), | |
| }); | |
| if (!res.ok) { | |
| const text = await res.text().catch(() => ""); | |
| throw new Error(`HTTP ${res.status}: ${text || "Request failed"}`); | |
| } | |
| const json = await res.json(); | |
| if (!json.ok) { | |
| throw new Error(json.error || "Promote session lemmas failed"); | |
| } | |
| return json; | |
| } | |
| export async function proveGoal(goal, token, maxDepth = 20, inferenceLimit = 50000) { | |
| const res = await fetch(`${API_BASE}/prolog/prove`, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: `Bearer ${token}`, | |
| }, | |
| body: JSON.stringify({ | |
| goal, | |
| max_depth: maxDepth, | |
| inference_limit: inferenceLimit, | |
| }), | |
| }); | |
| if (!res.ok) { | |
| const text = await res.text().catch(() => ""); | |
| throw new Error(`HTTP ${res.status}: ${text || "Request failed"}`); | |
| } | |
| const json = await res.json(); | |
| if (!json.ok) { | |
| throw new Error(json.error || "Prove goal failed"); | |
| } | |
| return json; | |
| } | |
| export async function getKBMetadata(token) { | |
| const res = await fetch(`${API_BASE}/prolog/metadata`, { | |
| method: "GET", | |
| headers: { | |
| Authorization: `Bearer ${token}`, | |
| }, | |
| }); | |
| if (!res.ok) { | |
| const text = await res.text().catch(() => ""); | |
| throw new Error(`HTTP ${res.status}: ${text || "Metadata fetch failed"}`); | |
| } | |
| const json = await res.json(); | |
| if (!json.ok) { | |
| throw new Error(json.error || "Metadata fetch failed"); | |
| } | |
| return json; | |
| } | |
| export async function queryProlog(query, token, maxSolutions = 10) { | |
| const res = await fetch(`${API_BASE}/prolog/query`, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: `Bearer ${token}`, | |
| }, | |
| body: JSON.stringify({ query, max_solutions: maxSolutions }), | |
| }); | |
| if (!res.ok) { | |
| const text = await res.text().catch(() => ""); | |
| throw new Error(`HTTP ${res.status}: ${text || "Query failed"}`); | |
| } | |
| const json = await res.json(); | |
| if (!json.ok) { | |
| throw new Error(json.error || "Query failed"); | |
| } | |
| return json; | |
| } | |
| export async function getMe(token) { | |
| const res = await fetch(`${API_BASE}/me`, { | |
| method: "GET", | |
| headers: { | |
| Authorization: `Bearer ${token}`, | |
| }, | |
| }); | |
| const text = await res.text().catch(() => ""); | |
| let json; | |
| try { | |
| json = text ? JSON.parse(text) : {}; | |
| } catch { | |
| json = {}; | |
| } | |
| if (!res.ok) { | |
| const msg = (json && json.error) || text || `HTTP ${res.status}`; | |
| throw new Error(msg); | |
| } | |
| if (!json.ok) { | |
| throw new Error(json.error || "Failed to fetch user info"); | |
| } | |
| return json; // { ok, username, is_admin } | |
| } | |
| export async function registerUser(username, password, token, overwrite = false) { | |
| const res = await fetch(`${API_BASE}/register`, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: `Bearer ${token}`, | |
| }, | |
| body: JSON.stringify({ username, password, overwrite }), | |
| }); | |
| const text = await res.text().catch(() => ""); | |
| let json; | |
| try { | |
| json = text ? JSON.parse(text) : {}; | |
| } catch { | |
| json = {}; | |
| } | |
| if (!res.ok) { | |
| const msg = (json && json.error) || text || `HTTP ${res.status}`; | |
| throw new Error(msg); | |
| } | |
| if (!json.ok) { | |
| throw new Error(json.error || "Register failed"); | |
| } | |
| return json; // { ok, username } | |
| } | |