generated from deepgram/oss-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 74
feat: release voice agent #357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,110 @@ | ||
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); | ||
naomi-lgbt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const agent = async () => { | ||
let audioBuffer = Buffer.alloc(0); | ||
let i = 0; | ||
const url = "https://dpgr.am/spacewalk.wav"; | ||
const connection = deepgram.agent(); | ||
connection.on(AgentEvents.Welcome, () => { | ||
console.log("Welcome to the Deepgram Voice Agent!"); | ||
|
||
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: "open_ai", | ||
}, | ||
model: "gpt-4o-mini", | ||
jpvajda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
}); | ||
|
||
console.log("Deepgram agent configured!"); | ||
|
||
setInterval(() => { | ||
console.log("Keep alive!"); | ||
connection.keepAlive(); | ||
}, 5000); | ||
naomi-lgbt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
fetch(url) | ||
.then((r) => r.body) | ||
.then((res) => { | ||
res.on("readable", () => { | ||
console.log("Sending audio chunk"); | ||
connection.send(res.read()); | ||
}); | ||
}); | ||
naomi-lgbt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
connection.on(AgentEvents.Open, () => { | ||
console.log("Connection opened"); | ||
}); | ||
|
||
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"); | ||
}); | ||
naomi-lgbt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) => { | ||
console.log("Audio chunk received"); | ||
// 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(JSON.stringify(err, null, 2)); | ||
console.error(err.message); | ||
}); | ||
|
||
connection.on(AgentEvents.AgentAudioDone, async () => { | ||
console.log("Agent audio done"); | ||
await writeFile(join(__dirname, `output-${i}.wav`), audioBuffer); | ||
audioBuffer = Buffer.alloc(0); | ||
i++; | ||
}); | ||
naomi-lgbt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
connection.on(AgentEvents.Unhandled, (data) => { | ||
console.dir(data, { depth: null }); | ||
}); | ||
}; | ||
|
||
void agent(); |
This file contains hidden or 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 hidden or 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 hidden or 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,78 @@ | ||
export enum AgentEvents { | ||
/** | ||
* Built in socket events. | ||
*/ | ||
Open = "Open", | ||
Close = "Close", | ||
Error = "Error", | ||
/** | ||
* Audio event? | ||
*/ | ||
Audio = "Audio", | ||
/** | ||
* Confirms the successful connection to the websocket. | ||
* { type: "Welcome", session_id: "String"} | ||
*/ | ||
Welcome = "Welcome", | ||
/** | ||
* Confirms that your `configure` request was successful. | ||
* { type: "SettingsApplied" } | ||
*/ | ||
SettingsApplied = "SettingsApplied", | ||
/** | ||
* Triggered when the agent "hears" the user say something. | ||
* { type: "ConversationText", role: string, content: string } | ||
*/ | ||
ConversationText = "ConversationText", | ||
/** | ||
* Triggered when the agent begins receiving user audio. | ||
* { type: "UserStartedSpeaking" } | ||
*/ | ||
UserStartedSpeaking = "UserStartedSpeaking", | ||
/** | ||
* Triggered when the user has stopped speaking and the agent is processing the audio. | ||
* { type: "AgentThinking", content: string } | ||
*/ | ||
AgentThinking = "AgentThinking", | ||
/** | ||
* A request to call client-side functions. | ||
* { type: "FunctionCallRequest", function_call_id: string, function_name: string, input: Record<string, any> } | ||
*/ | ||
FunctionCallRequest = "FunctionCallRequest", | ||
/** | ||
* Debug message triggered when the agent is calling a function. | ||
* { type: "FunctionCalling" } | ||
*/ | ||
FunctionCalling = "FunctionCalling", | ||
/** | ||
* Triggered when the agent begins streaming an audio response. | ||
* { type: "AgentStartedSpeaking", total_latency: number, tts_latency: number, ttt_latency: number } | ||
*/ | ||
AgentStartedSpeaking = "AgentStartedSpeaking", | ||
/** | ||
* Triggered when the agent has finished streaming an audio response. | ||
* { type: "AgentAudioDone" } | ||
*/ | ||
AgentAudioDone = "AgentAudioDone", | ||
/** | ||
* This event is only emitted when you send an `InjectAgentMessage` request while | ||
* the user is currently speaking or the server is processing user audio. | ||
* { type: "InjectionRefused" } | ||
*/ | ||
InjectionRefused = "InjectionRefused", | ||
/** | ||
* A successful response to the `UpdateInstructions` request. | ||
* { type: "InstructionsUpdated" } | ||
*/ | ||
InstructionsUpdated = "InstructionsUpdated", | ||
/** | ||
* A successful response to the `UpdateSpeak` request. | ||
* { type: "SpeakUpdated" } | ||
*/ | ||
SpeakUpdated = "SpeakUpdated", | ||
|
||
/** | ||
* Catch all for any other message event | ||
*/ | ||
Unhandled = "Unhandled", | ||
} |
This file contains hidden or 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"; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.