-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
92 lines (76 loc) · 2.28 KB
/
server.js
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
import express from 'express';
import fetch from 'node-fetch';
import cors from 'cors';
import dotenv from 'dotenv';
dotenv.config();
const app = express();
app.use(cors());
app.use(express.json());
const JINA_BASE_URL = 'https://r.jina.ai/';
// Existing read endpoint
app.get('/read', async (req, res) => {
try {
const url = req.query.url;
if (!url) {
return res.status(400).json({ error: 'URL parameter is required' });
}
const response = await fetch(`${JINA_BASE_URL}${url}`, {
headers: {
'Authorization': `Bearer ${process.env.JINA_API_KEY}`
}
});
if (!response.ok) {
throw new Error(`Jina API responded with ${response.status}`);
}
const contentType = response.headers.get('content-type');
let data;
if (contentType && contentType.includes('application/json')) {
data = await response.json();
} else {
// Handle as text if not JSON
data = { text: await response.text() };
}
res.json(data);
} catch (error) {
console.error('Error in /read endpoint:', error);
res.status(500).json({ error: error.message });
}
});
// DeepSearch chat endpoint
app.post('/deepsearch/chat', async (req, res) => {
try {
const { messages, model = 'jina-deepsearch-v1', stream = false, reasoning_effort = 'medium' } = req.body;
const response = await fetch('https://deepsearch.jina.ai/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.JINA_API_KEY}`
},
body: JSON.stringify({
model,
messages,
stream,
reasoning_effort
})
});
if (!response.ok) {
throw new Error(`DeepSearch API responded with ${response.status}`);
}
// Handle streaming response
if (stream) {
res.setHeader('Content-Type', 'text/event-stream');
response.body.pipe(res);
return;
}
// Handle normal JSON response
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error in /deepsearch/chat endpoint:', error);
res.status(500).json({ error: error.message });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});