-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
124 lines (110 loc) · 3.04 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
120
121
122
123
124
import express from 'express'
import cors from 'cors'
import bodyParser from 'body-parser'
import multer from 'multer'
import dotenv from 'dotenv'
dotenv.config()
const upload = multer()
const port = 8080
const app = express()
app.use(cors())
app.use(bodyParser.json())
////// OpenAI config //////
import { OpenAIApi, Configuration } from 'openai'
// const { Configuration, OpenAIApi } = require('openai')
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY
})
const openai = new OpenAIApi(configuration)
// 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 openai.createChatCompletion({
model: 'gpt-3.5-turbo',
messages
})
const completion = response.data.choices[0].message
console.dir(response.data.choices[0])
return res.status(200).json({
success: true,
message: completion
})
} catch (error) {
console.log(error.message)
}
})
////// Deepgram config //////
// const { Deepgram } = require('@deepgram/sdk')
import pkg from '@deepgram/sdk'
const { Deepgram } = pkg
const deepgram = new Deepgram(process.env.DG_API)
// Deepgram transcription
app.post('/dg-transcription', upload.single('file'), async (req, res) => {
try {
console.log(req.file)
const dgResponse = await deepgram.transcription.preRecorded(
{
buffer: req.file.buffer,
mimetype: req.file.mimetype
},
{
punctuate: true,
model: 'nova'
}
)
console.dir(dgResponse.results, { depth: 4 })
res.send({ transcript: dgResponse })
} catch (e) {
console.log('error', e)
}
})
////// Replicate config //////
// const Replicate = require('replicate')
import Replicate from 'replicate'
const replicate = new Replicate({
auth: process.env.REPLICATE
})
const miniGPT =
'daanelson/minigpt-4:b96a2f33cc8e4b0aa23eacfce731b9c41a7d9466d9ed4e167375587b54db9423'
// 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')
import { encode } from '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.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))