Skip to content

Commit e3afac1

Browse files
🔨 Allow setting API keys from env (#2353)
Allow setting API keys from env Signed-off-by: Ashwin Vaidya <[email protected]>
1 parent 552a1a4 commit e3afac1

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ core = [
5656
"open-clip-torch>=2.23.0,<2.26.1",
5757
]
5858
openvino = ["openvino>=2024.0", "nncf>=2.10.0", "onnx>=1.16.0"]
59-
vlm = ["ollama", "openai", "transformers"]
59+
vlm = ["ollama", "openai", "python-dotenv","transformers"]
6060
loggers = [
6161
"comet-ml>=3.31.7",
6262
"gradio>=4",

src/anomalib/models/image/vlm_ad/backends/chat_gpt.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55

66
import base64
77
import logging
8+
import os
89
from pathlib import Path
910
from typing import TYPE_CHECKING
1011

12+
from dotenv import load_dotenv
13+
1114
from anomalib.models.image.vlm_ad.utils import Prompt
1215
from anomalib.utils.exceptions import try_import
1316

@@ -27,12 +30,12 @@
2730
class ChatGPT(Backend):
2831
"""ChatGPT backend."""
2932

30-
def __init__(self, api_key: str, model_name: str) -> None:
33+
def __init__(self, model_name: str, api_key: str | None = None) -> None:
3134
"""Initialize the ChatGPT backend."""
32-
self.api_key = api_key
3335
self._ref_images_encoded: list[str] = []
3436
self.model_name: str = model_name
3537
self._client: OpenAI | None = None
38+
self.api_key = self._get_api_key(api_key)
3639

3740
@property
3841
def client(self) -> OpenAI:
@@ -86,3 +89,16 @@ def _encode_image_to_base_64(image: str | Path) -> str:
8689
"""Encode the image to base64."""
8790
image = Path(image)
8891
return base64.b64encode(image.read_bytes()).decode("utf-8")
92+
93+
def _get_api_key(self, api_key: str | None = None) -> str:
94+
if api_key is None:
95+
load_dotenv()
96+
api_key = os.getenv("OPENAI_API_KEY")
97+
if api_key is None:
98+
msg = (
99+
f"OpenAI API key must be provided to use {self.model_name}."
100+
" Please provide the API key in the constructor, or set the OPENAI_API_KEY environment variable"
101+
" or in a `.env` file."
102+
)
103+
raise ValueError(msg)
104+
return api_key

src/anomalib/models/image/vlm_ad/lightning_model.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ def _setup_vlm(model: VLMModel, api_key: str | None) -> Backend:
3636
if model == VLMModel.LLAMA_OLLAMA:
3737
return Ollama(model_name=model.value)
3838
if model == VLMModel.GPT_4O_MINI:
39-
if api_key is None:
40-
msg = f"ChatGPT API key is required to use {model.value} model."
41-
raise ValueError(msg)
4239
return ChatGPT(api_key=api_key, model_name=model.value)
4340
if model in {VLMModel.VICUNA_7B_HF, VLMModel.VICUNA_13B_HF, VLMModel.MISTRAL_7B_HF}:
4441
return Huggingface(model_name=model.value)

0 commit comments

Comments
 (0)