-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (88 loc) · 2.76 KB
/
index.js
File metadata and controls
111 lines (88 loc) · 2.76 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* @file index.js
* @description Express server for AWS Bedrock API
*/
import express from 'express';
import crypto from 'crypto';
import cors from 'cors';
import helmet from 'helmet';
import morgan from 'morgan';
import { invokeBedrockAgent } from './agent.js';
import { invokeBedrockRetrieval } from './retrieval.js';
import { sanitize, sanitizeSessionID } from './utils.js';
import 'dotenv/config';
const app = express();
const port = 4000;
// Allow CORS requests from the specified CLIENT_HOST
const corsOptions = {
origin: [process.env.CLIENT_HOST],
};
app.use(cors(corsOptions));
// Set up Helmet for security
app.use(helmet());
// include before other routes
app.options('*', cors(corsOptions))
// Set up Morgan for logging
const loggingFormat = ':method :url :status :res[content-length] - :response-time ms';
app.use(morgan(loggingFormat));
// Middleware to parse JSON payload
app.use(express.json());
// API endpoint for AWS Bedrock agent
app.post('/agent', async (req, res) => {
const { message } = req.body;
// DEBUG:
console.log(message);
// Validate text payload
if (!message) {
return res.status(400).send({ error: 'Prompt is required' });
}
try {
// generate a unique session ID
const uuid = crypto.randomBytes(16).toString('hex');
// Call the AWS Bedrock API
const sanitizedPrompt = sanitize(message);
const response = await invokeBedrockAgent(sanitizedPrompt, uuid);
// Return the response
res.send(response.completion);
} catch (error) {
console.error(error);
res.status(500).send({ error: 'Failed to connect to external API' });
}
});
// API endpoint for AWS Bedrock retrieval and generation
app.post('/retrieve', async (req, res) => {
const { message, session_id } = req.body;
// DEBUG:
console.log('Prompt:', message);
// Validate text payload
if (!message) {
return res.status(400).send({ error: 'Prompt is required' });
}
try {
// Call the AWS Bedrock API
const sanitizedPrompt = sanitize(message);
const sanitizedSessionId = sanitizeSessionID(session_id);
const response = await invokeBedrockRetrieval(sanitizedPrompt, sanitizedSessionId);
// Return the response
res.json(response);
} catch (error) {
res.status(error?.code || 500).json(error?.message);
}
});
// API endpoint for readiness check
app.get('/health', async (_, res) => {
res.status(200).send({ status: 'OK' });
});
// Global error handler
app.use((err, req, res, next) => {
console.error(err);
res.status(500).send({ error: 'Internal Server Error' });
});
// 404 handler
app.use((req, res, next) => {
res.status(404).send({ error: 'Not Found' });
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
console.log(`Client host at ${process.env.CLIENT_PORT}`);
});