afaceuser09 commited on
Commit
f57e5ca
·
verified ·
1 Parent(s): 8cf3145

Update reasoning_engine.py

Browse files
Files changed (1) hide show
  1. reasoning_engine.py +26 -5
reasoning_engine.py CHANGED
@@ -350,11 +350,29 @@ def retrieve_experiences(query: str, top_k: int = 3) -> List[Dict[str, Any]]:
350
  if not rows:
351
  return []
352
 
 
 
 
 
 
 
 
353
  qv = _embed(query)
354
- scored = []
355
- for row in rows:
356
- rv = _embed(row[1])
357
- scored.append((float(np.dot(qv, rv)), row))
 
 
 
 
 
 
 
 
 
 
 
358
  scored.sort(key=lambda x: x[0], reverse=True)
359
 
360
  return [
@@ -366,6 +384,7 @@ def retrieve_experiences(query: str, top_k: int = 3) -> List[Dict[str, Any]]:
366
  "answer": row[4],
367
  "quality": row[5],
368
  }
 
369
  for score, row in scored[:top_k]
370
  ]
371
  except Exception as exc:
@@ -539,6 +558,9 @@ Then give the final answer in a useful format.
539
 
540
 
541
  def plan(query: str, history: List[Dict[str, str]]) -> Dict[str, Any]:
 
 
 
542
  experiences = retrieve_experiences(query, top_k=3)
543
  experience_text = json.dumps(experiences, ensure_ascii=False, indent=2)
544
 
@@ -579,7 +601,6 @@ def plan(query: str, history: List[Dict[str, str]]) -> Dict[str, Any]:
579
  "experiences": experiences,
580
  }
581
 
582
-
583
  def analyze(query: str, plan_data: Dict[str, Any], evidence: str, history: List[Dict[str, str]]) -> str:
584
  messages = [
585
  {"role": "system", "content": ANALYST_SYSTEM},
 
350
  if not rows:
351
  return []
352
 
353
+ # 1. Get the embedding model from STATE
354
+ embed_model = STATE.get("embed_model")
355
+ if embed_model is None:
356
+ # Fallback if model isn't initialized yet
357
+ return []
358
+
359
+ # 2. Embed the incoming user query
360
  qv = _embed(query)
361
+
362
+ # 3. Batch embed all historical queries retrieved from the database
363
+ db_queries = [row[1] for row in rows]
364
+ rv_matrix = embed_model.encode(
365
+ db_queries,
366
+ normalize_embeddings=True,
367
+ convert_to_numpy=True,
368
+ show_progress_bar=False
369
+ )
370
+
371
+ # 4. Use matrix multiplication to calculate all scores simultaneously
372
+ scores = rv_matrix @ qv
373
+
374
+ # 5. Pair scores back up with their rows and sort
375
+ scored = [(float(score), row) for score, row in zip(scores, rows)]
376
  scored.sort(key=lambda x: x[0], reverse=True)
377
 
378
  return [
 
384
  "answer": row[4],
385
  "quality": row[5],
386
  }
387
+ # Slid down to only return the top_k best matches
388
  for score, row in scored[:top_k]
389
  ]
390
  except Exception as exc:
 
558
 
559
 
560
  def plan(query: str, history: List[Dict[str, str]]) -> Dict[str, Any]:
561
+ # FIX: Initialize models here so retrieve_experiences has access to the embedding model
562
+ initialize_model()
563
+
564
  experiences = retrieve_experiences(query, top_k=3)
565
  experience_text = json.dumps(experiences, ensure_ascii=False, indent=2)
566
 
 
601
  "experiences": experiences,
602
  }
603
 
 
604
  def analyze(query: str, plan_data: Dict[str, Any], evidence: str, history: List[Dict[str, str]]) -> str:
605
  messages = [
606
  {"role": "system", "content": ANALYST_SYSTEM},