Commit
·
65cfbc3
1
Parent(s):
f0d81d7
Use responses API for OpenAI
Browse files- app/smart_recommendation.py +17 -11
- requirements.txt +1 -2
app/smart_recommendation.py
CHANGED
|
@@ -5,15 +5,13 @@ from collections import defaultdict
|
|
| 5 |
from datetime import datetime, timedelta
|
| 6 |
from typing import Dict, List
|
| 7 |
|
| 8 |
-
import
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
|
| 11 |
from app.models import BudgetRecommendation, CategoryExpense
|
| 12 |
|
| 13 |
load_dotenv()
|
| 14 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 15 |
-
if OPENAI_API_KEY:
|
| 16 |
-
openai.api_key = OPENAI_API_KEY
|
| 17 |
|
| 18 |
class SmartBudgetRecommender:
|
| 19 |
"""
|
|
@@ -262,15 +260,23 @@ class SmartBudgetRecommender:
|
|
| 262 |
)
|
| 263 |
|
| 264 |
try:
|
| 265 |
-
response =
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
)
|
| 273 |
-
|
|
|
|
|
|
|
| 274 |
return json.loads(content)
|
| 275 |
except Exception as exc:
|
| 276 |
print(f"OpenAI recommendation error for {category}: {exc}")
|
|
|
|
| 5 |
from datetime import datetime, timedelta
|
| 6 |
from typing import Dict, List
|
| 7 |
|
| 8 |
+
import requests
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
|
| 11 |
from app.models import BudgetRecommendation, CategoryExpense
|
| 12 |
|
| 13 |
load_dotenv()
|
| 14 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
|
| 15 |
|
| 16 |
class SmartBudgetRecommender:
|
| 17 |
"""
|
|
|
|
| 260 |
)
|
| 261 |
|
| 262 |
try:
|
| 263 |
+
response = requests.post(
|
| 264 |
+
"https://api.openai.com/v1/responses",
|
| 265 |
+
headers={
|
| 266 |
+
"Authorization": f"Bearer {OPENAI_API_KEY}",
|
| 267 |
+
"Content-Type": "application/json",
|
| 268 |
+
},
|
| 269 |
+
json={
|
| 270 |
+
"model": "gpt-4.1-mini",
|
| 271 |
+
"input": prompt,
|
| 272 |
+
"temperature": 0.1,
|
| 273 |
+
"response_format": {"type": "json_object"},
|
| 274 |
+
},
|
| 275 |
+
timeout=30,
|
| 276 |
)
|
| 277 |
+
response.raise_for_status()
|
| 278 |
+
data = response.json()
|
| 279 |
+
content = data["output"][0]["content"][0]["text"]
|
| 280 |
return json.loads(content)
|
| 281 |
except Exception as exc:
|
| 282 |
print(f"OpenAI recommendation error for {category}: {exc}")
|
requirements.txt
CHANGED
|
@@ -3,7 +3,6 @@ uvicorn[standard]==0.24.0
|
|
| 3 |
pymongo==4.6.0
|
| 4 |
pydantic==2.5.0
|
| 5 |
python-multipart==0.0.6
|
| 6 |
-
openai==1.51.0
|
| 7 |
python-dotenv==1.0.1
|
| 8 |
-
|
| 9 |
|
|
|
|
| 3 |
pymongo==4.6.0
|
| 4 |
pydantic==2.5.0
|
| 5 |
python-multipart==0.0.6
|
|
|
|
| 6 |
python-dotenv==1.0.1
|
| 7 |
+
requests==2.32.3
|
| 8 |
|