From dc0b11a86a2585d105020e0f6e38b92def030f52 Mon Sep 17 00:00:00 2001 From: Anton Shevtsov <32237302+MrShevan@users.noreply.github.com> Date: Sun, 14 Jan 2024 22:14:09 +0100 Subject: [PATCH] Add openai api key not found help (#815) This pull request adds check for the presence of an environment variable `OPENAI_API_KEY` and removes an unused parameter in `retry_with_exponential_backoff` function. --- python/lancedb/embeddings/gemini_text.py | 2 +- python/lancedb/embeddings/openai.py | 5 +++++ python/lancedb/embeddings/utils.py | 2 -- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/python/lancedb/embeddings/gemini_text.py b/python/lancedb/embeddings/gemini_text.py index 563f2d29..314b1e02 100644 --- a/python/lancedb/embeddings/gemini_text.py +++ b/python/lancedb/embeddings/gemini_text.py @@ -124,5 +124,5 @@ class GeminiText(TextEmbeddingFunction): genai = self.safe_import("google.generativeai", "google.generativeai") if not os.environ.get("GOOGLE_API_KEY"): - raise ValueError(api_key_not_found_help("google")) + api_key_not_found_help("google") return genai diff --git a/python/lancedb/embeddings/openai.py b/python/lancedb/embeddings/openai.py index 678fe417..c4d2f384 100644 --- a/python/lancedb/embeddings/openai.py +++ b/python/lancedb/embeddings/openai.py @@ -10,6 +10,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import os from functools import cached_property from typing import List, Union @@ -17,6 +18,7 @@ import numpy as np from .base import TextEmbeddingFunction from .registry import register +from .utils import api_key_not_found_help @register("openai") @@ -51,4 +53,7 @@ class OpenAIEmbeddings(TextEmbeddingFunction): @cached_property def _openai_client(self): openai = self.safe_import("openai") + + if not os.environ.get("OPENAI_API_KEY"): + api_key_not_found_help("openai") return openai.OpenAI() diff --git a/python/lancedb/embeddings/utils.py b/python/lancedb/embeddings/utils.py index 8f893142..5aea89d2 100644 --- a/python/lancedb/embeddings/utils.py +++ b/python/lancedb/embeddings/utils.py @@ -216,7 +216,6 @@ def retry_with_exponential_backoff( exponential_base: float = 2, jitter: bool = True, max_retries: int = 7, - # errors: tuple = (), ): """Retry a function with exponential backoff. @@ -226,7 +225,6 @@ def retry_with_exponential_backoff( exponential_base (float): The base for exponential backoff (default is 2). jitter (bool): Whether to add jitter to the delay (default is True). max_retries (int): Maximum number of retries (default is 10). - errors (tuple): Tuple of specific exceptions to retry on (default is (openai.error.RateLimitError,)). Returns: function: The decorated function.