-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_v1.py
259 lines (214 loc) · 7.91 KB
/
api_v1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
from fastapi import FastAPI, Query, HTTPException
from pydantic import BaseModel
import psycopg2
import json
from typing import List, Optional, Dict
import re
import uvicorn
from utility_v1 import *
global documents, document_embeddings, index
documents = fetch_data_as_documents()
document_embeddings = generate_document_embeddings(documents)
index = create_faiss_index(document_embeddings)
# Load database credentials
with open('data/creds.json') as f:
creds = json.load(f)
# Function to connect to the PostgreSQL database
def connect_db():
conn = psycopg2.connect(
dbname=creds['database'],
user=creds['user'],
password=creds['password'],
host=creds['host'],
port=creds['port']
)
return conn
app = FastAPI()
# Define Pydantic models for product and review
class Product(BaseModel):
id: int
title: str
price: float
overall_rating: Optional[float] # Change to float
total_reviews: Optional[int] # Change to int
availability: Optional[str]
model: Optional[str]
material: Optional[str]
item_length: Optional[str]
length: Optional[str]
clasp: Optional[str]
model_number: Optional[str]
link: Optional[str]
class Review(BaseModel):
reviewer_name: str
review_text: str
review_rating: str
review_date: str
class AskRequest(BaseModel):
query: str
# Helper function to extract numeric values
def extract_numeric(value: str) -> float:
match = re.search(r"(\d+(\.\d+)?)", value)
return float(match.group(1)) if match else 0.0
def extract_review_count(value: str) -> int:
match = re.search(r"(\d{1,3}(,\d{3})*)", value)
return int(match.group(1).replace(',', '')) if match else 0
# GET /products
@app.get("/products", response_model=List[Product])
async def search_products(
brand: str = Query(None),
model: str = Query(None),
min_price: float = Query(None),
max_price: float = Query(None),
min_rating: float = Query(None),
page: int = Query(1, ge=1),
limit: int = Query(10, ge=1)
):
offset = (page - 1) * limit
params = [] # Start with an empty list
conditions = []
# Add brand filtering condition
if brand:
conditions.append("title ILIKE %s")
params.append(f"%{brand}%")
# Add model filtering condition
if model:
conditions.append("model ILIKE %s")
params.append(f"%{model}%")
# Add price filtering condition
if min_price is not None:
conditions.append("CAST(price AS FLOAT) >= %s")
params.append(min_price)
if max_price is not None:
conditions.append("CAST(price AS FLOAT) <= %s")
params.append(max_price)
# Add rating filtering condition
if min_rating is not None:
conditions.append("CAST(SUBSTRING(overall_rating FROM '([0-9]+(\\.[0-9]+)?)') AS FLOAT) >= %s")
params.append(min_rating)
# Add limit and offset at the end
params.append(limit)
params.append(offset)
# Build the WHERE clause from conditions
where_clause = " AND ".join(conditions) if conditions else "TRUE"
# Final query
query = f"""
SELECT id, title, price,
CAST(SUBSTRING(overall_rating FROM '([0-9]+(\\.[0-9]+)?)') AS FLOAT) AS overall_rating,
CAST(REPLACE(SUBSTRING(total_reviews FROM '([0-9,]+)')::TEXT, ',', '') AS INTEGER) AS total_reviews,
availability, model, material, item_length, length, clasp, model_number, link
FROM amazon_watches
WHERE {where_clause}
ORDER BY total_reviews DESC, overall_rating DESC
LIMIT %s OFFSET %s;
"""
# Execute query with the prepared params list
conn = connect_db()
try:
with conn.cursor() as cursor:
cursor.execute(query, params)
products = cursor.fetchall()
result = [
{
"id": row[0],
"title": row[1],
"price": row[2],
"overall_rating": row[3],
"total_reviews": row[4],
"availability": row[5],
"model": row[6],
"material": row[7],
"item_length": row[8],
"length": row[9],
"clasp": row[10],
"model_number": row[11],
"link": row[12],
} for row in products
]
return result
finally:
conn.close()
# GET /products/top
@app.get("/products/top", response_model=List[Product])
async def get_top_products(limit: int = Query(10, ge=1)):
conn = connect_db()
try:
with conn.cursor() as cursor:
query = """
SELECT id, title, price,
CAST(SUBSTRING(overall_rating FROM '([0-9]+(\\.[0-9]+)?)') AS FLOAT) AS overall_rating,
CAST(REPLACE(SUBSTRING(total_reviews FROM '([0-9,]+)')::TEXT, ',', '') AS INTEGER) AS total_reviews,
availability, model, material, item_length, length, clasp, model_number, link
FROM amazon_watches
ORDER BY total_reviews DESC, overall_rating DESC
LIMIT %s;
"""
cursor.execute(query, (limit,))
products = cursor.fetchall()
result = [
{
"id": row[0],
"title": row[1],
"price": row[2],
"overall_rating": row[3],
"total_reviews": row[4],
"availability": row[5],
"model": row[6],
"material": row[7],
"item_length": row[8],
"length": row[9],
"clasp": row[10],
"model_number": row[11],
"link": row[12],
} for row in products
]
return result
finally:
conn.close()
# GET /products/{product_id}/reviews
@app.get("/products/{product_id}/reviews", response_model=List[Review])
async def get_product_reviews(product_id: int, page: int = Query(1, ge=1), limit: int = Query(10, ge=1)):
conn = connect_db()
try:
with conn.cursor() as cursor:
query = """
SELECT reviewer_name_1, review_text_1, review_rating_1, review_date_1
FROM amazon_watches
WHERE id = %s
UNION ALL
SELECT reviewer_name_2, review_text_2, review_rating_2, review_date_2
FROM amazon_watches
WHERE id = %s
UNION ALL
SELECT reviewer_name_3, review_text_3, review_rating_3, review_date_3
FROM amazon_watches
WHERE id = %s
LIMIT %s OFFSET %s;
"""
cursor.execute(query, (product_id, product_id, product_id, limit, (page - 1) * limit))
reviews = cursor.fetchall()
result = [
{
"reviewer_name": row[0],
"review_text": row[1],
"review_rating": row[2],
"review_date": row[3],
} for row in reviews
]
return result
finally:
conn.close()
# POST /ask
@app.post("/ask")
async def ask_question(request: AskRequest):
query = request.query
# Step 3: Generate the query embedding for the input query
query_embedding = generate_query_embedding(query)
# Step 4: Retrieve relevant document indices using the FAISS index
top_docs_indices = search(query_embedding, index)
# Step 5: Fetch the top documents based on indices
top_docs = [documents[i] for i in top_docs_indices]
unique_top_docs = list(dict.fromkeys(top_docs))
return {"query": query, "retrieved_documents": unique_top_docs}
if __name__ == '__main__':
uvicorn.run(app, host="127.0.0.1", port=8000)