Spaces:
Runtime error
Runtime error
Create retrieval.py
Browse files- retrieval.py +35 -0
retrieval.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer, util
|
| 2 |
+
import chromadb
|
| 3 |
+
from chromadb.utils import embedding_functions
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
class SimpleRetriever:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
self.encoder = SentenceTransformer('all-MiniLM-L6-v2')
|
| 9 |
+
self.client = chromadb.Client()
|
| 10 |
+
self.collection = self.client.create_collection(
|
| 11 |
+
name="incidents",
|
| 12 |
+
embedding_function=embedding_functions.SentenceTransformerEmbeddingFunction()
|
| 13 |
+
)
|
| 14 |
+
self._seed_incidents()
|
| 15 |
+
|
| 16 |
+
def _seed_incidents(self):
|
| 17 |
+
incidents = [
|
| 18 |
+
("High latency in payment service, caused by database connection pool exhaustion.", "database_pool"),
|
| 19 |
+
("Memory leak in API gateway after 24 hours of uptime.", "memory_leak"),
|
| 20 |
+
("Authentication service returning 500 errors due to misconfigured OAuth.", "oauth_config"),
|
| 21 |
+
("Disk full on logging node, causing log loss.", "disk_full"),
|
| 22 |
+
]
|
| 23 |
+
for text, cause in incidents:
|
| 24 |
+
self.collection.add(
|
| 25 |
+
documents=[text],
|
| 26 |
+
metadatas=[{"cause": cause}],
|
| 27 |
+
ids=[cause]
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
def get_similarity(self, query: str) -> float:
|
| 31 |
+
results = self.collection.query(query_texts=[query], n_results=1)
|
| 32 |
+
if results['distances'] and len(results['distances'][0]) > 0:
|
| 33 |
+
# Convert L2 distance to similarity (inverse, scaled)
|
| 34 |
+
return 1.0 / (1.0 + results['distances'][0][0])
|
| 35 |
+
return 0.0
|