Bima Ardhia commited on
Commit
c26528a
·
1 Parent(s): 24bdd64

benarkan struktur

Browse files
Files changed (1) hide show
  1. api/api_mage_x.py +13 -12
api/api_mage_x.py CHANGED
@@ -138,6 +138,8 @@ def fetch_and_embed_data(user_id):
138
  def create_embeddings(text):
139
  return embedding_model.embed_query(text)
140
 
 
 
141
  def recommend_similar_items(user_preferences, desired_collection_types):
142
  # Menggabungkan embedding untuk semua referensi pengguna
143
  combined_embeddings = []
@@ -168,11 +170,16 @@ def recommend_similar_items(user_preferences, desired_collection_types):
168
  for match in all_items_response['matches']:
169
  collection_type = match['metadata']['collection_type']
170
  if collection_type in desired_collection_types:
171
- filtered_matches.append({
 
 
 
 
172
  "firebase_id": match['metadata']['firebase_id'],
173
  "score": match['score'],
174
- "text": match['metadata'].get('text', '') # Tambahkan metadata 'text'
175
- })
 
176
 
177
  return filtered_matches
178
 
@@ -218,15 +225,8 @@ async def get_recommendations(user_input: UserInput):
218
  desired_collection_types=[collection_name]
219
  )
220
 
221
- # Ambil hingga 20 rekomendasi, sertakan firebase_id, score, dan text
222
- recommendations[category] = [
223
- {
224
- "firebase_id": item["firebase_id"],
225
- "score": item["score"],
226
- "text": item["text"]
227
- }
228
- for item in sorted(items, key=lambda x: x["score"], reverse=True)[:20]
229
- ]
230
 
231
  except Exception as e:
232
  recommendations[category] = {"error": str(e)}
@@ -238,6 +238,7 @@ async def get_recommendations(user_input: UserInput):
238
 
239
 
240
 
 
241
  # Endpoint to initialize a new chat session
242
  @app.post("/initialize_session")
243
  def initialize_session(request: NewSessionRequest):
 
138
  def create_embeddings(text):
139
  return embedding_model.embed_query(text)
140
 
141
+ import json
142
+
143
  def recommend_similar_items(user_preferences, desired_collection_types):
144
  # Menggabungkan embedding untuk semua referensi pengguna
145
  combined_embeddings = []
 
170
  for match in all_items_response['matches']:
171
  collection_type = match['metadata']['collection_type']
172
  if collection_type in desired_collection_types:
173
+ # Parse metadata text to dictionary
174
+ metadata_text = match['metadata'].get('text', '{}')
175
+ metadata_dict = json.loads(metadata_text)
176
+ # Add firebase_id and score to the parsed metadata
177
+ parsed_result = {
178
  "firebase_id": match['metadata']['firebase_id'],
179
  "score": match['score'],
180
+ **metadata_dict # Spread parsed metadata fields into the result
181
+ }
182
+ filtered_matches.append(parsed_result)
183
 
184
  return filtered_matches
185
 
 
225
  desired_collection_types=[collection_name]
226
  )
227
 
228
+ # Ambil hingga 20 rekomendasi
229
+ recommendations[category] = sorted(items, key=lambda x: x["score"], reverse=True)[:20]
 
 
 
 
 
 
 
230
 
231
  except Exception as e:
232
  recommendations[category] = {"error": str(e)}
 
238
 
239
 
240
 
241
+
242
  # Endpoint to initialize a new chat session
243
  @app.post("/initialize_session")
244
  def initialize_session(request: NewSessionRequest):