Update agent_langchain.py
Browse files- agent_langchain.py +25 -17
agent_langchain.py
CHANGED
|
@@ -147,29 +147,37 @@ def initialize_kb(json_texts: list, metadatas: list):
|
|
| 147 |
|
| 148 |
def query_kb(text: str, top_k: int = 1):
|
| 149 |
"""
|
| 150 |
-
Query the knowledge base
|
| 151 |
-
Returns
|
| 152 |
"""
|
| 153 |
global kb_collection
|
| 154 |
-
if not kb_collection:
|
| 155 |
-
return {"answer":
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
if not results or not results.get("documents") or len(results["documents"][0]) == 0:
|
| 161 |
-
return {"answer": "No relevant KB entry found.", "confidence": 0.0, "metadata": {}}
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
except Exception as e:
|
| 171 |
-
print("⚠️ KB query failed:", e)
|
| 172 |
-
return {"answer": "Error querying KB.", "confidence": 0.0, "metadata": {}}
|
| 173 |
|
| 174 |
|
| 175 |
# ==============================================================
|
|
|
|
| 147 |
|
| 148 |
def query_kb(text: str, top_k: int = 1):
|
| 149 |
"""
|
| 150 |
+
Query the Chroma knowledge base using SentenceTransformer embeddings.
|
| 151 |
+
Returns answer + confidence.
|
| 152 |
"""
|
| 153 |
global kb_collection
|
| 154 |
+
if not kb_collection or kb_collection.count() == 0:
|
| 155 |
+
return {"answer": None, "confidence": 0.0}
|
| 156 |
|
| 157 |
+
# Embed the query manually using SentenceTransformer
|
| 158 |
+
encoder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 159 |
+
query_embedding = encoder.encode([text])[0]
|
|
|
|
|
|
|
| 160 |
|
| 161 |
+
# Query Chroma by embeddings
|
| 162 |
+
results = kb_collection.query(
|
| 163 |
+
query_embeddings=[query_embedding],
|
| 164 |
+
n_results=top_k
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
if not results or not results.get("documents") or len(results["documents"][0]) == 0:
|
| 168 |
+
return {"answer": None, "confidence": 0.0}
|
| 169 |
+
|
| 170 |
+
# Extract top document and metadata
|
| 171 |
+
answer = results["documents"][0][0]
|
| 172 |
+
metadata = results["metadatas"][0][0]
|
| 173 |
|
| 174 |
+
# Compute cosine similarity manually for confidence
|
| 175 |
+
stored_embedding = np.array(results["embeddings"][0][0])
|
| 176 |
+
query_vec = np.array(query_embedding)
|
| 177 |
+
confidence = float(np.dot(query_vec, stored_embedding) /
|
| 178 |
+
(np.linalg.norm(query_vec) * np.linalg.norm(stored_embedding)))
|
| 179 |
|
| 180 |
+
return {"answer": answer, "confidence": round(confidence, 3), "metadata": metadata}
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
|
| 183 |
# ==============================================================
|