Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
39 lines (31 loc) · 1.32 KB
/
Copy pathserver.ts
File metadata and controls
39 lines (31 loc) · 1.32 KB
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
// Server side of the call.
// It listens on a WebSocket and lets Micdrop orchestrate the three AI steps:
// transcribe what the user says, answer with an LLM, speak the answer out loud.
// https://micdrop.dev/docs/server
import { OpenaiAgent, OpenaiSTT, OpenaiTTS } from '@micdrop/openai'
import { MicdropServer } from '@micdrop/server'
import { WebSocketServer } from 'ws'
const PORT = 8085
const apiKey = process.env.OPENAI_API_KEY || ''
const server = new WebSocketServer({ port: PORT })
server.on('connection', (socket) => {
// One call per connection.
// https://micdrop.dev/docs/server/installation
new MicdropServer(socket, {
// Spoken right away, so the user hears a voice before the LLM has run.
// https://micdrop.dev/docs/server/first-message
firstMessage: 'Hi! What can I do for you?',
// The brain, an LLM that writes the answers.
// https://micdrop.dev/docs/agents/openai
agent: new OpenaiAgent({
apiKey,
systemPrompt:
'You are a friendly assistant. Your answers are spoken out loud, so keep them short and conversational.',
}),
// The ears, speech to text.
stt: new OpenaiSTT({ apiKey, language: 'en' }),
// The mouth, text to speech.
tts: new OpenaiTTS({ apiKey, voice: 'alloy' }),
})
})
console.log(`Micdrop server listening on ws://localhost:${PORT}`)