Backup Agent commited on
Commit
bb096d9
·
1 Parent(s): a234b23

Implement research API endpoint with Exa AI and static fallback

Browse files
Files changed (1) hide show
  1. index.js +57 -0
index.js CHANGED
@@ -80,6 +80,63 @@ app.post("/messages", express.json(), async (req, res) => {
80
  }
81
  });
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  app.listen(PORT, () => {
84
  console.log(`SSE MCP Host listening on port ${PORT}`);
85
  });
 
80
  }
81
  });
82
 
83
+ app.post("/api/research", express.json(), async (req, res) => {
84
+ const { query } = req.body;
85
+ console.log(`[Library] Received research request for: '${query}'`);
86
+
87
+ const exaKey = process.env.EXA_API_KEY;
88
+ const nimKey = process.env.NVIDIA_NIM_API_KEY || process.env.NVIDIA_API_KEY || process.env.OPENAI_API_KEY;
89
+
90
+ if (exaKey && nimKey) {
91
+ try {
92
+ const exaRes = await fetch("https://api.exa.ai/search", {
93
+ method: "POST",
94
+ headers: {
95
+ "x-api-key": exaKey,
96
+ "Content-Type": "application/json"
97
+ },
98
+ body: JSON.stringify({
99
+ query: query,
100
+ numResults: 3,
101
+ text: true
102
+ })
103
+ });
104
+ const exaData = await exaRes.json();
105
+ const rawText = exaData.results ? exaData.results.map(r => r.text).join("\n") : "";
106
+
107
+ const nimRes = await fetch("https://integrate.api.nvidia.com/v1/chat/completions", {
108
+ method: "POST",
109
+ headers: {
110
+ "Authorization": `Bearer ${nimKey}`,
111
+ "Content-Type": "application/json"
112
+ },
113
+ body: JSON.stringify({
114
+ model: "stepfun-ai/step-3.7-flash",
115
+ messages: [
116
+ { role: "system", content: "You are a research summarizer. Compress the raw text into a strict 500-word markdown brief detailing novel formula structures, chemical symbols, or algorithms." },
117
+ { role: "user", content: rawText.substring(0, 30000) }
118
+ ],
119
+ max_tokens: 500
120
+ })
121
+ });
122
+ const nimData = await nimRes.json();
123
+ const brief = nimData.choices[0].message.content;
124
+ return res.json({ status: "success", brief: brief });
125
+ } catch (e) {
126
+ console.error(`[Library Warning] Exa/NIM search failed, falling back to static research: ${e.message}`);
127
+ }
128
+ }
129
+
130
+ let fallbackBrief = `### Stoichiometry & Chemical Calculator Roadmap
131
+ - **Molar Mass Calculator:** Computes molar mass of compounds using atomic weights (e.g. H=1.008, O=15.999). Formula parser handles parentheses: Ca(NO3)2.
132
+ - **Stoichiometry Solver:** Computes limiting reactant, theoretical yield, and percent yield for balanced equations (e.g. 2H2 + O2 -> 2H2O).
133
+ - **Gas Law Calculators:** Ideals gas law (PV=nRT), Charles Law, Boyles Law.
134
+ - **Solution Concentrations:** Molarity (M), Molality (m), Mass Percent.
135
+ - **Equation Balancer:** Direct linear algebra balancing using fraction results to prevent rounding issues.`;
136
+
137
+ return res.json({ status: "success", brief: fallbackBrief });
138
+ });
139
+
140
  app.listen(PORT, () => {
141
  console.log(`SSE MCP Host listening on port ${PORT}`);
142
  });