Spaces:
Sleeping
Sleeping
File size: 5,567 Bytes
eb63144 97ca577 52a0642 97ca577 52a0642 fe617ac 52a0642 fe617ac 65b86c6 97ca577 1a2c179 fe617ac 97ca577 1a2c179 d2570c2 fe617ac d2570c2 fe617ac |
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 |
const API_URL = import.meta.env.VITE_API_URL || (import.meta.env.PROD ? "" : "http://127.0.0.1:6006");
export async function recommend(query, category = "All", tone = "All", user_id = "local", use_agentic = false, fast = false, async_rerank = false) {
const body = { query, category, tone, user_id, use_agentic, fast, async_rerank };
const resp = await fetch(`${API_URL}/recommend`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!resp.ok) throw new Error(await resp.text());
const data = await resp.json();
return data.recommendations || [];
}
export async function getOnboardingBooks(limit = 24) {
const resp = await fetch(`${API_URL}/api/onboarding/books?limit=${limit}`);
if (!resp.ok) throw new Error(await resp.text());
const data = await resp.json();
return data.books || [];
}
export async function getPersonalizedRecommendations(user_id = "local", limit = 20, recent_isbns = null, intent_query = null) {
// P1: recent_isbns — session-level ISBNs for cold-start (1+ clicks)
// P2: intent_query — zero-shot intent probing when user has no history
const params = new URLSearchParams({ user_id, limit: limit.toString() });
if (recent_isbns && Array.isArray(recent_isbns) && recent_isbns.length > 0) {
params.set("recent_isbns", recent_isbns.join(","));
}
if (intent_query && typeof intent_query === "string" && intent_query.trim()) {
params.set("intent_query", intent_query.trim());
}
const resp = await fetch(`${API_URL}/api/recommend/personal?${params.toString()}`);
if (!resp.ok) throw new Error(await resp.text());
const data = await resp.json();
return data.recommendations || [];
}
export async function getSimilarBooks(isbn, k = 6, category = "All") {
const params = new URLSearchParams({ k: k.toString(), category });
const resp = await fetch(`${API_URL}/api/recommend/similar/${encodeURIComponent(isbn)}?${params.toString()}`);
if (!resp.ok) throw new Error(await resp.text());
const data = await resp.json();
return data.recommendations || [];
}
export async function addFavorite(isbn, userId = "local") {
const resp = await fetch(`${API_URL}/favorites/add`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ isbn, user_id: userId }),
});
if (!resp.ok) throw new Error(await resp.text());
return resp.json();
}
export async function getPersona(userId = "local") {
const resp = await fetch(`${API_URL}/user/${userId}/persona`);
if (!resp.ok) throw new Error(await resp.text());
return resp.json();
}
export async function getFavorites(userId = "local") {
const resp = await fetch(`${API_URL}/favorites/list/${userId}`);
if (!resp.ok) throw new Error(await resp.text());
const data = await resp.json();
return data.favorites || [];
}
export async function updateBook(isbn, updates, userId = "local") {
const resp = await fetch(`${API_URL}/favorites/update`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ isbn, user_id: userId, ...updates }),
});
if (!resp.ok) throw new Error(await resp.text());
return resp.json();
}
export async function removeFromFavorites(isbn, userId = "local") {
const resp = await fetch(`${API_URL}/favorites/remove`, {
method: "DELETE",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ isbn, user_id: userId }),
});
if (!resp.ok) throw new Error(await resp.text());
return resp.json();
}
export async function getUserStats(userId = "local") {
const resp = await fetch(`${API_URL}/user/${userId}/stats`);
if (!resp.ok) throw new Error(await resp.text());
return resp.json();
}
export async function getHighlights(isbn, userId = "local") {
const resp = await fetch(`${API_URL}/marketing/highlights`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ isbn, user_id: userId }),
});
if (!resp.ok) throw new Error(await resp.text());
return resp.json();
}
export async function streamChat({ isbn, query, apiKey, provider, onChunk, onError }) {
try {
const resp = await fetch(`${API_URL}/chat/completions`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-LLM-Key": apiKey || ""
},
body: JSON.stringify({
isbn,
query,
user_id: "local",
provider: provider || "ollama"
}),
});
if (!resp.ok) {
const errText = await resp.text();
throw new Error(errText || resp.statusText);
}
const reader = resp.body.getReader();
const decoder = new TextDecoder("utf-8");
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value, { stream: true });
if (chunk) onChunk(chunk);
}
} catch (e) {
if (onError) onError(e);
else console.error(e);
}
}
export async function addBook(bookData) {
const resp = await fetch(`${API_URL}/books/add`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(bookData),
});
if (!resp.ok) throw new Error(await resp.text());
return resp.json();
}
export async function searchGoogleBooks(query) {
const resp = await fetch(`https://www.googleapis.com/books/v1/volumes?q=${encodeURIComponent(query)}&maxResults=5`);
if (!resp.ok) throw new Error("Failed to search Google Books");
const data = await resp.json();
return data.items || [];
}
|