ZBro7 commited on
Commit
3a32636
·
verified ·
1 Parent(s): 170c795

Update rag_engine.py

Browse files
Files changed (1) hide show
  1. rag_engine.py +11 -6
rag_engine.py CHANGED
@@ -2,23 +2,28 @@ from sentence_transformers import SentenceTransformer
2
  import faiss
3
  import numpy as np
4
 
5
- model = SentenceTransformer('all-MiniLM-L6-v2')
6
 
7
  knowledge = [
8
  "Hello! I am ZXAI.",
9
- "I am an AI built by ZX Labs.",
10
- "I can help you with coding, reasoning, and live data."
 
11
  ]
12
 
13
  embeddings = model.encode(knowledge)
14
  index = faiss.IndexFlatL2(384)
15
  index.add(np.array(embeddings))
16
 
17
- def rag_response(query):
 
 
18
  query_embedding = model.encode([query])
19
  D, I = index.search(np.array(query_embedding), 1)
20
 
21
- if D[0][0] < 0.8:
 
 
22
  return knowledge[I[0][0]]
 
23
  return None
24
-
 
2
  import faiss
3
  import numpy as np
4
 
5
+ model = SentenceTransformer("all-MiniLM-L6-v2")
6
 
7
  knowledge = [
8
  "Hello! I am ZXAI.",
9
+ "I am an AI developed by ZX.",
10
+ "I can help you with coding, reasoning, and live data.",
11
+ "To generate an image, type /image followed by your prompt."
12
  ]
13
 
14
  embeddings = model.encode(knowledge)
15
  index = faiss.IndexFlatL2(384)
16
  index.add(np.array(embeddings))
17
 
18
+
19
+ def rag_response(query, threshold=0.75):
20
+
21
  query_embedding = model.encode([query])
22
  D, I = index.search(np.array(query_embedding), 1)
23
 
24
+ similarity_score = D[0][0]
25
+
26
+ if similarity_score < threshold:
27
  return knowledge[I[0][0]]
28
+
29
  return None