-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
385 lines (343 loc) · 10.9 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
const express = require("express");
const { createProxyMiddleware } = require('http-proxy-middleware');
const swaggerUi = require('swagger-ui-express');
const swaggerSpec = require('./swagger'); // Import the Swagger configuration
const cors = require("cors");
require("dotenv").config();
const app = express();
const port = 4444; // 4444 for local dev, 3000 for Docker
app.use(express.json());
app.use("/", express.static("build"));
app.use(cors());
// Serve the Swagger UI documentation
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
// app.get("/", function (req, res) {
// res.render("build/index.html");
// });
// Swagger for the /helth endpoint
/**
* @swagger
* /health:
* get:
* summary: Check the health of the server.
* responses:
* 200:
* description: Server is healthy.
*/
app.get("/health", function (req, res) {
res.send("OK");
});
// Swagger for the //new/data.feed endpoint
/**
* @swagger
* /new/data.feed:
* get:
* summary: Get data from new legislation feed.
* description: Fetch data from the new legislation feed at legislation.gov.uk.
* responses:
* 200:
* description: Successful response.
* 500:
* description: Internal server error.
*/
const proxyOptionsNewLegislation = {
target: 'https://www.legislation.gov.uk', // Target URL without /new/data.feed
changeOrigin: true,
// No need for pathRewrite in this case
onProxyReq: (proxyReq, req) => {
proxyReq.setHeader('grpc-timeout', '60S');
},
};
app.use('/new/data.feed', createProxyMiddleware(proxyOptionsNewLegislation));
/* Swagger documentation for the /chat/completions endpoint */
/**
* @swagger
* /chat/completions:
* post:
* summary: Get chat completions.
* description: Get chat completions from an external API.
* requestBody:
* content:
* application/json:
* schema:
* type: object
* properties:
* model:
* type: string
* messages:
* type: array
* temperature:
* type: number
* example:
* model: "meta-llama/Llama-2-70b-chat-hf"
* messages:
* - role: "system"
* content: "You are a helpful assistant."
* - role: "user"
* content: "I need help with my legal matter."
* temperature: 0.7
* responses:
* 200:
* description: Successful response
* 500:
* description: Internal server error
*/
// Proxy route for making the POST request
const proxyOptionsChat = {
target: `https://${process.env.llm_url}`,
changeOrigin: true,
pathRewrite: { '^/chat/completions': '/chat/completions' },
onProxyReq: (proxyReq, req) => {
proxyReq.setHeader("Content-Type", "application/json");
proxyReq.setHeader("Accept", "application/json");
proxyReq.setHeader("Authorization", `Bearer ${process.env.token}`);
proxyReq.setHeader("grpc-timeout", "60S");
if (req.body) {
const bodyData = JSON.stringify(req.body);
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
}
},
};
app.use('/chat/completions', createProxyMiddleware(proxyOptionsChat));
// Swagger for the /v1/query endpoint
/**
* @swagger
* /v1/query:
* post:
* summary: Proxy a request to an external service.
* description: Proxy a request to an external service with specific headers.
* requestBody:
* content:
* application/json:
* schema:
* type: object
* description: The request body data to be sent in the proxy request.
* properties:
* query:
* type: string
* description: The query to be sent to the external service.
* language:
* type: string
* description: The language of the query.
* sources:
* type: array
* items:
* type: string
* description: The sources to search.
* rerank:
* type: boolean
* description: Whether to rerank the results.
* rerank_num_results:
* type: number
* description: The number of results to rerank.
* mmr:
* type: boolean
* description: Whether to rerank the results using MMR.
* mmr_num_results:
* type: number
* description: The number of results to rerank using MMR.
* mmr_diversity_bias:
* type: number
* description: The diversity bias to use for MMR.
* hybrid_search_num_words:
* type: number
* description: The number of words to use for hybrid search.
* hybrid_search_lambda_long:
* type: number
* description: The lambda long to use for hybrid search.
* hybrid_search_lambda_short:
* type: number
* description: The lambda short to use for hybrid search.
* summary_num_results:
* type: number
* description: The number of results to use for summary.
* summary_num_sentences:
* type: number
* description: The number of sentences to use for summary.
* summary_default_language:
* type: string
* description: The default language to use for summary.
* summary_prompt_name:
* type: string
* description: The prompt name to use for summary.
* enable_source_filters:
* type: boolean
* description: Whether to enable source filters.
* example:
* query: [
* {
* query: "i need help with a legal matter pertaining to data in schools what legislation can help me?",
* start: 0,
* numResults: 10,
* contextConfig: {
* charsBefore: 30,
* charsAfter: 30,
* sentencesBefore: 3,
* sentencesAfter: 3,
* startTag: "<b>",
* endTag: "</b>"
* },
* corpusKey: [
* {
* customerId: 1236398232,
* corpusId: 3,
* semantics: "DEFAULT",
* dim: [
* {
* name: "string",
* weight: 0
* }
* ],
* metadataFilter: "part.lang = 'eng'",
* lexicalInterpolationConfig: {
* lambda: 0
* }
* }
* ],
* summary: [
* {
* summarizerPromptName: "vectara-summary-ext-v1.2.0",
* maxSummarizedResults: 3,
* responseLang: "eng"
* }
* ]
* }
* ]
* responses:
* 200:
* description: Successfully proxied the request.
* 500:
* description: Failed to proxy the request.
*/
const proxyOptions = {
target: `https://${process.env.endpoint}`,
changeOrigin: true,
pathRewrite: { '^/v1/query': '/v1/query' },
onProxyReq: (proxyReq, req) => {
proxyReq.setHeader("Content-Type", "application/json");
proxyReq.setHeader("Accept", "application/json");
proxyReq.setHeader("customer-id", process.env.customer_id);
proxyReq.setHeader("x-api-key", process.env.api_key);
proxyReq.setHeader("grpc-timeout", "60S");
if (req.body) {
const bodyData = JSON.stringify(req.body);
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
}
},
};
app.use('/v1/query', createProxyMiddleware(proxyOptions));
// app.post("/config", (req, res) => {
// const {
// // Search
// endpoint,
// corpus_id,
// customer_id,
// api_key,
// // everything after this point isn't setup yet
// // App
// ux,
// app_title,
// enable_app_header,
// enable_app_footer,
// // App header
// app_header_logo_link,
// app_header_logo_src,
// app_header_logo_alt,
// app_header_logo_height,
// app_header_learn_more_link,
// app_header_learn_more_text,
// // Filters
// enable_source_filters,
// all_sources,
// sources,
// // summary
// summary_default_language,
// summary_num_results,
// summary_num_sentences,
// summary_prompt_name,
// // hybrid search
// hybrid_search_num_words,
// hybrid_search_lambda_long,
// hybrid_search_lambda_short,
// // rerank
// rerank,
// rerank_num_results,
// // MMR
// mmr,
// mmr_num_results,
// mmr_diversity_bias,
// // Search header
// search_logo_link,
// search_logo_src,
// search_logo_alt,
// search_logo_height,
// search_title,
// search_description,
// search_placeholder,
// // Auth
// authenticate,
// google_client_id,
// // Analytics
// google_analytics_tracking_code,
// full_story_org_id,
// } = process.env;
// res.send({
// // Search
// endpoint,
// corpus_id,
// customer_id,
// api_key,
// // everything after this point isn't setup yet
// // App
// ux,
// app_title,
// enable_app_header,
// enable_app_footer,
// // App header
// app_header_logo_link,
// app_header_logo_src,
// app_header_logo_alt,
// app_header_logo_height,
// app_header_learn_more_link,
// app_header_learn_more_text,
// // Filters
// enable_source_filters,
// all_sources,
// sources,
// // summary
// summary_default_language,
// summary_num_results,
// summary_num_sentences,
// summary_prompt_name,
// // hybrid search
// hybrid_search_num_words,
// hybrid_search_lambda_long,
// hybrid_search_lambda_short,
// // rerank
// rerank,
// rerank_num_results,
// // MMR
// mmr,
// mmr_num_results,
// mmr_diversity_bias,
// // Search header
// search_logo_link,
// search_logo_src,
// search_logo_alt,
// search_logo_height,
// search_title,
// search_description,
// search_placeholder,
// // Auth
// authenticate,
// google_client_id,
// // Analytics
// google_analytics_tracking_code,
// full_story_org_id,
// });
// });
app.listen(port, () => {
console.log(`Example app listening at ${process.env.server_endpoint}`); // https://legisense-backend.onrender.com for deployment
});