Spaces:
Paused
Paused
Update rag_engine.py
Browse files- 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(
|
| 6 |
|
| 7 |
knowledge = [
|
| 8 |
"Hello! I am ZXAI.",
|
| 9 |
-
"I am an AI
|
| 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 |
-
|
|
|
|
|
|
|
| 18 |
query_embedding = model.encode([query])
|
| 19 |
D, I = index.search(np.array(query_embedding), 1)
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
| 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
|
|
|