-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.ts
118 lines (97 loc) · 4.13 KB
/
server.ts
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
/*
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { APP_BASE_HREF } from '@angular/common';
import { CommonEngine } from '@angular/ssr';
import express from 'express';
import { fileURLToPath } from 'node:url';
import { dirname, join, resolve } from 'node:path';
import bootstrap from './src/main.server';
import {
GoogleGenerativeAI,
GenerateContentRequest,
} from '@google/generative-ai';
// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
const server = express();
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
const browserDistFolder = resolve(serverDistFolder, '../browser');
const indexHtml = join(serverDistFolder, 'index.server.html');
// Update this line to add your API_KEY if you are not using env variables
const GEMINI_API_KEY = process.env['API_KEY'] || '';
const commonEngine = new CommonEngine();
const genAI = new GoogleGenerativeAI(GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: 'gemini-1.5-pro-latest' });
model.generationConfig.responseMimeType = 'application/json';
const DEFAULT_LANG = 'spanish';
server.set('view engine', 'html');
server.set('views', browserDistFolder);
// Define Express Rest API endpoints
server.get('/api/ask-gemini', async (req, res) => {
if (!GEMINI_API_KEY) {
return res.status(500).send({
message: `Please provide an API key for Gemini, using default ${DEFAULT_LANG} translations.`,
});
}
let language = req.query['lang'] ? req.query['lang'] as string : DEFAULT_LANG;
try {
const prompt = `Your task is to generate 25 vocabulary words for learning ${language}.
Provide the ${language} translation, with 1 correct english translation and 2 incorrect english translations.
input: generate 25 vocaulary words for learning spanish
output: [{"phrase": "hola", "options"=["goodbye", "wait", "hello"], "answer": "hello"}, {"phrase": "adios", "options":["good morning", "see you later", "car"], "answer": "see you later"}, {"phrase": "gracias", "options":["shoes", "thank you", "comb"], "answer": "thank you"}]
`;
const request: GenerateContentRequest = {
contents: [{ role: 'user', parts: [{ text: prompt }] }],
tools: [],
};
const results = await model.generateContent(request);
return res
.status(200)
.send({ response: results.response.text(), usingDefault: false });
} catch (e) {
return res.status(500).send({
message: `Unable to generate the practice questions for ${language}, using default ${DEFAULT_LANG} translations.`,
});
}
});
// Serve static files from /browser
server.get(
'*.*',
express.static(browserDistFolder, {
maxAge: '1y',
})
);
// All regular routes use the Angular engine
server.get('*', (req, res, next) => {
const { protocol, originalUrl, baseUrl, headers } = req;
commonEngine
.render({
bootstrap,
documentFilePath: indexHtml,
url: `${protocol}://${headers.host}${originalUrl}`,
publicPath: browserDistFolder,
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
})
.then((html) => res.send(html))
.catch((err) => next(err));
});
return server;
}
function run(): void {
const port = process.env['PORT'] || process.argv[3] || 4000;
// Start up the Node server
const server = app();
server.listen(port, () => {
console.log(`Node Express server listening on http://localhost:${port}`);
});
}
run();