60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
import sys
|
|
import numpy as np
|
|
from sentence_transformers import SentenceTransformer
|
|
|
|
def main():
|
|
print("=" * 60)
|
|
print("LOCAL SEMANTIC CACHE SIMULATOR")
|
|
print("=" * 60)
|
|
|
|
# Initialize lightweight local embedding model
|
|
print("Loading local embedding model (all-MiniLM-L6-v2)...")
|
|
try:
|
|
model = SentenceTransformer("all-MiniLM-L6-v2")
|
|
except Exception as e:
|
|
print(f"Error loading model: {e}")
|
|
sys.exit(1)
|
|
|
|
print("\nModel loaded successfully.")
|
|
print("-" * 60)
|
|
|
|
# Enforce strict threshold to avoid false-positives
|
|
THRESHOLD = 0.92
|
|
|
|
# User input
|
|
sentence_1 = input("Enter Sentence 1 (The Baseline Cached Query):\n> ").strip()
|
|
sentence_2 = input("\nEnter Sentence 2 (The New Incoming Query):\n> ").strip()
|
|
|
|
if not sentence_1 or not sentence_2:
|
|
print("\n[Error] Both sentences must contain text.")
|
|
sys.exit(1)
|
|
|
|
print("\nGenerating local embeddings and performing vector math...")
|
|
|
|
# Generate embeddings (Returns 1D NumPy array for each string)
|
|
emb1 = model.encode(sentence_1)
|
|
emb2 = model.encode(sentence_2)
|
|
|
|
# Calculate Cosine Similarity w NumPy vector operations
|
|
dot_product = np.dot(emb1, emb2)
|
|
norm_1 = np.linalg.norm(emb1)
|
|
norm_2 = np.linalg.norm(emb2)
|
|
|
|
similarity_score = dot_product / (norm_1 * norm_2)
|
|
|
|
print("-" * 60)
|
|
print(f"RESULTS:")
|
|
print(f"-> Calculated Cosine Similarity: {similarity_score:.4f}")
|
|
print(f"-> Target Safety Threshold: {THRESHOLD:.4f}")
|
|
|
|
# Evaluate Cache Efficacy
|
|
if similarity_score >= THRESHOLD:
|
|
print("\n[CACHE HIT] Returning cached response.")
|
|
else:
|
|
print("\n[CACHE MISS] Score below threshold. Request routed to LLM.")
|
|
print("=" * 60)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
# To do: see README.md |