-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (49 loc) · 1.54 KB
/
index.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
/**
* This example demonstrates how to use the OpenAI API to create a chatbot that
* can be used in a Copilot GitHub Application. It uses the `stream` parameter to stream
* the completions back to the client as they are generated.
*/
const { createReadStream } = require('fs')
const express = require('express')
const fetch = require('node-fetch')
const { log } = require('console')
const openaiApiKey = process.env.OPENAI_API_KEY;
const app = express()
app.use(express.json())
if (!openaiApiKey) {
console.error('OPENAI_API_KEY environment variable is not set');
process.exit(1);
}
app.get('/', (req, res) => {
// An endpoint should exist to receive the OAuth redirect from new GitHub users.
res.send(process.env.OPENAI_API_KEY);
})
app.post('/', async (req, res) => {
// This endpoint receives the chat messages from the client and forwards them to the OpenAI API.
const messages = req.body.messages.map(({ role, content }) => ({
role,
content
}))
const completions = await fetch(
'https://api.openai.com/v1/chat/completions',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${openaiApiKey}`,
},
body: JSON.stringify({
messages,
model: 'gpt-4',
stream: true
})
}
)
// Forward the completions from the OpenAI API to the client.
res.type('text/event-stream')
res.setHeader('Transfer-Encoding', 'chunked')
completions.body.pipe(res)
})
app.listen(80, () => {
console.log('Server started on port 80');
});