-
Notifications
You must be signed in to change notification settings - Fork 39
/
server.js
119 lines (101 loc) · 2.86 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
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
119
const envConfig = require('dotenv').config()
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const multer = require('multer')
const upload = multer()
const port = 3000
const app = express()
app.use(cors())
app.use(bodyParser.json())
////// OpenAI config //////
const { OpenAI } = require('openai')
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
})
// OpenAI chat completion
app.post('/chat', async (req, res) => {
const messages = req.body.messages
console.log(messages)
try {
if (messages == null) {
throw new Error('We have a problem - no prompt was provided')
}
const response = await client.chat.completions.create({
model: 'gpt-3.5-turbo',
messages
})
const completion = response.choices[0].message
console.dir(response.choices[0], { depth: 4 })
return res.status(200).json({
success: true,
message: completion
})
} catch (error) {
console.log(error.message)
}
})
////// Deepgram config //////
const { createClient } = require('@deepgram/sdk')
const deepgram = createClient(process.env.DG_API)
// Deepgram transcription
app.post('/dg-transcription', upload.single('file'), async (req, res) => {
try {
console.log(req.file)
if (!req.file) {
return res.status(400).send('No file uploaded')
}
const audioBuffer = req.file.buffer
const response = await deepgram.listen.prerecorded.transcribeFile(audioBuffer, {
punctuate: true,
model: 'nova-2'
})
console.dir(response, { depth: 4 })
res.send({ transcript: response.result })
} catch (e) {
console.error('Error:', e)
res.status(500).send('An error occurred during transcription')
}
})
////// Replicate config //////
const Replicate = require('replicate')
const replicate = new Replicate({
auth: process.env.REPLICATE
})
const miniGPT =
'daanelson/minigpt-4:e447a8583cffd86ce3b93f9c2cd24f2eae603d99ace6afa94b33a08e94a3cd06'
// Replicate (minigpt) image analyzer
app.post('/minigpt', async (req, res) => {
try {
const miniGPTResponse = await replicate.run(miniGPT, {
input: {
image: req.body.image,
prompt: req.body.prompt
}
})
res.send({ message: miniGPTResponse })
} catch (e) {
console.log('error', e)
}
})
////// Token Counter //////
const { encode } = require('gpt-3-encoder')
// Token Counter
app.post('/tokenize', async (req, res) => {
const str = req.body.stringToTokenize
try {
if (str == null) {
throw new Error('No string was provided')
}
const encoded = encode(str)
const length = encoded.length
console.log('Token count is ' + length)
return res.status(200).json({
success: true,
tokens: length
})
} catch (error) {
console.log(error.message)
}
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))