-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver-old.js
More file actions
90 lines (66 loc) · 2.24 KB
/
Copy pathserver-old.js
File metadata and controls
90 lines (66 loc) · 2.24 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
import Replicate from 'replicate';
import express from 'express';
import bodyParser from 'body-parser';
// Import EventSource for handling Server-Sent Events (SSE)
import EventSource from 'eventsource';
import * as dotenv from 'dotenv';
dotenv.config();
const app = express();
app.use(bodyParser.json());
app.use(express.static('public'));
const replicate = new Replicate({
auth: process.env.REPLICATE_API_TOKEN,
});
const model = 'meta/meta-llama-3-70b-instruct';
// console.log(replicate.userAgent);
app.post('/api/text', async (request, response) => {
const options = {
model,
Headers: {
'Content-Type': 'application/json'
},
input: {
prompt: request.body.prompt,
system_prompt: 'You are a speechless mountain who loves dad jokes. You only speak short sentences and always keep the conversation short.',
max_new_tokens: 128,
temperature: 0.85,
},
// Enable streaming
stream: true,
};
console.log(options);
const output = await replicate.run(model, { input });
response.json({ output });
// Create a prediction request to Replicate API
const prediction = await replicate.predictions.create(options);
// console.log(prediction);
// Check if streaming URL is available in the prediction response
if (prediction && prediction.urls && prediction.urls.stream) {
// Initialize EventSource for streaming
const source = new EventSource(prediction.urls.stream, {
withCredentials: true
});
// Handle 'output' event for streaming data to client
source.addEventListener('output', (e) => {
response.write(e.data);
});
// Handle 'error' event during streaming
source.addEventListener('error', (e) => {
console.error('error', e);
response.end();
});
// Handle 'done' event when streaming is complete
source.addEventListener('done', (e) => {
source.close();
console.log('done', e);
response.end();
});
} else {
// Send error response if streaming is not supported or prediction failed
response.status(500).send('Streaming not supported or prediction failed');
}
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});