hkai20000 commited on
Commit
76ce598
·
verified ·
1 Parent(s): 04b8607

Update rag.py

Browse files
Files changed (1) hide show
  1. rag.py +1 -7
rag.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  from typing import List, Dict, Tuple
3
  import numpy as np
4
  from openai import OpenAI
5
- from services.faq_store import FAQ_ENTRIES, FAQ_VECS
6
 
7
  RAG_CONFIDENCE_THRESHOLD = 0.6
8
  MAX_FAQ_MATCHES = 3
@@ -23,7 +23,6 @@ FALLBACK_MESSAGE = (
23
 
24
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
25
 
26
- # In-memory embedding cache for repeated queries
27
  _query_cache: dict[str, np.ndarray] = {}
28
 
29
 
@@ -32,7 +31,6 @@ def cosine(a: np.ndarray, b: np.ndarray) -> float:
32
 
33
 
34
  async def get_answer(question: str, history: List[Dict]) -> Tuple[str, List[Dict]]:
35
- # Embed query (with in-memory cache)
36
  if question in _query_cache:
37
  vec = _query_cache[question]
38
  else:
@@ -40,21 +38,17 @@ async def get_answer(question: str, history: List[Dict]) -> Tuple[str, List[Dict
40
  vec = np.array(resp.data[0].embedding, dtype=np.float32)
41
  _query_cache[question] = vec
42
 
43
- # Cosine similarity against all FAQ vectors
44
  scores = [(fid, cosine(vec, fvec)) for fid, fvec in FAQ_VECS]
45
  scores.sort(key=lambda x: x[1], reverse=True)
46
 
47
- # Fallback if no FAQ meets the confidence threshold
48
  if not scores or scores[0][1] < RAG_CONFIDENCE_THRESHOLD:
49
  return FALLBACK_MESSAGE, []
50
 
51
- # Gather top matches
52
  matches = []
53
  for fid, score in scores[:MAX_FAQ_MATCHES]:
54
  faq = FAQ_ENTRIES[fid]
55
  matches.append({"id": fid, "answer": faq["answer"], "source": faq["source"], "score": score})
56
 
57
- # Build message list for GPT
58
  messages: List[Dict] = [{"role": "system", "content": SYSTEM_PROMPT}]
59
  for msg in history:
60
  messages.append({"role": msg["role"], "content": msg["content"]})
 
2
  from typing import List, Dict, Tuple
3
  import numpy as np
4
  from openai import OpenAI
5
+ from faq_store import FAQ_ENTRIES, FAQ_VECS
6
 
7
  RAG_CONFIDENCE_THRESHOLD = 0.6
8
  MAX_FAQ_MATCHES = 3
 
23
 
24
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
25
 
 
26
  _query_cache: dict[str, np.ndarray] = {}
27
 
28
 
 
31
 
32
 
33
  async def get_answer(question: str, history: List[Dict]) -> Tuple[str, List[Dict]]:
 
34
  if question in _query_cache:
35
  vec = _query_cache[question]
36
  else:
 
38
  vec = np.array(resp.data[0].embedding, dtype=np.float32)
39
  _query_cache[question] = vec
40
 
 
41
  scores = [(fid, cosine(vec, fvec)) for fid, fvec in FAQ_VECS]
42
  scores.sort(key=lambda x: x[1], reverse=True)
43
 
 
44
  if not scores or scores[0][1] < RAG_CONFIDENCE_THRESHOLD:
45
  return FALLBACK_MESSAGE, []
46
 
 
47
  matches = []
48
  for fid, score in scores[:MAX_FAQ_MATCHES]:
49
  faq = FAQ_ENTRIES[fid]
50
  matches.append({"id": fid, "answer": faq["answer"], "source": faq["source"], "score": score})
51
 
 
52
  messages: List[Dict] = [{"role": "system", "content": SYSTEM_PROMPT}]
53
  for msg in history:
54
  messages.append({"role": msg["role"], "content": msg["content"]})