ausername-12345 commited on
Commit
0d1279b
·
1 Parent(s): 771ba78

Fix HF API: use model in URL path, switch to Phi-3, improve error handling

Browse files
Files changed (3) hide show
  1. .DS_Store +0 -0
  2. server.js +38 -33
  3. src/rememberit.jsx +1 -1
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
server.js CHANGED
@@ -10,63 +10,62 @@ const PORT = process.env.PORT || 7860;
10
  app.use(express.json({ limit: "1mb" }));
11
 
12
  async function searchDuckDuckGo(query) {
13
- const url = `https://html.duckduckgo.com/html/?q=${encodeURIComponent(query)}`;
14
- const res = await fetch(url, {
15
- headers: {
16
- "User-Agent":
17
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
18
- },
19
- });
20
- const html = await res.text();
21
- const $ = cheerio.load(html);
22
- const results = [];
23
- $(".result").each((_, el) => {
24
- const title = $(el).find(".result__title a").text().trim();
25
- const snippet = $(el).find(".result__snippet").text().trim();
26
- const link = $(el).find(".result__url").text().trim() || $(el).find(".result__title a").attr("href") || "";
27
- if (title) results.push({ title, snippet, link });
28
- });
29
- return results.slice(0, 6);
 
 
 
 
30
  }
31
 
32
  app.post("/api/hf", async (req, res) => {
33
  const token = process.env.HF_TOKEN;
34
  if (!token) {
35
- return res.status(500).json({ error: "HF_TOKEN not configured" });
36
  }
37
  try {
 
38
  const messages = req.body.messages || [];
39
  const lastUserMsg = messages.filter((m) => m.role === "user").at(-1);
40
  const userQuery = lastUserMsg?.content || "";
 
41
  const searchResults = await searchDuckDuckGo(userQuery);
42
  const searchContext =
43
  searchResults.length > 0
44
  ? "\n\nWeb search results:\n" +
45
  searchResults
46
- .map(
47
- (r, i) =>
48
- `${i + 1}. "${r.title}"\n ${r.snippet}\n URL: ${r.link}`,
49
- )
50
  .join("\n\n")
51
  : "";
52
 
53
  const body = {
54
- ...req.body,
 
55
  messages: [
56
  ...(messages[0]?.role === "system"
57
- ? [
58
- {
59
- ...messages[0],
60
- content: messages[0].content + searchContext,
61
- },
62
- ]
63
  : [{ role: "system", content: searchContext }]),
64
  ...messages.filter((m) => m.role !== "system"),
65
  ],
66
  };
67
 
68
- const response = await fetch(
69
- "https://api-inference.huggingface.co/v1/chat/completions",
70
  {
71
  method: "POST",
72
  headers: {
@@ -76,8 +75,14 @@ app.post("/api/hf", async (req, res) => {
76
  body: JSON.stringify(body),
77
  },
78
  );
79
- const data = await response.json();
80
- res.status(response.status).json(data);
 
 
 
 
 
 
81
  } catch (err) {
82
  res.status(500).json({ error: err.message });
83
  }
 
10
  app.use(express.json({ limit: "1mb" }));
11
 
12
  async function searchDuckDuckGo(query) {
13
+ try {
14
+ const url = `https://html.duckduckgo.com/html/?q=${encodeURIComponent(query)}`;
15
+ const res = await fetch(url, {
16
+ headers: {
17
+ "User-Agent":
18
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
19
+ },
20
+ });
21
+ const html = await res.text();
22
+ const $ = cheerio.load(html);
23
+ const results = [];
24
+ $(".result").each((_, el) => {
25
+ const title = $(el).find(".result__title a").text().trim();
26
+ const snippet = $(el).find(".result__snippet").text().trim();
27
+ const link = $(el).find(".result__url").text().trim() || $(el).find(".result__title a").attr("href") || "";
28
+ if (title) results.push({ title, snippet, link });
29
+ });
30
+ return results.slice(0, 6);
31
+ } catch {
32
+ return [];
33
+ }
34
  }
35
 
36
  app.post("/api/hf", async (req, res) => {
37
  const token = process.env.HF_TOKEN;
38
  if (!token) {
39
+ return res.status(500).json({ error: "HF_TOKEN not configured. Add it in Space Settings → Repository Secrets." });
40
  }
41
  try {
42
+ const model = req.body.model || "microsoft/Phi-3-mini-4k-instruct";
43
  const messages = req.body.messages || [];
44
  const lastUserMsg = messages.filter((m) => m.role === "user").at(-1);
45
  const userQuery = lastUserMsg?.content || "";
46
+
47
  const searchResults = await searchDuckDuckGo(userQuery);
48
  const searchContext =
49
  searchResults.length > 0
50
  ? "\n\nWeb search results:\n" +
51
  searchResults
52
+ .map((r, i) => `${i + 1}. "${r.title}"\n ${r.snippet}\n URL: ${r.link}`)
 
 
 
53
  .join("\n\n")
54
  : "";
55
 
56
  const body = {
57
+ model,
58
+ max_tokens: req.body.max_tokens || 1000,
59
  messages: [
60
  ...(messages[0]?.role === "system"
61
+ ? [{ ...messages[0], content: messages[0].content + searchContext }]
 
 
 
 
 
62
  : [{ role: "system", content: searchContext }]),
63
  ...messages.filter((m) => m.role !== "system"),
64
  ],
65
  };
66
 
67
+ const hfRes = await fetch(
68
+ `https://api-inference.huggingface.co/models/${model}/v1/chat/completions`,
69
  {
70
  method: "POST",
71
  headers: {
 
75
  body: JSON.stringify(body),
76
  },
77
  );
78
+
79
+ const data = await hfRes.json();
80
+ if (!hfRes.ok) {
81
+ return res.status(hfRes.status).json({
82
+ error: data.error || data.message || `HF API returned ${hfRes.status}`,
83
+ });
84
+ }
85
+ res.json(data);
86
  } catch (err) {
87
  res.status(500).json({ error: err.message });
88
  }
src/rememberit.jsx CHANGED
@@ -113,7 +113,7 @@ export default function RememberIt() {
113
  method: "POST",
114
  headers: { "Content-Type": "application/json" },
115
  body: JSON.stringify({
116
- model: "mistralai/Mistral-7B-Instruct-v0.3",
117
  max_tokens: 1000,
118
  messages: [
119
  { role: "system", content: SYSTEM_PROMPT },
 
113
  method: "POST",
114
  headers: { "Content-Type": "application/json" },
115
  body: JSON.stringify({
116
+ model: "microsoft/Phi-3-mini-4k-instruct",
117
  max_tokens: 1000,
118
  messages: [
119
  { role: "system", content: SYSTEM_PROMPT },