generated from deepgram/oss-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2f6e98
commit 7e61801
Showing
9 changed files
with
200 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
chatlog.txt | ||
output-*.wav |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
const { writeFile, appendFile } = require("fs/promises"); | ||
const { createClient, AgentEvents } = require("../../dist/main/index"); | ||
const fetch = require("cross-fetch"); | ||
const { join } = require("path"); | ||
|
||
const deepgram = createClient(process.env.DEEPGRAM_API_KEY); | ||
|
||
const agent = async () => { | ||
let audioBuffer = Buffer.alloc(0); | ||
let i = 0; | ||
const url = "https://dpgr.am/spacewalk.wav"; | ||
const connection = deepgram.agent.live(); | ||
connection.on(AgentEvents.Open, async () => { | ||
console.log("Connection opened"); | ||
|
||
await connection.configure({ | ||
audio: { | ||
input: { | ||
encoding: "linear16", | ||
sampleRate: 44100, | ||
}, | ||
output: { | ||
encoding: "linear16", | ||
sampleRate: 16000, | ||
container: "wav", | ||
}, | ||
}, | ||
agent: { | ||
listen: { | ||
model: "nova-2", | ||
}, | ||
speak: { | ||
model: "aura-asteria-en", | ||
}, | ||
think: { | ||
provider: { | ||
type: "anthropic", | ||
}, | ||
model: "claude-3-haiku-20240307", | ||
}, | ||
}, | ||
}); | ||
console.log("Deepgram Agent configured."); | ||
|
||
setInterval(() => { | ||
console.log("Keep alive!"); | ||
void connection.keepAlive(); | ||
}, 5000); | ||
|
||
fetch(url) | ||
.then((r) => r.body) | ||
.then((res) => { | ||
res.on("readable", () => { | ||
connection.send(res.read()); | ||
}); | ||
}); | ||
}); | ||
|
||
connection.on(AgentEvents.Close, () => { | ||
console.log("Connection closed"); | ||
process.exit(0); | ||
}); | ||
|
||
connection.on(AgentEvents.ConversationText, async (data) => { | ||
await appendFile(join(__dirname, `chatlog.txt`), JSON.stringify(data) + "\n"); | ||
}); | ||
|
||
connection.on(AgentEvents.UserStartedSpeaking, () => { | ||
if (audioBuffer.length) { | ||
console.log("Interrupting agent."); | ||
audioBuffer = Buffer.alloc(0); | ||
} | ||
}); | ||
|
||
connection.on(AgentEvents.Metadata, (data) => { | ||
console.dir(data, { depth: null }); | ||
}); | ||
|
||
connection.on(AgentEvents.Audio, (data) => { | ||
// Concatenate the audio chunks into a single buffer | ||
const buffer = Buffer.from(data); | ||
audioBuffer = Buffer.concat([audioBuffer, buffer]); | ||
}); | ||
|
||
connection.on(AgentEvents.Error, (err) => { | ||
console.error("Error!"); | ||
console.error(err); | ||
console.error(err.message); | ||
}); | ||
|
||
connection.on(AgentEvents.AgentAudioDone, async () => { | ||
await writeFile(join(__dirname, `output-${i}.wav`), audioBuffer); | ||
audioBuffer = Buffer.alloc(0); | ||
i++; | ||
}); | ||
|
||
connection.on(AgentEvents.Unhandled, (data) => { | ||
console.dir(data, { depth: null }); | ||
}); | ||
}; | ||
|
||
void agent(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./AgentEvents"; | ||
export * from "./LiveConnectionState"; | ||
export * from "./LiveTranscriptionEvents"; | ||
export * from "./LiveTTSEvents"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { AbstractClient } from "./AbstractClient"; | ||
import { AgentLiveClient } from "./AgentLiveClient"; | ||
|
||
/** | ||
* The `AgentClient` class extends the `AbstractClient` class and provides access to the "agent" namespace. | ||
* It exposes one method: | ||
* | ||
* `live(ttsOptions: SpeakSchema = {}, endpoint = ":version/speak")`: Returns an `AgentLiveClient` instance for interacting with the voice agent API. | ||
*/ | ||
export class AgentClient extends AbstractClient { | ||
public namespace: string = "speak"; | ||
|
||
/** | ||
* Returns an `AgentLiveClient` instance for interacting with the voice agent API. | ||
* @param {string} [endpoint="/agent"] - The endpoint to use for the live speak API. | ||
* @returns {SpeakLiveClient} - A `SpeakLiveClient` instance for interacting with the live speak API. | ||
*/ | ||
public live(endpoint: string = "/agent"): AgentLiveClient { | ||
return new AgentLiveClient(this.options, endpoint); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters