-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
43 lines (37 loc) · 1.12 KB
/
server.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
import * as express from 'express'
import { CohereClient } from "cohere-ai";
import { ChatCompletion } from 'openai/resources';
const app = express()
const port = 8080
app.use(express.json())
app.post('/openai/v1/chat/completions', async (req, res) => {
const cohere = new CohereClient({
token: req.headers.authorization.split(" ")[1],
});
const [{ message }, ...chatHistory] = req.body.messages.map(({ role, content }) => ({ role, message: content }))
const response = await cohere.chat({
message,
chatHistory
});
const openaiResponse: ChatCompletion = {
id: response.generationId,
object: 'chat.completion',
created: Date.now(),
model: req.body.model,
choices: [
{
message: {
role: 'assistant',
content: response.text
},
index: 0,
finish_reason: 'stop',
logprobs: null,
}
]
}
res.json(openaiResponse)
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})