Skip to content

Commit 79f506e

Browse files
committed
refactor: replace dotenv with python-decouple
1 parent 575e888 commit 79f506e

File tree

3 files changed

+39
-43
lines changed

3 files changed

+39
-43
lines changed

models/const.py

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,36 @@
22
# pylint: disable=too-few-public-methods
33
"""Sales Support Model (hsr) for the LangChain project."""
44

5-
import os
65
import re
76

8-
from dotenv import find_dotenv, load_dotenv
9-
10-
11-
class ConfigurationError(Exception):
12-
"""Exception raised for errors in the configuration."""
13-
14-
def __init__(self, message):
15-
self.message = message
16-
super().__init__(self.message)
17-
18-
19-
# pylint: disable=duplicate-code
20-
dotenv_path = find_dotenv()
21-
if os.path.exists(dotenv_path):
22-
load_dotenv(dotenv_path=dotenv_path, verbose=True)
23-
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
24-
OPENAI_API_ORGANIZATION = os.environ["OPENAI_API_ORGANIZATION"]
25-
PINECONE_API_KEY = os.environ["PINECONE_API_KEY"]
26-
PINECONE_ENVIRONMENT = os.environ["PINECONE_ENVIRONMENT"]
27-
PINECONE_INDEX_NAME = os.environ.get("PINECONE_INDEX_NAME", "hsr")
28-
PINECONE_VECTORSTORE_TEXT_KEY = os.environ.get("PINECONE_VECTORSTORE_TEXT_KEY", "lc_id")
29-
PINECONE_METRIC = os.environ.get("PINECONE_METRIC", "dotproduct")
30-
PINECONE_DIMENSIONS = int(os.environ.get("PINECONE_DIMENSIONS", 1536))
31-
OPENAI_CHAT_MODEL_NAME = os.environ.get("OPENAI_CHAT_MODEL_NAME", "gpt-3.5-turbo")
32-
OPENAI_PROMPT_MODEL_NAME = os.environ.get("OPENAI_PROMPT_MODEL_NAME", "text-davinci-003")
33-
OPENAI_CHAT_TEMPERATURE = float(os.environ.get("OPENAI_CHAT_TEMPERATURE", 0.0))
34-
OPENAI_CHAT_MAX_RETRIES = int(os.environ.get("OPENAI_CHAT_MAX_RETRIES", 3))
35-
OPENAI_CHAT_CACHE = bool(os.environ.get("OPENAI_CHAT_CACHE", True))
36-
DEBUG_MODE = os.environ.get("DEBUG_MODE", "False") == "True"
37-
38-
if not re.match(r"^sk-\w+$", OPENAI_API_KEY):
39-
raise ConfigurationError("OPENAI_API_KEY is not set. Please add your OpenAI API key to the .env file.")
40-
if not re.match(r"^org-\w+$", OPENAI_API_ORGANIZATION):
41-
raise ConfigurationError(
42-
"OPENAI_API_ORGANIZATION is not set. Please add your OpenAI API organization to the .env file."
43-
)
44-
if not re.match(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", PINECONE_API_KEY):
45-
raise ConfigurationError("PINECONE_API_KEY is not set. Please add your Pinecone API key to the .env file.")
46-
47-
else:
48-
raise FileNotFoundError("No .env file found in root directory of repository")
7+
from decouple import config
8+
9+
from models.exceptions import ConfigurationError
10+
11+
12+
OPENAI_API_KEY = config("OPENAI_API_KEY")
13+
OPENAI_API_ORGANIZATION = config("OPENAI_API_ORGANIZATION")
14+
PINECONE_API_KEY = config("PINECONE_API_KEY")
15+
PINECONE_ENVIRONMENT = config("PINECONE_ENVIRONMENT")
16+
PINECONE_INDEX_NAME = config("PINECONE_INDEX_NAME", default="rag")
17+
PINECONE_VECTORSTORE_TEXT_KEY = config("PINECONE_VECTORSTORE_TEXT_KEY", default="lc_id")
18+
PINECONE_METRIC = config("PINECONE_METRIC", default="dotproduct")
19+
PINECONE_DIMENSIONS = config("PINECONE_DIMENSIONS", default=1536, cast=int)
20+
OPENAI_CHAT_MODEL_NAME = config("OPENAI_CHAT_MODEL_NAME", default="gpt-3.5-turbo")
21+
OPENAI_PROMPT_MODEL_NAME = config("OPENAI_PROMPT_MODEL_NAME", default="text-davinci-003")
22+
OPENAI_CHAT_TEMPERATURE = config("OPENAI_CHAT_TEMPERATURE", default=0.0, cast=float)
23+
OPENAI_CHAT_MAX_RETRIES = config("OPENAI_CHAT_MAX_RETRIES", default=3, cast=int)
24+
OPENAI_CHAT_CACHE = config("OPENAI_CHAT_CACHE", default=True, cast=bool)
25+
DEBUG_MODE = config("DEBUG_MODE", default=False, cast=bool)
26+
27+
if not re.match(r"^sk-\w+$", OPENAI_API_KEY):
28+
raise ConfigurationError("OPENAI_API_KEY is not set. Please add your OpenAI API key to the .env file.")
29+
if not re.match(r"^org-\w+$", OPENAI_API_ORGANIZATION):
30+
raise ConfigurationError(
31+
"OPENAI_API_ORGANIZATION is not set. Please add your OpenAI API organization to the .env file."
32+
)
33+
if not re.match(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", PINECONE_API_KEY):
34+
raise ConfigurationError("PINECONE_API_KEY is not set. Please add your Pinecone API key to the .env file.")
4935

5036

5137
class ReadOnly(type):

models/exceptions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
"""Module exceptions.py"""
3+
4+
5+
class ConfigurationError(Exception):
6+
"""Exception raised for errors in the configuration."""
7+
8+
def __init__(self, message):
9+
self.message = message
10+
super().__init__(self.message)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ codespell==2.2.6
1919

2020
# production
2121
# ------------
22+
python-decouple==3.8
2223
langchain==0.0.345
2324
langchainhub==0.1.14
2425
langchain-experimental==0.0.43
@@ -27,5 +28,4 @@ pinecone-client==2.2.4
2728
pinecone-text==0.7.0
2829
pydantic==2.5.2
2930
pypdf==3.17.1
30-
python-dotenv==1.0.0
3131
tiktoken==0.5.2

0 commit comments

Comments
 (0)