LogicGoInfotechSpaces commited on
Commit
a005924
·
1 Parent(s): d5ef069

Fix OpenAI client init

Browse files
Files changed (2) hide show
  1. app/smart_recommendation.py +10 -2
  2. requirements.txt +1 -0
app/smart_recommendation.py CHANGED
@@ -12,7 +12,15 @@ from app.models import BudgetRecommendation, CategoryExpense
12
 
13
  load_dotenv()
14
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
15
- openai_client = OpenAI(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else None
 
 
 
 
 
 
 
 
16
 
17
  class SmartBudgetRecommender:
18
  """
@@ -24,7 +32,7 @@ class SmartBudgetRecommender:
24
 
25
  def __init__(self, db):
26
  self.db = db
27
- self.openai_client = openai_client
28
 
29
  def get_recommendations(self, user_id: str, month: int, year: int) -> List[BudgetRecommendation]:
30
  """
 
12
 
13
  load_dotenv()
14
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
15
+
16
+ def _build_openai_client():
17
+ if not OPENAI_API_KEY:
18
+ return None
19
+ try:
20
+ return OpenAI(api_key=OPENAI_API_KEY)
21
+ except Exception as exc:
22
+ print(f"OpenAI client initialization failed: {exc}")
23
+ return None
24
 
25
  class SmartBudgetRecommender:
26
  """
 
32
 
33
  def __init__(self, db):
34
  self.db = db
35
+ self.openai_client = _build_openai_client()
36
 
37
  def get_recommendations(self, user_id: str, month: int, year: int) -> List[BudgetRecommendation]:
38
  """
requirements.txt CHANGED
@@ -5,4 +5,5 @@ pydantic==2.5.0
5
  python-multipart==0.0.6
6
  openai==1.51.0
7
  python-dotenv==1.0.1
 
8
 
 
5
  python-multipart==0.0.6
6
  openai==1.51.0
7
  python-dotenv==1.0.1
8
+ httpx==0.27.0
9