Skip to content

Commit

Permalink
Allow an override on the API key and version bump (#38)
Browse files Browse the repository at this point in the history
* Allow an override on the API key

* version bump

* Add future import

* Use old school type hint
  • Loading branch information
ReinderVosDeWael authored Feb 8, 2024
1 parent f4e2e8b commit 6cace2d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "cloai"
version = "0.0.1a12"
version = "0.0.1a13"
description = "A CLI for OpenAI's API"
authors = ["Reinder Vos de Wael <[email protected]>"]
license = "LGPL-2.1"
Expand Down
6 changes: 3 additions & 3 deletions src/cloai/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
import pathlib
from importlib import metadata
from typing import Literal
from typing import Literal, Optional

import pydantic
import pydantic_settings
Expand Down Expand Up @@ -86,8 +86,8 @@ class Settings(pydantic_settings.BaseSettings):
LOGGER_NAME: str = "cloai"
LOGGER_VERBOSITY: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "INFO"

OPENAI_API_KEY: pydantic.SecretStr = pydantic.Field(
...,
OPENAI_API_KEY: Optional[pydantic.SecretStr] = pydantic.Field( # noqa: UP007 Disable because pydantic and future annotations don't play well together.
None,
json_schema_extra={
"env": "OPENAI_API_KEY",
"description": "The API key for OpenAI.",
Expand Down
12 changes: 10 additions & 2 deletions src/cloai/openai_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@ class OpenAIBaseClass(abc.ABC):
client: The OpenAI client used to interact with the model.
"""

def __init__(self) -> None:
def __init__(self, api_key: str | None = None) -> None:
"""Initializes a new instance of the OpenAIBaseClass class."""
self.client = openai.AsyncOpenAI(api_key=OPENAI_API_KEY.get_secret_value())
if api_key:
key = api_key
elif OPENAI_API_KEY:
key = OPENAI_API_KEY.get_secret_value()
else:
msg = "No API key provided."
raise exceptions.OpenAIError(msg)

self.client = openai.AsyncOpenAI(api_key=key)

@abc.abstractmethod
async def run(self, *_args: Any, **_kwargs: Any) -> Any: # noqa: ANN401
Expand Down

0 comments on commit 6cace2d

Please sign in to comment.