-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
89 lines (76 loc) · 2.66 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
// server.js
const express = require('express');
const cors = require('cors');
const axios = require('axios');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));
const OLLAMA_API_URL = process.env.OLLAMA_API_URL || 'http://host.docker.internal:11434';
// Ollama routes
app.get('/ollama/api/tags', async (req, res) => {
try {
const response = await axios.get(`${OLLAMA_API_URL}/api/tags`);
res.json(response.data);
} catch (error) {
console.error('Error fetching Ollama tags:', error.message);
res.status(500).json({ error: 'Failed to fetch Ollama tags' });
}
});
app.post('/ollama/api/generate', async (req, res) => {
try {
const response = await axios.post(`${OLLAMA_API_URL}/api/generate`, req.body);
res.json(response.data);
} catch (error) {
console.error('Error generating Ollama response:', error.message);
res.status(500).json({ error: 'Failed to generate Ollama response' });
}
});
const ANTHROPIC_MODEL = process.env.ANTHROPIC_MODEL || "claude-3-5-sonnet-20240620";
app.post('/api/anthropic', async (req, res) => {
if (!process.env.ANTHROPIC_API_KEY) {
return res.status(400).json({ error: 'Anthropic API key is not configured' });
}
try {
const { prompt, imageData, context } = req.body;
let messages;
if (imageData) {
messages = [
{
role: "user",
content: [
{ type: "text", text: context + "\n" + prompt },
{ type: "image", source: { type: "base64", media_type: "image/jpeg", data: imageData.split(',')[1] } }
]
}
];
} else {
messages = [
{ role: "user", content: context + "\n" + prompt }
];
}
const response = await axios.post('https://api.anthropic.com/v1/messages', {
model: ANTHROPIC_MODEL,
max_tokens: 1024,
messages: messages,
}, {
headers: {
'x-api-key': `${process.env.ANTHROPIC_API_KEY}`,
'anthropic-version': '2023-06-01',
'Content-Type': 'application/json',
}
});
res.json(response.data);
} catch (error) {
console.error('Error with Anthropic API:', error.response ? error.response.data : error.message);
res.status(500).json({ error: 'An error occurred while processing your request', details: error.message });
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
console.log('Anthropic Model:', ANTHROPIC_MODEL);
console.log('API Keys loaded:');
console.log('- Anthropic:', process.env.ANTHROPIC_API_KEY ? 'Set' : 'Not set');
});