Skip to content

Commit

Permalink
chore(ai_services): raise exception if api_key None or empty
Browse files Browse the repository at this point in the history
  • Loading branch information
vipyne committed Jan 21, 2025
1 parent 4f758c5 commit 287b6f7
Show file tree
Hide file tree
Showing 25 changed files with 34 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/pipecat/services/ai_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
class AIService(FrameProcessor):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._require_api_key: bool = True
self._api_key: str = ""
self._model_name: str = ""
self._settings: Dict[str, Any] = {}
self._session_properties: Dict[str, Any] = {}
Expand All @@ -61,7 +63,10 @@ def set_model_name(self, model: str):
self.set_core_metrics_data(MetricsData(processor=self.name, model=self._model_name))

async def start(self, frame: StartFrame):
pass
if self._require_api_key and not self._api_key:
raise Exception(f"{self}: Error: No api_key value found.")
else:
pass

async def stop(self, frame: EndFrame):
pass
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def __init__(
**kwargs,
):
super().__init__(**kwargs)
self._api_key = api_key
self._client = client or AsyncAnthropic(
api_key=api_key
) # if the client is provided, use it and remove it, otherwise create a new one
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/assemblyai.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(
super().__init__(**kwargs)

aai.settings.api_key = api_key
self._api_key = api_key
self._transcriber: aai.RealtimeTranscriber | None = None
# Store reference to the main event loop for use in callback functions
self._loop = asyncio.get_event_loop()
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def __init__(
**kwargs,
):
super().__init__(sample_rate=sample_rate, **kwargs)
self._api_key = api_key

self._polly_client = boto3.client(
"polly",
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ def __init__(
):
# Initialize variables before calling parent __init__() because that
# will call create_client() and we need those values there.
self._api_key = api_key
self._endpoint = endpoint
self._api_version = api_version
super().__init__(api_key=api_key, model=model, **kwargs)
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/cerebras.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(
**kwargs,
):
super().__init__(api_key=api_key, base_url=base_url, model=model, **kwargs)
self._api_key = api_key

def create_client(self, api_key=None, base_url=None, **kwargs):
"""Create OpenAI-compatible client for Cerebras API endpoint."""
Expand Down
2 changes: 2 additions & 0 deletions src/pipecat/services/deepgram.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(
):
super().__init__(sample_rate=sample_rate, **kwargs)

self._api_key = api_key
self._settings = {
"sample_rate": sample_rate,
"encoding": encoding,
Expand Down Expand Up @@ -148,6 +149,7 @@ def __init__(
):
merged_options.language = merged_options.language.value

self._api_key = api_key
self._settings = merged_options.to_dict()

self._client = DeepgramClient(
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/deepseek.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(
**kwargs,
):
super().__init__(api_key=api_key, base_url=base_url, model=model, **kwargs)
self._api_key = api_key

def create_client(self, api_key=None, base_url=None, **kwargs):
"""Create OpenAI-compatible client for DeepSeek API endpoint."""
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/fal.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(
self._aiohttp_session = aiohttp_session
if key:
os.environ["FAL_KEY"] = key
self._api_key = key

async def run_image_gen(self, prompt: str) -> AsyncGenerator[Frame, None]:
logger.debug(f"Generating image from prompt: {prompt}")
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/fireworks.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(
**kwargs,
):
super().__init__(api_key=api_key, base_url=base_url, model=model, **kwargs)
self._api_key = api_key

def create_client(self, api_key=None, base_url=None, **kwargs):
"""Create OpenAI-compatible client for Fireworks API endpoint."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class AudioTranscriber:
def __init__(self, api_key, model="gemini-2.0-flash-exp"):
gai.configure(api_key=api_key)
self.api_key = api_key
self._api_key = api_key
self.model = model

self._client = None
Expand Down
4 changes: 2 additions & 2 deletions src/pipecat/services/gemini_multimodal_live/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ def __init__(
**kwargs,
):
super().__init__(base_url=base_url, **kwargs)
self.api_key = api_key
self.base_url = base_url
self.set_model_name(model)
self._api_key = api_key
self._voice_id = voice_id

self._system_instruction = system_instruction
Expand Down Expand Up @@ -384,7 +384,7 @@ async def _connect(self):
# handle disconnections in the send/recv code paths.
return

uri = f"wss://{self.base_url}/ws/google.ai.generativelanguage.v1alpha.GenerativeService.BidiGenerateContent?key={self.api_key}"
uri = f"wss://{self.base_url}/ws/google.ai.generativelanguage.v1alpha.GenerativeService.BidiGenerateContent?key={self._api_key}"
logger.info(f"Connecting to {uri}")
self._websocket = await websockets.connect(uri=uri)
self._receive_task = self.get_event_loop().create_task(self._receive_task_handler())
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ def __init__(
super().__init__(**kwargs)
gai.configure(api_key=api_key)
self.set_model_name(model)
self._api_key = api_key
self._system_instruction = system_instruction
self._create_client()
self._settings = {
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/grok.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def __init__(
):
super().__init__(api_key=api_key, base_url=base_url, model=model, **kwargs)
# Initialize counters for token usage metrics
self._api_key = api_key
self._prompt_tokens = 0
self._completion_tokens = 0
self._total_tokens = 0
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/groq.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(
**kwargs,
):
super().__init__(api_key=api_key, base_url=base_url, model=model, **kwargs)
self._api_key = api_key

def create_client(self, api_key=None, base_url=None, **kwargs):
"""Create OpenAI-compatible client for Groq API endpoint."""
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/moondream.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(
self, *, model="vikhyatk/moondream2", revision="2024-08-26", use_cpu=False, **kwargs
):
super().__init__(**kwargs)
self._require_api_key = False

self.set_model_name(model)

Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/nim.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(
):
super().__init__(api_key=api_key, base_url=base_url, model=model, **kwargs)
# Counters for accumulating token usage metrics
self._api_key = api_key
self._prompt_tokens = 0
self._completion_tokens = 0
self._total_tokens = 0
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/ollama.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
class OLLamaLLMService(BaseOpenAILLMService):
def __init__(self, *, model: str = "llama2", base_url: str = "http://localhost:11434/v1"):
super().__init__(model=model, base_url=base_url, api_key="ollama")
self._api_key = "ollama"
2 changes: 2 additions & 0 deletions src/pipecat/services/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def __init__(
**kwargs,
):
super().__init__(**kwargs)
self._api_key = api_key
self._settings = {
"frequency_penalty": params.frequency_penalty,
"presence_penalty": params.presence_penalty,
Expand Down Expand Up @@ -416,6 +417,7 @@ def __init__(
**kwargs,
):
super().__init__(sample_rate=sample_rate, **kwargs)
self._api_key = api_key

self._settings = {
"sample_rate": sample_rate,
Expand Down
4 changes: 2 additions & 2 deletions src/pipecat/services/openai_realtime_beta/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ def __init__(
):
full_url = f"{base_url}?model={model}"
super().__init__(base_url=full_url, **kwargs)
self.api_key = api_key
self.base_url = full_url

self._api_key = api_key
self._session_properties: events.SessionProperties = session_properties
self._audio_input_paused = start_audio_paused
self._send_transcription_frames = send_transcription_frames
Expand Down Expand Up @@ -262,7 +262,7 @@ async def _connect(self):
self._websocket = await websockets.connect(
uri=self.base_url,
extra_headers={
"Authorization": f"Bearer {self.api_key}",
"Authorization": f"Bearer {self._api_key}",
"OpenAI-Beta": "realtime=v1",
},
)
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/openpipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(
**kwargs,
)
self._tags = tags
self._api_key = api_key

def create_client(self, api_key=None, base_url=None, **kwargs):
openpipe_api_key = kwargs.get("openpipe_api_key") or ""
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/openrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(
model=model,
**kwargs,
)
self._api_key = api_key

def create_client(self, api_key=None, base_url=None, **kwargs):
logger.debug(f"Creating OpenRouter client with api {base_url}")
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/together.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(
**kwargs,
):
super().__init__(api_key=api_key, base_url=base_url, model=model, **kwargs)
self._api_key = api_key

def create_client(self, api_key=None, base_url=None, **kwargs):
"""Create OpenAI-compatible client for Together.ai API endpoint."""
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/whisper.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(
**kwargs,
):
super().__init__(**kwargs)
self._require_api_key = False
self._device: str = device
self._compute_type = compute_type
self.set_model_name(model if isinstance(model, str) else model.value)
Expand Down
1 change: 1 addition & 0 deletions src/pipecat/services/xtts.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def __init__(
**kwargs,
):
super().__init__(sample_rate=sample_rate, **kwargs)
self._require_api_key = False

self._settings = {
"language": self.language_to_service_language(language),
Expand Down

0 comments on commit 287b6f7

Please sign in to comment.