-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
71 lines (58 loc) · 2.43 KB
/
server.mjs
File metadata and controls
71 lines (58 loc) · 2.43 KB
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
import express from 'express';
import fetch from 'node-fetch';
import cors from 'cors';
const app = express();
// Use environment variables for PORT and API key
const PORT = process.env.PORT || 8080; // Railway will provide the PORT
const CLAUDE_API_KEY = process.env.CLAUDE_API_KEY; // Set this in Railway's environment variables
app.use(cors());
app.use(express.json());
app.post('/api/claude', async (req, res) => {
console.log('Request body:', req.body);
const prompt = req.body.prompt || req.body.question;
if (!prompt) {
console.error('Missing prompt or question in request body');
return res.status(400).json({ error: 'Missing prompt or question in request body' });
}
// Create the messages array with the user's input
const messages = [
{ role: 'user', content: prompt }
];
// Fix: Multi-line string properly formatted using backticks
const systemMessage = `
You are a conversational AI assistant. Provide brief, concise responses similar to human conversation.
Aim for responses of 1-2 short sentences. Be friendly but succinct.
If asked for your name, say that you are an AI assistant or just "Assistant", but do not mention the name "Claude".
`;
try {
const response = await fetch('https://api.anthropic.com/v1/messages', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': CLAUDE_API_KEY,
'anthropic-version': '2023-06-01',
},
body: JSON.stringify({
model: 'claude-3-opus-20240229', // Updated model version
messages: messages,
max_tokens: 150,
temperature: 0.7, // Optional: Controls randomness of responses
system: systemMessage
}),
});
const data = await response.json();
if (data.error) {
console.error('Claude API Error:', data.error);
return res.status(400).json({ error: data.error });
}
console.log('Claude API response:', data);
// Return the response from Claude API
res.json(data);
} catch (error) {
console.error('Error fetching from Claude API:', error.message);
res.status(500).json({ error: 'Failed to fetch from Claude API' });
}
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});