-
Notifications
You must be signed in to change notification settings - Fork 84
feat: support arbitrary attributes for speak provider #532
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -342,7 +342,6 @@ | |
Listen, | ||
ListenProvider, | ||
Speak, | ||
SpeakProvider, | ||
Header, | ||
Item, | ||
Properties, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -356,7 +356,6 @@ | |
Listen, | ||
ListenProvider, | ||
Speak, | ||
SpeakProvider, | ||
Header, | ||
Item, | ||
Properties, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -365,7 +365,6 @@ | |
Listen, | ||
ListenProvider, | ||
Speak, | ||
SpeakProvider, | ||
Header, | ||
Item, | ||
Properties, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,6 @@ | |
Listen, | ||
ListenProvider, | ||
Speak, | ||
SpeakProvider, | ||
Header, | ||
Item, | ||
Properties, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,6 @@ | |
Listen, | ||
ListenProvider, | ||
Speak, | ||
SpeakProvider, | ||
Header, | ||
Item, | ||
Properties, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,6 @@ | |
Listen, | ||
ListenProvider, | ||
Speak, | ||
SpeakProvider, | ||
Header, | ||
Item, | ||
Properties, | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,7 +2,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# SPDX-License-Identifier: MIT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from typing import List, Optional, Union, Any, Tuple | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from typing import List, Optional, Union, Any, Tuple, Dict | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import logging | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from dataclasses import dataclass, field | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -167,52 +167,6 @@ class ThinkProvider(BaseResponse): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@dataclass | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class SpeakProvider(BaseResponse): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This class defines the provider for the Speak model. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
type: Optional[str] = field(default="deepgram") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Deepgram OR OpenAI model to use. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
model: Optional[str] = field( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
default="aura-2-thalia-en", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
metadata=dataclass_config(exclude=lambda f: f is None), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ElevenLabs or Cartesia model to use. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
model_id: Optional[str] = field( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
default=None, metadata=dataclass_config(exclude=lambda f: f is None) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Cartesia voice configuration. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
voice: Optional[CartesiaVoice] = field( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
default=None, metadata=dataclass_config(exclude=lambda f: f is None) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Cartesia language. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
language: Optional[str] = field( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
default=None, metadata=dataclass_config(exclude=lambda f: f is None) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ElevenLabs language. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
language_code: Optional[str] = field( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
default=None, metadata=dataclass_config(exclude=lambda f: f is None) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def __getitem__(self, key): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_dict = self.to_dict() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if "voice" in _dict and isinstance(_dict["voice"], dict): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_dict["voice"] = CartesiaVoice.from_dict(_dict["voice"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return _dict[key] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@dataclass | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class Think(BaseResponse): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -264,15 +218,26 @@ class Speak(BaseResponse): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
This class defines any configuration settings for the Speak model. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
provider: SpeakProvider = field(default_factory=SpeakProvider) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
provider: dict = field(default_factory=dict) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
endpoint: Optional[Endpoint] = field( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
default=None, metadata=dataclass_config(exclude=lambda f: f is None) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def __post_init__(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Allow attribute-style access to provider dict | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class AttrDict(dict): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def __getattr__(self, name): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return self[name] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except KeyError: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise AttributeError(name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def __setattr__(self, name, value): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self[name] = value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if not isinstance(self.provider, AttrDict): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.provider = AttrDict(self.provider) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix linting issues in AttrDict implementation. The AttrDict implementation is clever and achieves the desired functionality, but there are linting issues that need to be addressed: def __post_init__(self):
- # Allow attribute-style access to provider dict
class AttrDict(dict):
+ """Dictionary that supports attribute-style access to keys."""
+
def __getattr__(self, name):
try:
return self[name]
except KeyError:
- raise AttributeError(name)
+ raise AttributeError(name) from None
def __setattr__(self, name, value):
self[name] = value
if not isinstance(self.provider, AttrDict):
self.provider = AttrDict(self.provider) 📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Actions: Check - lint[warning] 228-228: pylint: Missing class docstring (missing-class-docstring) [warning] 233-233: pylint: Consider explicitly re-raising using 'except KeyError as exc' and 'raise AttributeError(name) from exc' (raise-missing-from) 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def __getitem__(self, key): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_dict = self.to_dict() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if "provider" in _dict and isinstance(_dict["provider"], dict): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_dict["provider"] = SpeakProvider.from_dict(_dict["provider"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if "endpoint" in _dict and isinstance(_dict["endpoint"], dict): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_dict["endpoint"] = Endpoint.from_dict(_dict["endpoint"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return _dict[key] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# Copyright 2025 Deepgram SDK contributors. All Rights Reserved. | ||
# Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
# SPDX-License-Identifier: MIT | ||
|
||
# Import dependencies and set up the main function | ||
import requests | ||
import wave | ||
import io | ||
import time | ||
import os | ||
import json | ||
import threading | ||
from datetime import datetime | ||
|
||
from deepgram import ( | ||
DeepgramClient, | ||
DeepgramClientOptions, | ||
AgentWebSocketEvents, | ||
AgentKeepAlive, | ||
) | ||
from deepgram.clients.agent.v1.websocket.options import SettingsOptions | ||
|
||
def main(): | ||
try: | ||
# Initialize the Voice Agent | ||
api_key = os.getenv("DEEPGRAM_API_KEY") | ||
if not api_key: | ||
raise ValueError("DEEPGRAM_API_KEY environment variable is not set") | ||
print(f"API Key found:") | ||
|
||
# Initialize Deepgram client | ||
config = DeepgramClientOptions( | ||
options={ | ||
"keepalive": "true", | ||
# "speaker_playback": "true", | ||
}, | ||
) | ||
deepgram = DeepgramClient(api_key, config) | ||
connection = deepgram.agent.websocket.v("1") | ||
print("Created WebSocket connection...") | ||
|
||
# 4. Configure the Agent | ||
options = SettingsOptions() | ||
# Audio input configuration | ||
options.audio.input.encoding = "linear16" | ||
options.audio.input.sample_rate = 24000 | ||
# Audio output configuration | ||
options.audio.output.encoding = "linear16" | ||
options.audio.output.sample_rate = 24000 | ||
options.audio.output.container = "wav" | ||
# Agent configuration | ||
options.agent.language = "en" | ||
options.agent.listen.provider.type = "deepgram" | ||
options.agent.listen.provider.model = "nova-3" | ||
options.agent.think.provider.type = "open_ai" | ||
options.agent.think.provider.model = "gpt-4o-mini" | ||
options.agent.think.prompt = "You are a friendly AI assistant." | ||
options.agent.speak.provider.type = "deepgram" | ||
options.agent.speak.provider.model = "aura-2-thalia-en" | ||
options.agent.greeting = "Hello! How can I help you today?" | ||
options.agent.speak.provider.arbitrary_key = "test" | ||
|
||
def on_welcome(self, welcome, **kwargs): | ||
print(f"Welcome message received: {welcome}") | ||
with open("chatlog.txt", 'a') as chatlog: | ||
chatlog.write(f"Welcome message: {welcome}\n") | ||
|
||
def on_settings_applied(self, settings_applied, **kwargs): | ||
print(f"Settings applied: {settings_applied}") | ||
with open("chatlog.txt", 'a') as chatlog: | ||
chatlog.write(f"Settings applied: {settings_applied}\n") | ||
|
||
def on_error(self, error, **kwargs): | ||
print(f"Error received: {error}") | ||
with open("chatlog.txt", 'a') as chatlog: | ||
chatlog.write(f"Error: {error}\n") | ||
|
||
# Register handlers | ||
connection.on(AgentWebSocketEvents.Welcome, on_welcome) | ||
connection.on(AgentWebSocketEvents.SettingsApplied, on_settings_applied) | ||
connection.on(AgentWebSocketEvents.Error, on_error) | ||
print("Event handlers registered") | ||
|
||
# Start the connection | ||
print("Starting WebSocket connection...") | ||
print(options) | ||
if not connection.start(options): | ||
print("Failed to start connection") | ||
return | ||
print("WebSocket connection started successfully") | ||
|
||
# Cleanup | ||
connection.finish() | ||
print("Finished") | ||
|
||
except Exception as e: | ||
print(f"Error: {str(e)}") | ||
|
||
if __name__ == "__main__": | ||
main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of something along the lines of