-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (63 loc) · 2.47 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
///*
// * Copyright (C) 2024 NPC Worldwide, LLC
//*
//* This file is part of the Open Web Copilot project.
//*
//* Open Web Copilot is free software: you can redistribute it and/or modify
//* it under the terms of the GNU Lesser General Public License as published by
//* the Free Software Foundation, either version 3 of the License, or
//* (at your option) any later version.
//*
//* Open Web Copilot is distributed in the hope that it will be useful,
//* but WITHOUT ANY WARRANTY; without even the implied warranty of
//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//* GNU Lesser General Public License for more details.
//*
//* You should have received a copy of the GNU Lesser General Public License
//* along with Open Web Copilot. If not, see <https://www.gnu.org/licenses/>.
//*/
import express from 'express';
import fetch from 'node-fetch';
import cors from 'cors';
import dotenv from 'dotenv';
console.log('Starting server...');
dotenv.config();
console.log('Environment variables loaded.');
const app = express();
const port = 3333;
console.log('Express app created.');
app.use(cors());
app.use(express.json());
console.log('Middleware set up.');
app.post('/generate', async (req, res) => {
console.log('Received request to /generate');
const { context } = req.body;
console.log('Received context:', context);
try {
console.log('Sending request to Ollama...');
const ollamaResponse = await fetch('http://localhost:11434/api/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: process.env.MODEL_NAME,
prompt: 'What will follow on a new line is a piece of text that you must continue. Do not provide any additional information. Reply with only the text that you generate that continues the text. Content: \n ' + context + '. ',
stream: false
})
});
console.log('Received response from Ollama');
const data = await ollamaResponse.json();
console.log('Generated text:', data.response);
res.json({ generatedText: data.response });
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Failed to generate text' });
}
});
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});