diginoron commited on
Commit
185ff32
·
verified ·
1 Parent(s): 014f4b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -4
app.py CHANGED
@@ -36,13 +36,22 @@ def retrieve_answer(query, threshold=0.65, top_k=1):
36
  query_embedding = model.encode([query])[0]
37
  result = index.query(vector=query_embedding.tolist(), top_k=top_k, include_metadata=True)
38
 
39
- # لاگ کامل نتیجه Pinecone
 
 
 
 
 
40
  print("=== Pinecone query result ===")
41
- print(json.dumps(result, indent=2, ensure_ascii=False))
 
 
 
42
  print("============================")
43
 
44
- if result.get('matches') and len(result['matches']) > 0 and result['matches'][0]['score'] > threshold:
45
- metadata = result['matches'][0]['metadata']
 
46
  print("Matched answer:", metadata.get('answer'))
47
  return metadata.get('answer', 'پاسخ یافت نشد')
48
  else:
 
36
  query_embedding = model.encode([query])[0]
37
  result = index.query(vector=query_embedding.tolist(), top_k=top_k, include_metadata=True)
38
 
39
+ # تبدیل به دیکشنری برای لاگ امن
40
+ try:
41
+ result_dict = result.to_dict()
42
+ except Exception:
43
+ result_dict = str(result)
44
+
45
  print("=== Pinecone query result ===")
46
+ if isinstance(result_dict, dict):
47
+ print(json.dumps(result_dict, indent=2, ensure_ascii=False))
48
+ else:
49
+ print(result_dict)
50
  print("============================")
51
 
52
+ # بررسی و دسترسی به نتیجه به صورت attribute
53
+ if hasattr(result, 'matches') and result.matches and len(result.matches) > 0 and result.matches[0].score > threshold:
54
+ metadata = result.matches[0].metadata
55
  print("Matched answer:", metadata.get('answer'))
56
  return metadata.get('answer', 'پاسخ یافت نشد')
57
  else: