forked from cybergolemai/iac_bedrock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (48 loc) · 1.82 KB
/
server.js
File metadata and controls
57 lines (48 loc) · 1.82 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
const express = require('express');
const { BedrockRuntimeClient, InvokeModelCommand } = require('@aws-sdk/client-bedrock-runtime');
const app = express();
const port = process.env.PORT || 80;
// Initialize Bedrock client
const bedrock = new BedrockRuntimeClient({
region: process.env.AWS_REGION || 'us-west-2'
});
app.use(express.json());
app.post('/invoke', async (req, res) => {
try {
const { prompt, model_id = 'meta.llama3-2-90b-instruct-v1:0', max_tokens = 4000, temperature = 0.7 } = req.body;
if (!prompt) {
return res.status(400).json({ error: 'prompt is required' });
}
// Prepare request for Bedrock with Llama-specific format and guardrail
const params = {
modelId: model_id,
contentType: "application/json",
accept: "application/json",
body: JSON.stringify({
promptArn: "arn:aws:bedrock:us-west-2:381492005022:prompt/4NLYS6J1L0",
guardrailArn: "arn:aws:bedrock:us-west-2:381492005022:guardrail/k6tcx8eogg3w",
prompt: prompt,
max_gen_len: max_tokens,
temperature: temperature,
top_p: 0.9
})
};
const command = new InvokeModelCommand(params);
const response = await bedrock.send(command);
const responseBody = JSON.parse(new TextDecoder().decode(response.body));
res.json({
completion: responseBody.generation,
model: model_id
});
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: error.message });
}
});
// Health check endpoint
app.get('/health', (req, res) => {
res.json({ status: 'healthy' });
});
app.listen(port, '0.0.0.0', () => {
console.log(`Server running on port ${port}`);
});