From 052061f2b793698515411568c674f8aa50139289 Mon Sep 17 00:00:00 2001 From: Dirk Brand Date: Mon, 16 Dec 2024 11:30:01 +0200 Subject: [PATCH 1/7] Add key validation on all models --- phi/llm/openai/chat.py | 6 ++++++ phi/model/InternLM/internlm.py | 13 +++++++++++-- phi/model/anthropic/claude.py | 10 +++++++--- phi/model/azure/openai_chat.py | 5 ++--- phi/model/cohere/chat.py | 6 ++++++ phi/model/deepseek/deepseek.py | 12 ++++++++++-- phi/model/fireworks/fireworks.py | 13 +++++++++++-- phi/model/google/gemini.py | 10 +++++++--- phi/model/google/gemini_openai.py | 13 +++++++++++-- phi/model/groq/groq.py | 6 ++++++ phi/model/huggingface/hf.py | 6 ++++++ phi/model/mistral/mistral.py | 5 +++++ phi/model/nvidia/nvidia.py | 13 +++++++++++-- phi/model/openai/chat.py | 6 ++++++ phi/model/openrouter/openrouter.py | 11 ++++++++++- phi/model/sambanova/sambanova.py | 10 +++++++++- phi/model/together/together.py | 8 ++++++++ phi/model/xai/xai.py | 11 ++++++++++- 18 files changed, 142 insertions(+), 22 deletions(-) diff --git a/phi/llm/openai/chat.py b/phi/llm/openai/chat.py index 666313522d..dffd3fecfa 100644 --- a/phi/llm/openai/chat.py +++ b/phi/llm/openai/chat.py @@ -1,3 +1,5 @@ +import os + import httpx from typing import Optional, List, Iterator, Dict, Any, Union, Tuple @@ -73,6 +75,10 @@ def get_client(self) -> OpenAIClient: if self.openai_client: return self.openai_client + self.api_key = self.api_key or os.getenv("OPENAI_API_KEY") + if not self.api_key: + logger.error("OPENAI_API_KEY not set. Please set the OPENAI_API_KEY environment variable.") + _client_params: Dict[str, Any] = {} if self.api_key: _client_params["api_key"] = self.api_key diff --git a/phi/model/InternLM/internlm.py b/phi/model/InternLM/internlm.py index f41714e482..be06044a7b 100644 --- a/phi/model/InternLM/internlm.py +++ b/phi/model/InternLM/internlm.py @@ -1,5 +1,8 @@ from os import getenv -from typing import Optional +from typing import Optional, Any + +from pydantic import model_validator + from phi.model.openai.like import OpenAILike @@ -19,5 +22,11 @@ class InternLM(OpenAILike): name: str = "InternLM" provider: str = "InternLM" - api_key: Optional[str] = getenv("INTERNLM_API_KEY") + api_key: Optional[str] = getenv("INTERNLM_API_KEY", None) base_url: Optional[str] = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/chat/completions" + + @model_validator(mode='before') + def validate_api_key(cls, data: Any) -> str: + if 'api_key' not in data or data['api_key'] is None: + raise ValueError("API key must be set for InternLM. Set it as an environment variable (INTERNLM_API_KEY) or provide it explicitly.") + return data diff --git a/phi/model/anthropic/claude.py b/phi/model/anthropic/claude.py index 87508a335a..ddb8972c12 100644 --- a/phi/model/anthropic/claude.py +++ b/phi/model/anthropic/claude.py @@ -1,4 +1,5 @@ import json +import os from dataclasses import dataclass, field from typing import Optional, List, Iterator, Dict, Any, Union, Tuple @@ -18,9 +19,8 @@ RawContentBlockDeltaEvent, ContentBlockStopEvent, ) -except ImportError: - logger.error("`anthropic` not installed") - raise +except (ModuleNotFoundError, ImportError): + raise ImportError("`anthropic` not installed. Please install using `pip install anthropic`") @dataclass @@ -89,6 +89,10 @@ def get_client(self) -> AnthropicClient: if self.client: return self.client + self.api_key = self.api_key or os.getenv("ANTHROPIC_API_KEY") + if not self.api_key: + logger.error("ANTHROPIC_API_KEY not set. Please set the ANTHROPIC_API_KEY environment variable.") + _client_params: Dict[str, Any] = {} # Set client parameters if they are provided if self.api_key: diff --git a/phi/model/azure/openai_chat.py b/phi/model/azure/openai_chat.py index d40fb28b82..873057b867 100644 --- a/phi/model/azure/openai_chat.py +++ b/phi/model/azure/openai_chat.py @@ -7,9 +7,8 @@ try: from openai import AzureOpenAI as AzureOpenAIClient from openai import AsyncAzureOpenAI as AsyncAzureOpenAIClient -except ImportError: - logger.error("`azure openai` not installed") - raise +except (ModuleNotFoundError, ImportError): + raise ImportError("`azure openai` not installed. Please install using `pip install openai`") class AzureOpenAIChat(OpenAILike): diff --git a/phi/model/cohere/chat.py b/phi/model/cohere/chat.py index ddda236d2e..8de39dd295 100644 --- a/phi/model/cohere/chat.py +++ b/phi/model/cohere/chat.py @@ -1,4 +1,5 @@ import json +import os from dataclasses import dataclass, field from typing import Optional, List, Iterator, Dict, Any, Tuple @@ -72,6 +73,11 @@ def client(self) -> CohereClient: return self.cohere_client _client_params: Dict[str, Any] = {} + + self.api_key = self.api_key or os.getenv("CO_API_KEY") + if not self.api_key: + logger.error("CO_API_KEY not set. Please set the CO_API_KEY environment variable.") + if self.api_key: _client_params["api_key"] = self.api_key return CohereClient(**_client_params) diff --git a/phi/model/deepseek/deepseek.py b/phi/model/deepseek/deepseek.py index e2f1aa35b3..9a5495fd3d 100644 --- a/phi/model/deepseek/deepseek.py +++ b/phi/model/deepseek/deepseek.py @@ -1,6 +1,8 @@ -from typing import Optional +from typing import Optional, Any from os import getenv +from pydantic import model_validator + from phi.model.openai.like import OpenAILike @@ -20,5 +22,11 @@ class DeepSeekChat(OpenAILike): name: str = "DeepSeekChat" provider: str = "DeepSeek" - api_key: Optional[str] = getenv("DEEPSEEK_API_KEY") + api_key: Optional[str] = getenv("DEEPSEEK_API_KEY", None) base_url: str = "https://api.deepseek.com" + + @model_validator(mode='before') + def validate_api_key(cls, data: Any) -> str: + if 'api_key' not in data or data['api_key'] is None: + raise ValueError("API key must be set for DeepSeekChat. Set it as an environment variable (DEEPSEEK_API_KEY) or provide it explicitly.") + return data diff --git a/phi/model/fireworks/fireworks.py b/phi/model/fireworks/fireworks.py index 60559f2b7e..0ad8a753ea 100644 --- a/phi/model/fireworks/fireworks.py +++ b/phi/model/fireworks/fireworks.py @@ -1,5 +1,7 @@ from os import getenv -from typing import Optional, List, Iterator +from typing import Optional, List, Iterator, Any + +from pydantic import model_validator from phi.model.message import Message from phi.model.openai import OpenAILike @@ -22,9 +24,16 @@ class Fireworks(OpenAILike): name: str = "Fireworks: " + id provider: str = "Fireworks" - api_key: Optional[str] = getenv("FIREWORKS_API_KEY") + api_key: Optional[str] = getenv("FIREWORKS_API_KEY", None) base_url: str = "https://api.fireworks.ai/inference/v1" + @model_validator(mode='before') + def validate_api_key(cls, data: Any) -> str: + if 'api_key' not in data or data['api_key'] is None: + raise ValueError("API key must be set for Fireworks. Set it as an environment variable (FIREWORKS_API_KEY) or provide it explicitly.") + return data + + def invoke_stream(self, messages: List[Message]) -> Iterator[ChatCompletionChunk]: """ Send a streaming chat completion request to the Fireworks API. diff --git a/phi/model/google/gemini.py b/phi/model/google/gemini.py index 263d3afb09..e04ed170ec 100644 --- a/phi/model/google/gemini.py +++ b/phi/model/google/gemini.py @@ -1,3 +1,4 @@ +import os import time import json from pathlib import Path @@ -103,9 +104,12 @@ def get_client(self) -> GenerativeModel: return self.client client_params: Dict[str, Any] = {} - # Set client parameters if they are provided - if self.api_key: - client_params["api_key"] = self.api_key + + self.api_key = self.api_key or os.getenv("GOOGLE_API_KEY") + if not self.api_key: + logger.error("GOOGLE_API_KEY not set. Please set the GOOGLE_API_KEY environment variable.") + client_params["api_key"] = self.api_key + if self.client_params: client_params.update(self.client_params) genai.configure(**client_params) diff --git a/phi/model/google/gemini_openai.py b/phi/model/google/gemini_openai.py index c9b144746d..8c8801488f 100644 --- a/phi/model/google/gemini_openai.py +++ b/phi/model/google/gemini_openai.py @@ -1,5 +1,8 @@ from os import getenv -from typing import Optional +from typing import Optional, Any + +from pydantic import model_validator + from phi.model.openai.like import OpenAILike @@ -19,5 +22,11 @@ class GeminiOpenAIChat(OpenAILike): name: str = "Gemini" provider: str = "Google" - api_key: Optional[str] = getenv("GOOGLE_API_KEY") + api_key: Optional[str] = getenv("GOOGLE_API_KEY", None) base_url: Optional[str] = "https://generativelanguage.googleapis.com/v1beta/" + + @model_validator(mode='before') + def validate_api_key(cls, data: Any) -> str: + if 'api_key' not in data or data['api_key'] is None: + raise ValueError("API key must be set for GeminiOpenAIChat. Set it as an environment variable (GOOGLE_API_KEY) or provide it explicitly.") + return data diff --git a/phi/model/groq/groq.py b/phi/model/groq/groq.py index 1e8cbee544..a79c1f79f6 100644 --- a/phi/model/groq/groq.py +++ b/phi/model/groq/groq.py @@ -1,3 +1,4 @@ +import os from dataclasses import dataclass, field from typing import Optional, List, Iterator, Dict, Any, Union @@ -104,6 +105,11 @@ class Groq(Model): async_client: Optional[AsyncGroqClient] = None def get_client_params(self) -> Dict[str, Any]: + + self.api_key = self.api_key or os.getenv("GROQ_API_KEY") + if not self.api_key: + logger.error("GROQ_API_KEY not set. Please set the GROQ_API_KEY environment variable.") + client_params: Dict[str, Any] = {} if self.api_key: client_params["api_key"] = self.api_key diff --git a/phi/model/huggingface/hf.py b/phi/model/huggingface/hf.py index 18f449ca31..2266311e5c 100644 --- a/phi/model/huggingface/hf.py +++ b/phi/model/huggingface/hf.py @@ -1,3 +1,4 @@ +import os from dataclasses import dataclass, field from typing import Optional, List, Iterator, Dict, Any, Union @@ -129,6 +130,11 @@ class HuggingFaceChat(Model): async_client: Optional[AsyncInferenceClient] = None def get_client_params(self) -> Dict[str, Any]: + + self.api_key = self.api_key or os.getenv("HF_TOKEN") + if not self.api_key: + logger.error("HF_TOKEN not set. Please set the HF_TOKEN environment variable.") + _client_params: Dict[str, Any] = {} if self.api_key is not None: _client_params["api_key"] = self.api_key diff --git a/phi/model/mistral/mistral.py b/phi/model/mistral/mistral.py index b3d3001c20..6983e542d9 100644 --- a/phi/model/mistral/mistral.py +++ b/phi/model/mistral/mistral.py @@ -1,3 +1,4 @@ +import os from dataclasses import dataclass, field from typing import Optional, List, Iterator, Dict, Any, Union @@ -91,6 +92,10 @@ def client(self) -> Mistral: if self.mistral_client: return self.mistral_client + self.api_key = self.api_key or os.getenv("MISTRAL_API_KEY") + if not self.api_key: + logger.error("MISTRAL_API_KEY not set. Please set the MISTRAL_API_KEY environment variable.") + _client_params: Dict[str, Any] = {} if self.api_key: _client_params["api_key"] = self.api_key diff --git a/phi/model/nvidia/nvidia.py b/phi/model/nvidia/nvidia.py index 503f5d0ba1..a2854844a9 100644 --- a/phi/model/nvidia/nvidia.py +++ b/phi/model/nvidia/nvidia.py @@ -1,5 +1,7 @@ from os import getenv -from typing import Optional +from typing import Optional, Any + +from pydantic import model_validator from phi.model.openai.like import OpenAILike @@ -20,5 +22,12 @@ class Nvidia(OpenAILike): name: str = "Nvidia" provider: str = "Nvidia " + id - api_key: Optional[str] = getenv("NVIDIA_API_KEY") + api_key: Optional[str] = getenv("NVIDIA_API_KEY", None) base_url: str = "https://integrate.api.nvidia.com/v1" + + @model_validator(mode='before') + def validate_api_key(cls, data: Any) -> str: + if 'api_key' not in data or data['api_key'] is None: + raise ValueError("API key must be set for DeepSeekChat. Set it as an environment variable (DEEPSEEK_API_KEY) or provide it explicitly.") + return data + diff --git a/phi/model/openai/chat.py b/phi/model/openai/chat.py index 95c20bc170..30f99716f4 100644 --- a/phi/model/openai/chat.py +++ b/phi/model/openai/chat.py @@ -1,3 +1,4 @@ +import os from dataclasses import dataclass, field from typing import Optional, List, Iterator, Dict, Any, Union @@ -118,6 +119,11 @@ class OpenAIChat(Model): def get_client_params(self) -> Dict[str, Any]: client_params: Dict[str, Any] = {} + + self.api_key = self.api_key or os.getenv("OPENAI_API_KEY") + if not self.api_key: + logger.error("OPENAI_API_KEY not set. Please set the OPENAI_API_KEY environment variable.") + if self.api_key is not None: client_params["api_key"] = self.api_key if self.organization is not None: diff --git a/phi/model/openrouter/openrouter.py b/phi/model/openrouter/openrouter.py index fd511d766f..9f72063d2e 100644 --- a/phi/model/openrouter/openrouter.py +++ b/phi/model/openrouter/openrouter.py @@ -1,5 +1,7 @@ from os import getenv -from typing import Optional +from typing import Optional, Any + +from pydantic import model_validator from phi.model.openai.like import OpenAILike @@ -24,3 +26,10 @@ class OpenRouter(OpenAILike): api_key: Optional[str] = getenv("OPENROUTER_API_KEY") base_url: str = "https://openrouter.ai/api/v1" max_tokens: int = 1024 + + @model_validator(mode='before') + def validate_api_key(cls, data: Any) -> str: + if 'api_key' not in data or data['api_key'] is None: + raise ValueError("API key must be set for OpenRouter. Set it as an environment variable (OPENROUTER_API_KEY) or provide it explicitly.") + return data + diff --git a/phi/model/sambanova/sambanova.py b/phi/model/sambanova/sambanova.py index 3b34329791..b3644a6ef2 100644 --- a/phi/model/sambanova/sambanova.py +++ b/phi/model/sambanova/sambanova.py @@ -1,6 +1,8 @@ -from typing import Optional +from typing import Optional, Any from os import getenv +from pydantic import model_validator + from phi.model.openai.like import OpenAILike @@ -22,3 +24,9 @@ class Sambanova(OpenAILike): api_key: Optional[str] = getenv("SAMBANOVA_API_KEY") base_url: str = "https://api.sambanova.ai/v1" + + @model_validator(mode='before') + def validate_api_key(cls, data: Any) -> str: + if 'api_key' not in data or data['api_key'] is None: + raise ValueError("API key must be set for Sambanova. Set it as an environment variable (SAMBANOVA_API_KEY) or provide it explicitly.") + return data diff --git a/phi/model/together/together.py b/phi/model/together/together.py index a8ceb15fa1..47321c59a3 100644 --- a/phi/model/together/together.py +++ b/phi/model/together/together.py @@ -2,6 +2,8 @@ from os import getenv from typing import Optional, List, Iterator, Dict, Any +from pydantic import model_validator + from phi.model.message import Message from phi.model.openai.chat import StreamData, Metrics from phi.model.openai.like import OpenAILike @@ -40,6 +42,12 @@ class Together(OpenAILike): base_url: str = "https://api.together.xyz/v1" monkey_patch: bool = False + @model_validator(mode='before') + def validate_api_key(cls, data: Any) -> str: + if 'api_key' not in data or data['api_key'] is None: + raise ValueError("API key must be set for Together. Set it as an environment variable (TOGETHER_API_KEY) or provide it explicitly.") + return data + def response_stream(self, messages: List[Message]) -> Iterator[ModelResponse]: if not self.monkey_patch: yield from super().response_stream(messages) diff --git a/phi/model/xai/xai.py b/phi/model/xai/xai.py index a28698f3c1..f928cf8448 100644 --- a/phi/model/xai/xai.py +++ b/phi/model/xai/xai.py @@ -1,5 +1,8 @@ from os import getenv -from typing import Optional +from typing import Optional, Any + +from pydantic import model_validator + from phi.model.openai.like import OpenAILike @@ -21,3 +24,9 @@ class xAI(OpenAILike): api_key: Optional[str] = getenv("XAI_API_KEY") base_url: Optional[str] = "https://api.x.ai/v1" + + @model_validator(mode='before') + def validate_api_key(cls, data: Any) -> str: + if 'api_key' not in data or data['api_key'] is None: + raise ValueError("API key must be set for xAI. Set it as an environment variable (XAI_API_KEY) or provide it explicitly.") + return data From 34c1d7da47d6c1d19840e5ede0d47b32444411d5 Mon Sep 17 00:00:00 2001 From: Dirk Brand Date: Mon, 16 Dec 2024 11:31:36 +0200 Subject: [PATCH 2/7] Style fix --- phi/model/InternLM/internlm.py | 8 +++++--- phi/model/azure/openai_chat.py | 1 - phi/model/deepseek/deepseek.py | 8 +++++--- phi/model/fireworks/fireworks.py | 9 +++++---- phi/model/google/gemini_openai.py | 8 +++++--- phi/model/groq/groq.py | 1 - phi/model/huggingface/hf.py | 1 - phi/model/nvidia/nvidia.py | 9 +++++---- phi/model/openrouter/openrouter.py | 9 +++++---- phi/model/sambanova/sambanova.py | 8 +++++--- phi/model/together/together.py | 8 +++++--- phi/model/xai/xai.py | 8 +++++--- phi/workspace/config.py | 1 + 13 files changed, 46 insertions(+), 33 deletions(-) diff --git a/phi/model/InternLM/internlm.py b/phi/model/InternLM/internlm.py index be06044a7b..d9b99bc93b 100644 --- a/phi/model/InternLM/internlm.py +++ b/phi/model/InternLM/internlm.py @@ -25,8 +25,10 @@ class InternLM(OpenAILike): api_key: Optional[str] = getenv("INTERNLM_API_KEY", None) base_url: Optional[str] = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/chat/completions" - @model_validator(mode='before') + @model_validator(mode="before") def validate_api_key(cls, data: Any) -> str: - if 'api_key' not in data or data['api_key'] is None: - raise ValueError("API key must be set for InternLM. Set it as an environment variable (INTERNLM_API_KEY) or provide it explicitly.") + if "api_key" not in data or data["api_key"] is None: + raise ValueError( + "API key must be set for InternLM. Set it as an environment variable (INTERNLM_API_KEY) or provide it explicitly." + ) return data diff --git a/phi/model/azure/openai_chat.py b/phi/model/azure/openai_chat.py index 873057b867..afd040bd32 100644 --- a/phi/model/azure/openai_chat.py +++ b/phi/model/azure/openai_chat.py @@ -1,6 +1,5 @@ from os import getenv from typing import Optional, Dict, Any -from phi.utils.log import logger from phi.model.openai.like import OpenAILike import httpx diff --git a/phi/model/deepseek/deepseek.py b/phi/model/deepseek/deepseek.py index 9a5495fd3d..942b52b71e 100644 --- a/phi/model/deepseek/deepseek.py +++ b/phi/model/deepseek/deepseek.py @@ -25,8 +25,10 @@ class DeepSeekChat(OpenAILike): api_key: Optional[str] = getenv("DEEPSEEK_API_KEY", None) base_url: str = "https://api.deepseek.com" - @model_validator(mode='before') + @model_validator(mode="before") def validate_api_key(cls, data: Any) -> str: - if 'api_key' not in data or data['api_key'] is None: - raise ValueError("API key must be set for DeepSeekChat. Set it as an environment variable (DEEPSEEK_API_KEY) or provide it explicitly.") + if "api_key" not in data or data["api_key"] is None: + raise ValueError( + "API key must be set for DeepSeekChat. Set it as an environment variable (DEEPSEEK_API_KEY) or provide it explicitly." + ) return data diff --git a/phi/model/fireworks/fireworks.py b/phi/model/fireworks/fireworks.py index 0ad8a753ea..1f0765e25b 100644 --- a/phi/model/fireworks/fireworks.py +++ b/phi/model/fireworks/fireworks.py @@ -27,13 +27,14 @@ class Fireworks(OpenAILike): api_key: Optional[str] = getenv("FIREWORKS_API_KEY", None) base_url: str = "https://api.fireworks.ai/inference/v1" - @model_validator(mode='before') + @model_validator(mode="before") def validate_api_key(cls, data: Any) -> str: - if 'api_key' not in data or data['api_key'] is None: - raise ValueError("API key must be set for Fireworks. Set it as an environment variable (FIREWORKS_API_KEY) or provide it explicitly.") + if "api_key" not in data or data["api_key"] is None: + raise ValueError( + "API key must be set for Fireworks. Set it as an environment variable (FIREWORKS_API_KEY) or provide it explicitly." + ) return data - def invoke_stream(self, messages: List[Message]) -> Iterator[ChatCompletionChunk]: """ Send a streaming chat completion request to the Fireworks API. diff --git a/phi/model/google/gemini_openai.py b/phi/model/google/gemini_openai.py index 8c8801488f..ebdfebc6fb 100644 --- a/phi/model/google/gemini_openai.py +++ b/phi/model/google/gemini_openai.py @@ -25,8 +25,10 @@ class GeminiOpenAIChat(OpenAILike): api_key: Optional[str] = getenv("GOOGLE_API_KEY", None) base_url: Optional[str] = "https://generativelanguage.googleapis.com/v1beta/" - @model_validator(mode='before') + @model_validator(mode="before") def validate_api_key(cls, data: Any) -> str: - if 'api_key' not in data or data['api_key'] is None: - raise ValueError("API key must be set for GeminiOpenAIChat. Set it as an environment variable (GOOGLE_API_KEY) or provide it explicitly.") + if "api_key" not in data or data["api_key"] is None: + raise ValueError( + "API key must be set for GeminiOpenAIChat. Set it as an environment variable (GOOGLE_API_KEY) or provide it explicitly." + ) return data diff --git a/phi/model/groq/groq.py b/phi/model/groq/groq.py index a79c1f79f6..2afbaf67a4 100644 --- a/phi/model/groq/groq.py +++ b/phi/model/groq/groq.py @@ -105,7 +105,6 @@ class Groq(Model): async_client: Optional[AsyncGroqClient] = None def get_client_params(self) -> Dict[str, Any]: - self.api_key = self.api_key or os.getenv("GROQ_API_KEY") if not self.api_key: logger.error("GROQ_API_KEY not set. Please set the GROQ_API_KEY environment variable.") diff --git a/phi/model/huggingface/hf.py b/phi/model/huggingface/hf.py index 2266311e5c..7ff653ab4d 100644 --- a/phi/model/huggingface/hf.py +++ b/phi/model/huggingface/hf.py @@ -130,7 +130,6 @@ class HuggingFaceChat(Model): async_client: Optional[AsyncInferenceClient] = None def get_client_params(self) -> Dict[str, Any]: - self.api_key = self.api_key or os.getenv("HF_TOKEN") if not self.api_key: logger.error("HF_TOKEN not set. Please set the HF_TOKEN environment variable.") diff --git a/phi/model/nvidia/nvidia.py b/phi/model/nvidia/nvidia.py index a2854844a9..179d46e7b6 100644 --- a/phi/model/nvidia/nvidia.py +++ b/phi/model/nvidia/nvidia.py @@ -25,9 +25,10 @@ class Nvidia(OpenAILike): api_key: Optional[str] = getenv("NVIDIA_API_KEY", None) base_url: str = "https://integrate.api.nvidia.com/v1" - @model_validator(mode='before') + @model_validator(mode="before") def validate_api_key(cls, data: Any) -> str: - if 'api_key' not in data or data['api_key'] is None: - raise ValueError("API key must be set for DeepSeekChat. Set it as an environment variable (DEEPSEEK_API_KEY) or provide it explicitly.") + if "api_key" not in data or data["api_key"] is None: + raise ValueError( + "API key must be set for DeepSeekChat. Set it as an environment variable (DEEPSEEK_API_KEY) or provide it explicitly." + ) return data - diff --git a/phi/model/openrouter/openrouter.py b/phi/model/openrouter/openrouter.py index 9f72063d2e..4a07ff9c34 100644 --- a/phi/model/openrouter/openrouter.py +++ b/phi/model/openrouter/openrouter.py @@ -27,9 +27,10 @@ class OpenRouter(OpenAILike): base_url: str = "https://openrouter.ai/api/v1" max_tokens: int = 1024 - @model_validator(mode='before') + @model_validator(mode="before") def validate_api_key(cls, data: Any) -> str: - if 'api_key' not in data or data['api_key'] is None: - raise ValueError("API key must be set for OpenRouter. Set it as an environment variable (OPENROUTER_API_KEY) or provide it explicitly.") + if "api_key" not in data or data["api_key"] is None: + raise ValueError( + "API key must be set for OpenRouter. Set it as an environment variable (OPENROUTER_API_KEY) or provide it explicitly." + ) return data - diff --git a/phi/model/sambanova/sambanova.py b/phi/model/sambanova/sambanova.py index b3644a6ef2..2d35e51201 100644 --- a/phi/model/sambanova/sambanova.py +++ b/phi/model/sambanova/sambanova.py @@ -25,8 +25,10 @@ class Sambanova(OpenAILike): api_key: Optional[str] = getenv("SAMBANOVA_API_KEY") base_url: str = "https://api.sambanova.ai/v1" - @model_validator(mode='before') + @model_validator(mode="before") def validate_api_key(cls, data: Any) -> str: - if 'api_key' not in data or data['api_key'] is None: - raise ValueError("API key must be set for Sambanova. Set it as an environment variable (SAMBANOVA_API_KEY) or provide it explicitly.") + if "api_key" not in data or data["api_key"] is None: + raise ValueError( + "API key must be set for Sambanova. Set it as an environment variable (SAMBANOVA_API_KEY) or provide it explicitly." + ) return data diff --git a/phi/model/together/together.py b/phi/model/together/together.py index 47321c59a3..b85f7ba6df 100644 --- a/phi/model/together/together.py +++ b/phi/model/together/together.py @@ -42,10 +42,12 @@ class Together(OpenAILike): base_url: str = "https://api.together.xyz/v1" monkey_patch: bool = False - @model_validator(mode='before') + @model_validator(mode="before") def validate_api_key(cls, data: Any) -> str: - if 'api_key' not in data or data['api_key'] is None: - raise ValueError("API key must be set for Together. Set it as an environment variable (TOGETHER_API_KEY) or provide it explicitly.") + if "api_key" not in data or data["api_key"] is None: + raise ValueError( + "API key must be set for Together. Set it as an environment variable (TOGETHER_API_KEY) or provide it explicitly." + ) return data def response_stream(self, messages: List[Message]) -> Iterator[ModelResponse]: diff --git a/phi/model/xai/xai.py b/phi/model/xai/xai.py index f928cf8448..676ee81de3 100644 --- a/phi/model/xai/xai.py +++ b/phi/model/xai/xai.py @@ -25,8 +25,10 @@ class xAI(OpenAILike): api_key: Optional[str] = getenv("XAI_API_KEY") base_url: Optional[str] = "https://api.x.ai/v1" - @model_validator(mode='before') + @model_validator(mode="before") def validate_api_key(cls, data: Any) -> str: - if 'api_key' not in data or data['api_key'] is None: - raise ValueError("API key must be set for xAI. Set it as an environment variable (XAI_API_KEY) or provide it explicitly.") + if "api_key" not in data or data["api_key"] is None: + raise ValueError( + "API key must be set for xAI. Set it as an environment variable (XAI_API_KEY) or provide it explicitly." + ) return data diff --git a/phi/workspace/config.py b/phi/workspace/config.py index 241bcfc601..833a948407 100644 --- a/phi/workspace/config.py +++ b/phi/workspace/config.py @@ -19,6 +19,7 @@ def get_workspace_objects_from_file(resource_file: Path) -> dict: """Returns workspace objects from the resource file""" from phi.aws.resources import AwsResources from phi.docker.resources import DockerResources + try: python_objects = get_python_objects_from_module(resource_file) # logger.debug(f"python_objects: {python_objects}") From 05e6a3ed4e57cd0841681be89e561ca9c71d48fd Mon Sep 17 00:00:00 2001 From: Dirk Brand Date: Mon, 16 Dec 2024 11:34:48 +0200 Subject: [PATCH 3/7] Update error handling for missing imports --- phi/model/cohere/chat.py | 5 ++--- phi/model/google/gemini.py | 3 +-- phi/model/groq/groq.py | 5 ++--- phi/model/huggingface/hf.py | 5 ++--- phi/model/mistral/mistral.py | 5 ++--- phi/model/ollama/chat.py | 5 ++--- phi/model/openai/chat.py | 5 ++--- phi/model/vertexai/gemini.py | 5 ++--- 8 files changed, 15 insertions(+), 23 deletions(-) diff --git a/phi/model/cohere/chat.py b/phi/model/cohere/chat.py index 8de39dd295..a3ae1d2b0c 100644 --- a/phi/model/cohere/chat.py +++ b/phi/model/cohere/chat.py @@ -29,9 +29,8 @@ ) from cohere.types.api_meta_tokens import ApiMetaTokens from cohere.types.api_meta import ApiMeta -except ImportError: - logger.error("`cohere` not installed") - raise +except (ModuleNotFoundError, ImportError): + raise ImportError("`cohere` not installed. Please install using `pip install cohere`") @dataclass diff --git a/phi/model/google/gemini.py b/phi/model/google/gemini.py index e04ed170ec..526dcbe67b 100644 --- a/phi/model/google/gemini.py +++ b/phi/model/google/gemini.py @@ -25,8 +25,7 @@ ) from google.protobuf.struct_pb2 import Struct except (ModuleNotFoundError, ImportError): - logger.error("`google-generativeai` not installed. Please install it using `pip install google-generativeai`") - raise + raise ImportError("`google-generativeai` not installed. Please install it using `pip install google-generativeai`") @dataclass diff --git a/phi/model/groq/groq.py b/phi/model/groq/groq.py index 2afbaf67a4..5b5fa33cb6 100644 --- a/phi/model/groq/groq.py +++ b/phi/model/groq/groq.py @@ -17,9 +17,8 @@ from groq.types.chat import ChatCompletion, ChatCompletionMessage from groq.types.chat.chat_completion_chunk import ChatCompletionChunk, ChoiceDeltaToolCall, ChoiceDelta from groq.types.completion_usage import CompletionUsage -except ImportError: - logger.error("`groq` not installed") - raise +except (ModuleNotFoundError, ImportError): + raise ImportError("`groq` not installed. Please install using `pip install groq`") @dataclass diff --git a/phi/model/huggingface/hf.py b/phi/model/huggingface/hf.py index 7ff653ab4d..941cbb614d 100644 --- a/phi/model/huggingface/hf.py +++ b/phi/model/huggingface/hf.py @@ -24,9 +24,8 @@ ChatCompletionOutputMessage, ChatCompletionOutputUsage, ) -except ImportError: - logger.error("`huggingface_hub` not installed") - raise +except (ModuleNotFoundError, ImportError): + raise ImportError("`huggingface_hub` not installed. Please install using `pip install huggingface_hub`") @dataclass diff --git a/phi/model/mistral/mistral.py b/phi/model/mistral/mistral.py index 6983e542d9..9f3a24da95 100644 --- a/phi/model/mistral/mistral.py +++ b/phi/model/mistral/mistral.py @@ -15,9 +15,8 @@ from mistralai.models.chatcompletionresponse import ChatCompletionResponse from mistralai.models.deltamessage import DeltaMessage from mistralai.types.basemodel import Unset -except ImportError: - logger.error("`mistralai` not installed") - raise +except (ModuleNotFoundError, ImportError): + raise ImportError("`mistralai` not installed. Please install using `pip install mistralai`") MistralMessage = Union[models.UserMessage, models.AssistantMessage, models.SystemMessage, models.ToolMessage] diff --git a/phi/model/ollama/chat.py b/phi/model/ollama/chat.py index 4b4f1b3e23..4c5c809daa 100644 --- a/phi/model/ollama/chat.py +++ b/phi/model/ollama/chat.py @@ -14,9 +14,8 @@ try: from ollama import Client as OllamaClient, AsyncClient as AsyncOllamaClient -except ImportError: - logger.error("`ollama` not installed") - raise +except (ModuleNotFoundError, ImportError): + raise ImportError("`ollama` not installed. Please install using `pip install ollama`") @dataclass diff --git a/phi/model/openai/chat.py b/phi/model/openai/chat.py index 30f99716f4..ee3f8b8e6b 100644 --- a/phi/model/openai/chat.py +++ b/phi/model/openai/chat.py @@ -24,9 +24,8 @@ ChoiceDeltaToolCall, ) from openai.types.chat.chat_completion_message import ChatCompletionMessage -except ImportError: - logger.error("`openai` not installed") - raise +except (ModuleNotFoundError, ImportError): + raise ImportError("`openai` not installed. Please install using `pip install openai`") @dataclass diff --git a/phi/model/vertexai/gemini.py b/phi/model/vertexai/gemini.py index 87bb5dee78..e8c0e1eae0 100644 --- a/phi/model/vertexai/gemini.py +++ b/phi/model/vertexai/gemini.py @@ -21,9 +21,8 @@ Content, Part, ) -except ImportError: - logger.error("`google-cloud-aiplatform` not installed") - raise +except (ModuleNotFoundError, ImportError): + raise ImportError("`google-cloud-aiplatform` not installed. Please install using `pip install google-cloud-aiplatform`") @dataclass From 89b9c750c27290e017cef9c39579a75e184dc374 Mon Sep 17 00:00:00 2001 From: Dirk Brand Date: Mon, 16 Dec 2024 16:01:00 +0200 Subject: [PATCH 4/7] Fix imports --- .../workflows/games/game_output_file.html | 136 ++++++++++++++++++ phi/model/anthropic/claude.py | 4 +- phi/model/cohere/chat.py | 4 +- phi/model/google/gemini.py | 4 +- phi/model/groq/groq.py | 4 +- phi/model/huggingface/hf.py | 4 +- phi/model/mistral/mistral.py | 4 +- phi/model/openai/chat.py | 4 +- 8 files changed, 150 insertions(+), 14 deletions(-) create mode 100644 cookbook/workflows/games/game_output_file.html diff --git a/cookbook/workflows/games/game_output_file.html b/cookbook/workflows/games/game_output_file.html new file mode 100644 index 0000000000..894a4b18e6 --- /dev/null +++ b/cookbook/workflows/games/game_output_file.html @@ -0,0 +1,136 @@ + + + + + + + Flappy Bird Clone + + + + +
Press Space or click to make the bird fly!
+ + + + + + diff --git a/phi/model/anthropic/claude.py b/phi/model/anthropic/claude.py index ddb8972c12..e80696dfcd 100644 --- a/phi/model/anthropic/claude.py +++ b/phi/model/anthropic/claude.py @@ -1,5 +1,5 @@ import json -import os +from os import getenv from dataclasses import dataclass, field from typing import Optional, List, Iterator, Dict, Any, Union, Tuple @@ -89,7 +89,7 @@ def get_client(self) -> AnthropicClient: if self.client: return self.client - self.api_key = self.api_key or os.getenv("ANTHROPIC_API_KEY") + self.api_key = self.api_key or getenv("ANTHROPIC_API_KEY") if not self.api_key: logger.error("ANTHROPIC_API_KEY not set. Please set the ANTHROPIC_API_KEY environment variable.") diff --git a/phi/model/cohere/chat.py b/phi/model/cohere/chat.py index a3ae1d2b0c..eae45cc66e 100644 --- a/phi/model/cohere/chat.py +++ b/phi/model/cohere/chat.py @@ -1,5 +1,5 @@ import json -import os +from os import getenv from dataclasses import dataclass, field from typing import Optional, List, Iterator, Dict, Any, Tuple @@ -73,7 +73,7 @@ def client(self) -> CohereClient: _client_params: Dict[str, Any] = {} - self.api_key = self.api_key or os.getenv("CO_API_KEY") + self.api_key = self.api_key or getenv("CO_API_KEY") if not self.api_key: logger.error("CO_API_KEY not set. Please set the CO_API_KEY environment variable.") diff --git a/phi/model/google/gemini.py b/phi/model/google/gemini.py index 526dcbe67b..b2de87c2ee 100644 --- a/phi/model/google/gemini.py +++ b/phi/model/google/gemini.py @@ -1,4 +1,4 @@ -import os +from os import getenv import time import json from pathlib import Path @@ -104,7 +104,7 @@ def get_client(self) -> GenerativeModel: client_params: Dict[str, Any] = {} - self.api_key = self.api_key or os.getenv("GOOGLE_API_KEY") + self.api_key = self.api_key or getenv("GOOGLE_API_KEY") if not self.api_key: logger.error("GOOGLE_API_KEY not set. Please set the GOOGLE_API_KEY environment variable.") client_params["api_key"] = self.api_key diff --git a/phi/model/groq/groq.py b/phi/model/groq/groq.py index 5b5fa33cb6..671e4e02b6 100644 --- a/phi/model/groq/groq.py +++ b/phi/model/groq/groq.py @@ -1,4 +1,4 @@ -import os +from os import getenv from dataclasses import dataclass, field from typing import Optional, List, Iterator, Dict, Any, Union @@ -104,7 +104,7 @@ class Groq(Model): async_client: Optional[AsyncGroqClient] = None def get_client_params(self) -> Dict[str, Any]: - self.api_key = self.api_key or os.getenv("GROQ_API_KEY") + self.api_key = self.api_key or getenv("GROQ_API_KEY") if not self.api_key: logger.error("GROQ_API_KEY not set. Please set the GROQ_API_KEY environment variable.") diff --git a/phi/model/huggingface/hf.py b/phi/model/huggingface/hf.py index 941cbb614d..8ab956b2fc 100644 --- a/phi/model/huggingface/hf.py +++ b/phi/model/huggingface/hf.py @@ -1,4 +1,4 @@ -import os +from os import getenv from dataclasses import dataclass, field from typing import Optional, List, Iterator, Dict, Any, Union @@ -129,7 +129,7 @@ class HuggingFaceChat(Model): async_client: Optional[AsyncInferenceClient] = None def get_client_params(self) -> Dict[str, Any]: - self.api_key = self.api_key or os.getenv("HF_TOKEN") + self.api_key = self.api_key or getenv("HF_TOKEN") if not self.api_key: logger.error("HF_TOKEN not set. Please set the HF_TOKEN environment variable.") diff --git a/phi/model/mistral/mistral.py b/phi/model/mistral/mistral.py index 9f3a24da95..3dfd8d95d1 100644 --- a/phi/model/mistral/mistral.py +++ b/phi/model/mistral/mistral.py @@ -1,4 +1,4 @@ -import os +from os import getenv from dataclasses import dataclass, field from typing import Optional, List, Iterator, Dict, Any, Union @@ -91,7 +91,7 @@ def client(self) -> Mistral: if self.mistral_client: return self.mistral_client - self.api_key = self.api_key or os.getenv("MISTRAL_API_KEY") + self.api_key = self.api_key or getenv("MISTRAL_API_KEY") if not self.api_key: logger.error("MISTRAL_API_KEY not set. Please set the MISTRAL_API_KEY environment variable.") diff --git a/phi/model/openai/chat.py b/phi/model/openai/chat.py index ee3f8b8e6b..3d8715d899 100644 --- a/phi/model/openai/chat.py +++ b/phi/model/openai/chat.py @@ -1,4 +1,4 @@ -import os +from os import getenv from dataclasses import dataclass, field from typing import Optional, List, Iterator, Dict, Any, Union @@ -119,7 +119,7 @@ class OpenAIChat(Model): def get_client_params(self) -> Dict[str, Any]: client_params: Dict[str, Any] = {} - self.api_key = self.api_key or os.getenv("OPENAI_API_KEY") + self.api_key = self.api_key or getenv("OPENAI_API_KEY") if not self.api_key: logger.error("OPENAI_API_KEY not set. Please set the OPENAI_API_KEY environment variable.") From 2d12ef344ff428cbde78060f2fb96294f6982860 Mon Sep 17 00:00:00 2001 From: Dirk Brand Date: Mon, 16 Dec 2024 16:01:39 +0200 Subject: [PATCH 5/7] Update --- .../workflows/games/game_output_file.html | 136 ------------------ 1 file changed, 136 deletions(-) delete mode 100644 cookbook/workflows/games/game_output_file.html diff --git a/cookbook/workflows/games/game_output_file.html b/cookbook/workflows/games/game_output_file.html deleted file mode 100644 index 894a4b18e6..0000000000 --- a/cookbook/workflows/games/game_output_file.html +++ /dev/null @@ -1,136 +0,0 @@ - - - - - - - Flappy Bird Clone - - - - -
Press Space or click to make the bird fly!
- - - - - - From 79e7fe62bd50f900264c674073ad76c6d06ccd5c Mon Sep 17 00:00:00 2001 From: Dirk Brand Date: Mon, 16 Dec 2024 16:13:11 +0200 Subject: [PATCH 6/7] Update --- phi/model/InternLM/internlm.py | 10 ---------- phi/model/deepseek/deepseek.py | 10 ---------- phi/model/fireworks/fireworks.py | 10 ---------- phi/model/google/gemini_openai.py | 9 --------- phi/model/nvidia/nvidia.py | 10 ---------- phi/model/openrouter/openrouter.py | 10 ---------- phi/model/sambanova/sambanova.py | 10 ---------- phi/model/together/together.py | 10 ---------- phi/model/xai/xai.py | 10 ---------- 9 files changed, 89 deletions(-) diff --git a/phi/model/InternLM/internlm.py b/phi/model/InternLM/internlm.py index d9b99bc93b..cf98ca7749 100644 --- a/phi/model/InternLM/internlm.py +++ b/phi/model/InternLM/internlm.py @@ -1,8 +1,6 @@ from os import getenv from typing import Optional, Any -from pydantic import model_validator - from phi.model.openai.like import OpenAILike @@ -24,11 +22,3 @@ class InternLM(OpenAILike): api_key: Optional[str] = getenv("INTERNLM_API_KEY", None) base_url: Optional[str] = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/chat/completions" - - @model_validator(mode="before") - def validate_api_key(cls, data: Any) -> str: - if "api_key" not in data or data["api_key"] is None: - raise ValueError( - "API key must be set for InternLM. Set it as an environment variable (INTERNLM_API_KEY) or provide it explicitly." - ) - return data diff --git a/phi/model/deepseek/deepseek.py b/phi/model/deepseek/deepseek.py index 942b52b71e..6ee2190028 100644 --- a/phi/model/deepseek/deepseek.py +++ b/phi/model/deepseek/deepseek.py @@ -1,8 +1,6 @@ from typing import Optional, Any from os import getenv -from pydantic import model_validator - from phi.model.openai.like import OpenAILike @@ -24,11 +22,3 @@ class DeepSeekChat(OpenAILike): api_key: Optional[str] = getenv("DEEPSEEK_API_KEY", None) base_url: str = "https://api.deepseek.com" - - @model_validator(mode="before") - def validate_api_key(cls, data: Any) -> str: - if "api_key" not in data or data["api_key"] is None: - raise ValueError( - "API key must be set for DeepSeekChat. Set it as an environment variable (DEEPSEEK_API_KEY) or provide it explicitly." - ) - return data diff --git a/phi/model/fireworks/fireworks.py b/phi/model/fireworks/fireworks.py index 1f0765e25b..f8122e8e0c 100644 --- a/phi/model/fireworks/fireworks.py +++ b/phi/model/fireworks/fireworks.py @@ -1,8 +1,6 @@ from os import getenv from typing import Optional, List, Iterator, Any -from pydantic import model_validator - from phi.model.message import Message from phi.model.openai import OpenAILike from openai.types.chat.chat_completion_chunk import ChatCompletionChunk @@ -27,14 +25,6 @@ class Fireworks(OpenAILike): api_key: Optional[str] = getenv("FIREWORKS_API_KEY", None) base_url: str = "https://api.fireworks.ai/inference/v1" - @model_validator(mode="before") - def validate_api_key(cls, data: Any) -> str: - if "api_key" not in data or data["api_key"] is None: - raise ValueError( - "API key must be set for Fireworks. Set it as an environment variable (FIREWORKS_API_KEY) or provide it explicitly." - ) - return data - def invoke_stream(self, messages: List[Message]) -> Iterator[ChatCompletionChunk]: """ Send a streaming chat completion request to the Fireworks API. diff --git a/phi/model/google/gemini_openai.py b/phi/model/google/gemini_openai.py index ebdfebc6fb..1cc0934ce1 100644 --- a/phi/model/google/gemini_openai.py +++ b/phi/model/google/gemini_openai.py @@ -1,7 +1,6 @@ from os import getenv from typing import Optional, Any -from pydantic import model_validator from phi.model.openai.like import OpenAILike @@ -24,11 +23,3 @@ class GeminiOpenAIChat(OpenAILike): api_key: Optional[str] = getenv("GOOGLE_API_KEY", None) base_url: Optional[str] = "https://generativelanguage.googleapis.com/v1beta/" - - @model_validator(mode="before") - def validate_api_key(cls, data: Any) -> str: - if "api_key" not in data or data["api_key"] is None: - raise ValueError( - "API key must be set for GeminiOpenAIChat. Set it as an environment variable (GOOGLE_API_KEY) or provide it explicitly." - ) - return data diff --git a/phi/model/nvidia/nvidia.py b/phi/model/nvidia/nvidia.py index 179d46e7b6..b8ccad2947 100644 --- a/phi/model/nvidia/nvidia.py +++ b/phi/model/nvidia/nvidia.py @@ -1,8 +1,6 @@ from os import getenv from typing import Optional, Any -from pydantic import model_validator - from phi.model.openai.like import OpenAILike @@ -24,11 +22,3 @@ class Nvidia(OpenAILike): api_key: Optional[str] = getenv("NVIDIA_API_KEY", None) base_url: str = "https://integrate.api.nvidia.com/v1" - - @model_validator(mode="before") - def validate_api_key(cls, data: Any) -> str: - if "api_key" not in data or data["api_key"] is None: - raise ValueError( - "API key must be set for DeepSeekChat. Set it as an environment variable (DEEPSEEK_API_KEY) or provide it explicitly." - ) - return data diff --git a/phi/model/openrouter/openrouter.py b/phi/model/openrouter/openrouter.py index 4a07ff9c34..d34546c03f 100644 --- a/phi/model/openrouter/openrouter.py +++ b/phi/model/openrouter/openrouter.py @@ -1,8 +1,6 @@ from os import getenv from typing import Optional, Any -from pydantic import model_validator - from phi.model.openai.like import OpenAILike @@ -26,11 +24,3 @@ class OpenRouter(OpenAILike): api_key: Optional[str] = getenv("OPENROUTER_API_KEY") base_url: str = "https://openrouter.ai/api/v1" max_tokens: int = 1024 - - @model_validator(mode="before") - def validate_api_key(cls, data: Any) -> str: - if "api_key" not in data or data["api_key"] is None: - raise ValueError( - "API key must be set for OpenRouter. Set it as an environment variable (OPENROUTER_API_KEY) or provide it explicitly." - ) - return data diff --git a/phi/model/sambanova/sambanova.py b/phi/model/sambanova/sambanova.py index 2d35e51201..fc670944f2 100644 --- a/phi/model/sambanova/sambanova.py +++ b/phi/model/sambanova/sambanova.py @@ -1,8 +1,6 @@ from typing import Optional, Any from os import getenv -from pydantic import model_validator - from phi.model.openai.like import OpenAILike @@ -24,11 +22,3 @@ class Sambanova(OpenAILike): api_key: Optional[str] = getenv("SAMBANOVA_API_KEY") base_url: str = "https://api.sambanova.ai/v1" - - @model_validator(mode="before") - def validate_api_key(cls, data: Any) -> str: - if "api_key" not in data or data["api_key"] is None: - raise ValueError( - "API key must be set for Sambanova. Set it as an environment variable (SAMBANOVA_API_KEY) or provide it explicitly." - ) - return data diff --git a/phi/model/together/together.py b/phi/model/together/together.py index b85f7ba6df..a8ceb15fa1 100644 --- a/phi/model/together/together.py +++ b/phi/model/together/together.py @@ -2,8 +2,6 @@ from os import getenv from typing import Optional, List, Iterator, Dict, Any -from pydantic import model_validator - from phi.model.message import Message from phi.model.openai.chat import StreamData, Metrics from phi.model.openai.like import OpenAILike @@ -42,14 +40,6 @@ class Together(OpenAILike): base_url: str = "https://api.together.xyz/v1" monkey_patch: bool = False - @model_validator(mode="before") - def validate_api_key(cls, data: Any) -> str: - if "api_key" not in data or data["api_key"] is None: - raise ValueError( - "API key must be set for Together. Set it as an environment variable (TOGETHER_API_KEY) or provide it explicitly." - ) - return data - def response_stream(self, messages: List[Message]) -> Iterator[ModelResponse]: if not self.monkey_patch: yield from super().response_stream(messages) diff --git a/phi/model/xai/xai.py b/phi/model/xai/xai.py index 676ee81de3..cb418b769f 100644 --- a/phi/model/xai/xai.py +++ b/phi/model/xai/xai.py @@ -1,8 +1,6 @@ from os import getenv from typing import Optional, Any -from pydantic import model_validator - from phi.model.openai.like import OpenAILike @@ -24,11 +22,3 @@ class xAI(OpenAILike): api_key: Optional[str] = getenv("XAI_API_KEY") base_url: Optional[str] = "https://api.x.ai/v1" - - @model_validator(mode="before") - def validate_api_key(cls, data: Any) -> str: - if "api_key" not in data or data["api_key"] is None: - raise ValueError( - "API key must be set for xAI. Set it as an environment variable (XAI_API_KEY) or provide it explicitly." - ) - return data From 19432d1813591667608204f5cd476d2eab6da78b Mon Sep 17 00:00:00 2001 From: Dirk Brand Date: Mon, 16 Dec 2024 16:15:11 +0200 Subject: [PATCH 7/7] Update --- phi/model/InternLM/internlm.py | 2 +- phi/model/deepseek/deepseek.py | 2 +- phi/model/fireworks/fireworks.py | 2 +- phi/model/google/gemini_openai.py | 2 +- phi/model/nvidia/nvidia.py | 2 +- phi/model/openrouter/openrouter.py | 2 +- phi/model/sambanova/sambanova.py | 2 +- phi/model/vertexai/gemini.py | 4 +++- phi/model/xai/xai.py | 2 +- 9 files changed, 11 insertions(+), 9 deletions(-) diff --git a/phi/model/InternLM/internlm.py b/phi/model/InternLM/internlm.py index cf98ca7749..04cf0ea196 100644 --- a/phi/model/InternLM/internlm.py +++ b/phi/model/InternLM/internlm.py @@ -1,5 +1,5 @@ from os import getenv -from typing import Optional, Any +from typing import Optional from phi.model.openai.like import OpenAILike diff --git a/phi/model/deepseek/deepseek.py b/phi/model/deepseek/deepseek.py index 6ee2190028..37b2079c18 100644 --- a/phi/model/deepseek/deepseek.py +++ b/phi/model/deepseek/deepseek.py @@ -1,4 +1,4 @@ -from typing import Optional, Any +from typing import Optional from os import getenv from phi.model.openai.like import OpenAILike diff --git a/phi/model/fireworks/fireworks.py b/phi/model/fireworks/fireworks.py index f8122e8e0c..3e0d19ab7f 100644 --- a/phi/model/fireworks/fireworks.py +++ b/phi/model/fireworks/fireworks.py @@ -1,5 +1,5 @@ from os import getenv -from typing import Optional, List, Iterator, Any +from typing import Optional, List, Iterator from phi.model.message import Message from phi.model.openai import OpenAILike diff --git a/phi/model/google/gemini_openai.py b/phi/model/google/gemini_openai.py index 1cc0934ce1..2104692752 100644 --- a/phi/model/google/gemini_openai.py +++ b/phi/model/google/gemini_openai.py @@ -1,5 +1,5 @@ from os import getenv -from typing import Optional, Any +from typing import Optional from phi.model.openai.like import OpenAILike diff --git a/phi/model/nvidia/nvidia.py b/phi/model/nvidia/nvidia.py index b8ccad2947..2b447b5a61 100644 --- a/phi/model/nvidia/nvidia.py +++ b/phi/model/nvidia/nvidia.py @@ -1,5 +1,5 @@ from os import getenv -from typing import Optional, Any +from typing import Optional from phi.model.openai.like import OpenAILike diff --git a/phi/model/openrouter/openrouter.py b/phi/model/openrouter/openrouter.py index d34546c03f..fd511d766f 100644 --- a/phi/model/openrouter/openrouter.py +++ b/phi/model/openrouter/openrouter.py @@ -1,5 +1,5 @@ from os import getenv -from typing import Optional, Any +from typing import Optional from phi.model.openai.like import OpenAILike diff --git a/phi/model/sambanova/sambanova.py b/phi/model/sambanova/sambanova.py index fc670944f2..3b34329791 100644 --- a/phi/model/sambanova/sambanova.py +++ b/phi/model/sambanova/sambanova.py @@ -1,4 +1,4 @@ -from typing import Optional, Any +from typing import Optional from os import getenv from phi.model.openai.like import OpenAILike diff --git a/phi/model/vertexai/gemini.py b/phi/model/vertexai/gemini.py index e8c0e1eae0..c8d17a8953 100644 --- a/phi/model/vertexai/gemini.py +++ b/phi/model/vertexai/gemini.py @@ -22,7 +22,9 @@ Part, ) except (ModuleNotFoundError, ImportError): - raise ImportError("`google-cloud-aiplatform` not installed. Please install using `pip install google-cloud-aiplatform`") + raise ImportError( + "`google-cloud-aiplatform` not installed. Please install using `pip install google-cloud-aiplatform`" + ) @dataclass diff --git a/phi/model/xai/xai.py b/phi/model/xai/xai.py index cb418b769f..dd4851fa7f 100644 --- a/phi/model/xai/xai.py +++ b/phi/model/xai/xai.py @@ -1,5 +1,5 @@ from os import getenv -from typing import Optional, Any +from typing import Optional from phi.model.openai.like import OpenAILike