Spaces:
Sleeping
Sleeping
added embeddings
Browse files
app.py
CHANGED
|
@@ -1,7 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import redis
|
|
|
|
|
|
|
| 4 |
from openai import AzureOpenAI
|
|
|
|
| 5 |
|
| 6 |
# Redis Cloud connection
|
| 7 |
redis_client = redis.Redis(
|
|
@@ -9,42 +12,76 @@ redis_client = redis.Redis(
|
|
| 9 |
port=12628,
|
| 10 |
decode_responses=True,
|
| 11 |
username="default",
|
| 12 |
-
password=os.getenv("REDIS_PASSWORD")
|
| 13 |
)
|
| 14 |
|
| 15 |
-
# Azure OpenAI client
|
| 16 |
client = AzureOpenAI(
|
| 17 |
api_key=os.getenv("AZURE_OPENAI_API_KEY").strip(),
|
| 18 |
-
api_version="2025-01-01-preview",
|
| 19 |
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT").strip()
|
| 20 |
)
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def chat_with_ai(user_input):
|
| 24 |
if not user_input:
|
| 25 |
return "Please type something."
|
| 26 |
|
| 27 |
-
# Check Redis
|
| 28 |
-
cached =
|
| 29 |
if cached:
|
| 30 |
return f"[From Redis] {cached}"
|
| 31 |
|
| 32 |
# Otherwise query Azure OpenAI
|
| 33 |
response = client.chat.completions.create(
|
| 34 |
-
model=
|
| 35 |
messages=[{"role": "user", "content": user_input}],
|
| 36 |
max_tokens=150
|
| 37 |
)
|
| 38 |
output = response.choices[0].message.content.strip()
|
| 39 |
|
| 40 |
-
# Save in Redis
|
| 41 |
-
|
| 42 |
|
| 43 |
return f"[From OpenAI] {output}"
|
| 44 |
|
| 45 |
# Gradio UI
|
| 46 |
with gr.Blocks(title="Azure OpenAI + Redis Cloud Chat") as demo:
|
| 47 |
-
gr.Markdown("# 💬 Azure OpenAI + Redis Cloud Demo")
|
| 48 |
with gr.Row():
|
| 49 |
chatbot = gr.Chatbot(type="messages")
|
| 50 |
with gr.Row():
|
|
@@ -61,4 +98,4 @@ with gr.Blocks(title="Azure OpenAI + Redis Cloud Chat") as demo:
|
|
| 61 |
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, debug=True,pwa=True)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import redis
|
| 4 |
+
import numpy as np
|
| 5 |
+
import json
|
| 6 |
from openai import AzureOpenAI
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
| 8 |
|
| 9 |
# Redis Cloud connection
|
| 10 |
redis_client = redis.Redis(
|
|
|
|
| 12 |
port=12628,
|
| 13 |
decode_responses=True,
|
| 14 |
username="default",
|
| 15 |
+
password=os.getenv("REDIS_PASSWORD")
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# Azure OpenAI client (only for chat, not embeddings anymore)
|
| 19 |
client = AzureOpenAI(
|
| 20 |
api_key=os.getenv("AZURE_OPENAI_API_KEY").strip(),
|
| 21 |
+
api_version="2025-01-01-preview",
|
| 22 |
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT").strip()
|
| 23 |
)
|
| 24 |
+
|
| 25 |
+
CHAT_DEPLOYMENT = "gpt-4.1" # your Azure chat deployment
|
| 26 |
+
|
| 27 |
+
# Load a small local HF embedding model
|
| 28 |
+
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 29 |
+
|
| 30 |
+
# Helper: get embedding from HF
|
| 31 |
+
def get_embedding(text):
|
| 32 |
+
return embedder.encode(text, convert_to_numpy=True).astype(np.float32)
|
| 33 |
+
|
| 34 |
+
# Helper: cosine similarity
|
| 35 |
+
def cosine_similarity(vec1, vec2):
|
| 36 |
+
return float(np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2)))
|
| 37 |
+
|
| 38 |
+
def search_cache(user_input, threshold=0.8):
|
| 39 |
+
query_vec = get_embedding(user_input)
|
| 40 |
+
best_key, best_score, best_val = None, -1, None
|
| 41 |
+
|
| 42 |
+
for key, val in redis_client.hgetall("cache").items():
|
| 43 |
+
entry = json.loads(val)
|
| 44 |
+
vec = np.array(entry["embedding"], dtype=np.float32)
|
| 45 |
+
score = cosine_similarity(query_vec, vec)
|
| 46 |
+
if score > best_score:
|
| 47 |
+
best_score, best_key, best_val = score, key, entry["output"]
|
| 48 |
+
|
| 49 |
+
if best_score >= threshold:
|
| 50 |
+
return best_val
|
| 51 |
+
return None
|
| 52 |
+
|
| 53 |
+
def store_cache(user_input, output):
|
| 54 |
+
vec = get_embedding(user_input).tolist()
|
| 55 |
+
redis_client.hset("cache", user_input, json.dumps({
|
| 56 |
+
"embedding": vec,
|
| 57 |
+
"output": output
|
| 58 |
+
}))
|
| 59 |
|
| 60 |
def chat_with_ai(user_input):
|
| 61 |
if not user_input:
|
| 62 |
return "Please type something."
|
| 63 |
|
| 64 |
+
# 🔍 Check Redis semantic cache
|
| 65 |
+
cached = search_cache(user_input)
|
| 66 |
if cached:
|
| 67 |
return f"[From Redis] {cached}"
|
| 68 |
|
| 69 |
# Otherwise query Azure OpenAI
|
| 70 |
response = client.chat.completions.create(
|
| 71 |
+
model=CHAT_DEPLOYMENT,
|
| 72 |
messages=[{"role": "user", "content": user_input}],
|
| 73 |
max_tokens=150
|
| 74 |
)
|
| 75 |
output = response.choices[0].message.content.strip()
|
| 76 |
|
| 77 |
+
# 💾 Save with embedding in Redis
|
| 78 |
+
store_cache(user_input, output)
|
| 79 |
|
| 80 |
return f"[From OpenAI] {output}"
|
| 81 |
|
| 82 |
# Gradio UI
|
| 83 |
with gr.Blocks(title="Azure OpenAI + Redis Cloud Chat") as demo:
|
| 84 |
+
gr.Markdown("# 💬 Azure OpenAI + Redis Cloud (Semantic Cache) Demo")
|
| 85 |
with gr.Row():
|
| 86 |
chatbot = gr.Chatbot(type="messages")
|
| 87 |
with gr.Row():
|
|
|
|
| 98 |
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
| 101 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, debug=True, pwa=True)
|