Skip to content

Commit

Permalink
added pydantic
Browse files Browse the repository at this point in the history
  • Loading branch information
SermetPekin committed Sep 11, 2024
1 parent 054816e commit 87e04fe
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 71 deletions.
46 changes: 24 additions & 22 deletions evdschat/common/akeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@
# limitations under the License.


from dataclasses import dataclass, field
from pydantic import BaseModel, Field
from abc import ABC
import os
from pathlib import Path
from typing import Union





class ErrorApiKey(Exception):
"""Exception raised for errors related to API key issues.
Expand All @@ -34,18 +31,16 @@ def __init__(self, message="There is an issue with the provided API key."):
self.message = message
super().__init__(self.message)

class ApiKey(ABC):
def __init__(self , key : str ) -> None:
self.key = key
self.check()

def __postinit__(self):
class ApiKey(ABC):
def __init__(self, key: str) -> None:
self.key = key
self.check()

def check(self):
if isinstance(self.key , type(None)) :
if isinstance(self.key, type(None)):
raise ErrorApiKey('Api key not set. Please see the documentation.')
if not isinstance(self.key , str ) or len(str(self.key))< 5:
if not isinstance(self.key, str) or len(str(self.key)) < 5:
raise ErrorApiKey(f"Api key {self.key} is not a valid key")
return True

Expand All @@ -56,14 +51,13 @@ def set_key(self, key: str):
self.key = key



class OpenaiApiKey(ApiKey):
def __init__(self , key : str ) -> None:
self.key = key
def __init__(self, key: str) -> None:
super().__init__(key)
self.key = key
self.check()

def check(self)-> Union[bool , None ] :
#super().__init__(self)
def check(self) -> Union[bool, None]:

if not str(self.key).startswith("sk-") and len(str(self.key)) < 6:
raise ErrorApiKey(f"{self.key} is not a valid key")
Expand All @@ -81,8 +75,8 @@ def load_api_keys() -> Union[dict[str, str], None]:

env_file = Path(".env")
load_dotenv(env_file)
openai_api_key = OpenaiApiKey( os.getenv("OPENAI_API_KEY" ))
evds_api_key = EvdsApiKey( os.getenv("EVDS_API_KEY" ) )
openai_api_key = OpenaiApiKey(os.getenv("OPENAI_API_KEY"))
evds_api_key = EvdsApiKey(os.getenv("EVDS_API_KEY"))
return {
"OPENAI_API_KEY": openai_api_key,
"EVDS_API_KEY": evds_api_key,
Expand All @@ -91,9 +85,17 @@ def load_api_keys() -> Union[dict[str, str], None]:

def get_openai_key():
d = load_api_keys()
return d["OPENAI_API_KEY"].key
return d["OPENAI_API_KEY"].key


# @dataclass

class ApiKeyManager(BaseModel):
api_key: ApiKey = Field(default_factory=lambda: ApiKey())

class Config:
arbitrary_types_allowed = True

@dataclass
class ApiKeyManager:
api_key: ApiKey = field(default_factory=ApiKey)
# def __get_pydantic_core_schema__(cls, handler):
# # Generate a schema if necessary, or skip it
# return handler.generate_schema(cls)
9 changes: 5 additions & 4 deletions evdschat/core/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@

from dataclasses import dataclass, field

from pydantic import BaseModel, Field

from ..model.chatters import ModelAbstract, OpenAI
from ..common.akeys import ApiKey, OpenaiApiKey, get_openai_key


@dataclass
class Builder:
retriever: ModelAbstract = field(default_factory=ModelAbstract)
api_key: ApiKey = field(default_factory=ApiKey)
class Builder(BaseModel):
retriever: ModelAbstract = Field(default_factory=lambda: ModelAbstract())
api_key: ApiKey = Field(default_factory=lambda: ApiKey())


def build_openai():
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ keywords = ["evdspy", "evdschat", "chatbot", "data-aggregation", "open-source",
requires-python = ">=3.10"
dependencies = [
"evdspy>=1.1.40",
"pydantic>=2.9.1",
"rich>=13.8.1",
]
[project.urls]
Expand Down
Loading

0 comments on commit 87e04fe

Please sign in to comment.