-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathstream-to-client-raw.ts
executable file
·57 lines (47 loc) · 1.49 KB
/
stream-to-client-raw.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
#!/usr/bin/env -S npm run tsn -T
// This file demonstrates how to stream from the server as a text/plain
// response with express and the stream async iterator.
import OpenAI from 'openai';
import express, { Request, Response } from 'express';
const openai = new OpenAI();
const app = express();
app.use(express.text());
// This endpoint can be called with:
//
// curl 127.0.0.1:3000 -N -X POST -H 'Content-Type: text/plain' \
// --data 'Can you explain why dogs are better than cats?'
//
// Or consumed with fetch:
//
// fetch('http://localhost:3000', {
// method: 'POST',
// body: 'Tell me why dogs are better than cats',
// }).then(async res => {
// const decoder = new TextDecoder();
// for await (const chunk of res.body) {
// console.log(`chunk: ${decoder.decode(chunk)}`);
// }
// })
//
app.post('/', async (req: Request, res: Response) => {
try {
console.log('Received request:', req.body);
const stream = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
stream: true,
messages: [{ role: 'user', content: req.body }],
});
res.header('Content-Type', 'text/plain');
// Sends each content stream chunk-by-chunk, such that the client
// ultimately receives a single string.
for await (const chunk of stream) {
res.write(chunk.choices[0]?.delta.content || '');
}
res.end();
} catch (e) {
console.error(e);
}
});
app.listen('3000', () => {
console.log('Started proxy express server');
});