Spaces:
Sleeping
Sleeping
Update agent_langchain.py
Browse files- agent_langchain.py +49 -15
agent_langchain.py
CHANGED
|
@@ -3,7 +3,10 @@ import requests
|
|
| 3 |
import torch
|
| 4 |
import time
|
| 5 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
|
|
|
| 6 |
import chromadb
|
|
|
|
| 7 |
from chromadb.config import Settings
|
| 8 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 9 |
from langchain.agents import initialize_agent, Tool, AgentType
|
|
@@ -87,30 +90,61 @@ def call_routing(text, retries=3, delay=5):
|
|
| 87 |
# 📚 KNOWLEDGE BASE SETUP
|
| 88 |
# ==============================================================
|
| 89 |
# Persistent Chroma client (new API)
|
| 90 |
-
|
| 91 |
-
|
| 92 |
COLLECTION_NAME = "knowledge_base"
|
| 93 |
|
| 94 |
try:
|
|
|
|
| 95 |
kb_collection = chroma_client.get_or_create_collection(COLLECTION_NAME)
|
| 96 |
except Exception as e:
|
| 97 |
kb_collection = None
|
| 98 |
print("⚠️ Could not initialize KB:", e)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
results = kb_collection.query(query_texts=[text], n_results=top_k)
|
| 106 |
-
if not results or not results.get("documents") or len(results["documents"][0]) == 0:
|
| 107 |
-
return {"answer": "No relevant KB found.", "confidence": 0.0}
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
# ==============================================================
|
| 116 |
# 🧠 GEMINI LLM (Official LangChain Integration)
|
|
|
|
| 3 |
import torch
|
| 4 |
import time
|
| 5 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
import chromadb
|
| 9 |
+
from chromadb.utils import embedding_functions
|
| 10 |
from chromadb.config import Settings
|
| 11 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 12 |
from langchain.agents import initialize_agent, Tool, AgentType
|
|
|
|
| 90 |
# 📚 KNOWLEDGE BASE SETUP
|
| 91 |
# ==============================================================
|
| 92 |
# Persistent Chroma client (new API)
|
| 93 |
+
CHROMA_PATH = "/data/chroma"
|
|
|
|
| 94 |
COLLECTION_NAME = "knowledge_base"
|
| 95 |
|
| 96 |
try:
|
| 97 |
+
chroma_client = chromadb.PersistentClient(path=CHROMA_PATH)
|
| 98 |
kb_collection = chroma_client.get_or_create_collection(COLLECTION_NAME)
|
| 99 |
except Exception as e:
|
| 100 |
kb_collection = None
|
| 101 |
print("⚠️ Could not initialize KB:", e)
|
| 102 |
|
| 103 |
+
# Sentence Transformer Embedding Function
|
| 104 |
+
embedding_func = embedding_functions.SentenceTransformerEmbeddingFunction(
|
| 105 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
| 106 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
def cosine_similarity(vec1, vec2):
|
| 109 |
+
"""Compute cosine similarity between two vectors."""
|
| 110 |
+
if np.linalg.norm(vec1) == 0 or np.linalg.norm(vec2) == 0:
|
| 111 |
+
return 0.0
|
| 112 |
+
return np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))
|
| 113 |
+
|
| 114 |
+
def query_kb(text: str, top_k: int = 1):
|
| 115 |
+
"""
|
| 116 |
+
Query the knowledge base for relevant solutions using Chroma.
|
| 117 |
+
Returns: {"answer": str, "confidence": float, "metadata": dict}
|
| 118 |
+
"""
|
| 119 |
+
if not kb_collection:
|
| 120 |
+
return {"answer": "⚠️ Knowledge Base not initialized.", "confidence": 0.0, "metadata": {}}
|
| 121 |
+
|
| 122 |
+
try:
|
| 123 |
+
# Embed the query
|
| 124 |
+
query_vector = embedding_func([text])[0]
|
| 125 |
+
|
| 126 |
+
# Query Chroma
|
| 127 |
+
results = kb_collection.query(query_embeddings=[query_vector], n_results=top_k)
|
| 128 |
+
if not results or not results.get("documents") or len(results["documents"][0]) == 0:
|
| 129 |
+
return {"answer": "No relevant KB entry found.", "confidence": 0.0, "metadata": {}}
|
| 130 |
+
|
| 131 |
+
# Extract best match
|
| 132 |
+
best_doc = results["documents"][0][0]
|
| 133 |
+
best_meta = results["metadatas"][0][0]
|
| 134 |
+
best_vec = np.array(results["embeddings"][0][0]) if "embeddings" in results else query_vector
|
| 135 |
+
|
| 136 |
+
# Compute similarity confidence manually
|
| 137 |
+
confidence = float(cosine_similarity(query_vector, best_vec))
|
| 138 |
+
|
| 139 |
+
return {
|
| 140 |
+
"answer": best_doc,
|
| 141 |
+
"confidence": round(confidence, 3),
|
| 142 |
+
"metadata": best_meta
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
except Exception as e:
|
| 146 |
+
print(f"⚠️ KB query failed: {e}")
|
| 147 |
+
return {"answer": "Error accessing KB.", "confidence": 0.0, "metadata": {}}
|
| 148 |
|
| 149 |
# ==============================================================
|
| 150 |
# 🧠 GEMINI LLM (Official LangChain Integration)
|