-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
270 lines (231 loc) · 11.4 KB
/
Copy pathapp.py
File metadata and controls
270 lines (231 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import os
import time
import tempfile
import streamlit as st
from credentials import (
LLAMA_CLOUD_API_KEY, GROQ_API_KEY, HF_TOKEN,
REDIS_HOST, REDIS_PORT, REDIS_PASSWORD,
)
os.environ["LLAMA_CLOUD_API_KEY"] = LLAMA_CLOUD_API_KEY
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
os.environ["HF_TOKEN"] = HF_TOKEN
# ── Page config ──────────────────────────────────────────────────────────────
st.set_page_config(
page_title="Semantic Cache",
page_icon="⚡",
layout="wide",
)
# ── Cached resources (initialised once per session) ──────────────────────────
@st.cache_resource(show_spinner="Loading embedding model…")
def get_vectorizer():
from redisvl.utils.vectorize import HFTextVectorizer
return HFTextVectorizer(model="sentence-transformers/all-MiniLM-L6-v2")
@st.cache_resource(show_spinner="Connecting to Redis…")
def get_cache(distance_threshold: float = 0.2):
from redisvl.extensions.cache.llm import SemanticCache
redis_url = f"redis://:{REDIS_PASSWORD}@{REDIS_HOST}:{REDIS_PORT}"
return SemanticCache(
vectorizer=get_vectorizer(),
distance_threshold=distance_threshold,
redis_url=redis_url,
)
@st.cache_resource(show_spinner="Loading LLM…")
def get_llm():
from langchain_groq import ChatGroq
return ChatGroq(model="llama-3.1-8b-instant", temperature=0.5)
# ── Session state defaults ────────────────────────────────────────────────────
for key, default in [("hits", 0), ("misses", 0), ("history", [])]:
if key not in st.session_state:
st.session_state[key] = default
# ── Sidebar ───────────────────────────────────────────────────────────────────
with st.sidebar:
st.title("⚡ Semantic Cache")
st.caption("Redis · LangChain · HuggingFace")
st.caption("Author: Clement T. Okolo")
st.divider()
# Connection status
try:
import redis as _redis
r = _redis.Redis(host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWORD)
r.ping()
st.success("Redis connected", icon="🟢")
except Exception as e:
st.error(f"Redis error: {e}", icon="🔴")
st.divider()
# Distance threshold — clear cached SemanticCache if changed
threshold = st.slider(
"Similarity threshold",
min_value=0.05, max_value=0.50, value=0.20, step=0.05,
help="Maximum vector distance for a cache hit. Lower = stricter match.",
)
if st.session_state.get("threshold") != threshold:
get_cache.clear()
st.session_state["threshold"] = threshold
st.divider()
# Cache stats
total = st.session_state.hits + st.session_state.misses
hit_rate = (st.session_state.hits / total * 100) if total else 0
col1, col2 = st.columns(2)
col1.metric("Cache hits", st.session_state.hits)
col2.metric("Cache misses", st.session_state.misses)
st.progress(hit_rate / 100, text=f"Hit rate: {hit_rate:.0f}%")
if st.button("Reset stats"):
st.session_state.hits = 0
st.session_state.misses = 0
st.rerun()
st.divider()
# ── Populate cache from PDF ───────────────────────────────────────────────
with st.expander("📄 Populate cache from PDF", expanded=False):
st.caption(
"Upload a PDF to parse it with LlamaCloud, extract FAQs with the "
"Groq LLM, and load them into the cache. This may take a few minutes."
)
pdf_source = st.radio(
"PDF source",
["Use existing (data/ folder)", "Upload a new PDF"],
index=0,
)
uploaded_file = None
if pdf_source == "Upload a new PDF":
uploaded_file = st.file_uploader("Choose a PDF", type="pdf")
auto_store = st.checkbox("Store new LLM responses in cache", value=True)
if st.button("🚀 Populate cache", use_container_width=True):
_populate_clicked = True
else:
_populate_clicked = False
if _populate_clicked:
with st.spinner("Parsing PDF and generating FAQs…"):
try:
from llama_cloud import LlamaCloud
from llama_index.core import Document
from llama_index.core.node_parser import MarkdownNodeParser
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.prompts import PromptTemplate
from pydantic import BaseModel, Field
from typing import List
llama_client = LlamaCloud(api_key=LLAMA_CLOUD_API_KEY)
# Resolve PDF path
if pdf_source == "Upload a new PDF" and uploaded_file:
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
tmp.write(uploaded_file.read())
pdf_path = tmp.name
else:
pdf_path = "./data/2022-chevrolet-colorado-ebrochure.pdf"
# Upload & parse
file_obj = llama_client.files.create(file=pdf_path, purpose="parse")
result = llama_client.parsing.parse(
file_id=file_obj.id,
tier="agentic",
version="latest",
expand=["markdown"],
)
documents = [
Document(
text=page.markdown,
metadata={"page_label": str(i + 1)},
)
for i, page in enumerate(result.markdown.pages)
]
# Chunk into nodes
nodes = MarkdownNodeParser().get_nodes_from_documents(documents)
# Define FAQ schema
class PromptResponse(BaseModel):
prompt: str = Field(description="Question about the document.")
response: str = Field(description="Grounded answer from the document.")
class FAQs(BaseModel):
pairs: List[PromptResponse] = Field(description="List of FAQ pairs.")
json_parser = JsonOutputParser(pydantic_object=FAQs)
llm = get_llm()
prompt_template = PromptTemplate(
template=(
"Extract as many FAQ prompt/response pairs as possible from the "
"document context below. Focus on factual data.\n\n"
"{format_instructions}\n\nDocument Context:\n{doc}\n"
),
input_variables=["doc"],
partial_variables={"format_instructions": json_parser.get_format_instructions()},
)
chain = prompt_template | llm | json_parser
all_faqs = []
progress = st.progress(0, text="Extracting FAQs from nodes…")
for i, node in enumerate(nodes):
res = chain.invoke({"doc": node.text})
if res and res.get("pairs"):
all_faqs.extend(res["pairs"])
progress.progress((i + 1) / len(nodes), text=f"Node {i+1}/{len(nodes)}")
# Embed & store
vectorizer = get_vectorizer()
prompts = [p["prompt"] for p in all_faqs]
embeddings = vectorizer.embed_many(prompts)
cache = get_cache(threshold)
for i, entry in enumerate(all_faqs):
if "prompt" in entry and "response" in entry:
cache.store(
prompt=entry["prompt"],
response=entry["response"],
vector=embeddings[i],
)
st.success(f"✅ Loaded {len(all_faqs)} FAQs into the cache.")
except Exception as e:
st.error(f"Failed to populate cache: {e}")
# ── Main area ─────────────────────────────────────────────────────────────────
st.header("Ask a question")
st.caption(
"Semantically similar questions are answered from the Redis cache instantly. "
"Cache misses fall back to the Groq LLM."
)
with st.form("query_form", clear_on_submit=False):
question = st.text_input(
"Your question",
placeholder="e.g. What engine options does the Chevy Colorado offer?",
label_visibility="collapsed",
)
store_on_miss = st.checkbox("Store LLM response in cache on miss", value=True)
submitted = st.form_submit_button("Ask ⚡", use_container_width=True)
if submitted and question.strip():
cache = get_cache(threshold)
llm = get_llm()
start = time.perf_counter()
cached_results = cache.check(question)
elapsed = time.perf_counter() - start
if cached_results:
st.session_state.hits += 1
answer = cached_results[0].get("response", "")
score = cached_results[0].get("vector_distance", None)
st.success("⚡ Cache hit", icon="✅")
col1, col2 = st.columns([3, 1])
with col1:
st.markdown(f"**Answer:**\n\n{answer}")
with col2:
st.metric("Response time", f"{elapsed * 1000:.1f} ms")
if score is not None:
st.metric("Vector distance", f"{score:.3f}")
else:
st.session_state.misses += 1
with st.spinner("Cache miss — querying LLM…"):
llm_start = time.perf_counter()
llm_response = llm.invoke(question).content
llm_elapsed = time.perf_counter() - llm_start
st.warning("🔄 Cache miss — answered by LLM", icon="🤖")
col1, col2 = st.columns([3, 1])
with col1:
st.markdown(f"**Answer:**\n\n{llm_response}")
with col2:
st.metric("Response time", f"{llm_elapsed * 1000:.0f} ms")
if store_on_miss:
vectorizer = get_vectorizer()
embedding = vectorizer.embed(question)
cache.store(prompt=question, response=llm_response, vector=embedding)
st.caption("💾 Stored in cache for future queries.")
# Append to history
st.session_state.history.append({
"question": question,
"hit": bool(cached_results),
})
# ── Query history ─────────────────────────────────────────────────────────────
if st.session_state.history:
st.divider()
with st.expander("📋 Query history", expanded=False):
for item in reversed(st.session_state.history):
badge = "✅ hit" if item["hit"] else "🤖 miss"
st.markdown(f"- {badge} — {item['question']}")