Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -40,7 +40,8 @@ logging.basicConfig(
|
|
| 40 |
)
|
| 41 |
logger = logging.getLogger(__name__)
|
| 42 |
|
| 43 |
-
# Apply nest_asyncio for environments like notebooks
|
|
|
|
| 44 |
|
| 45 |
# Reproducibility
|
| 46 |
SEED = 42
|
|
@@ -96,11 +97,12 @@ vector_store = QdrantVectorStore(
|
|
| 96 |
prefer_grpc=False
|
| 97 |
)
|
| 98 |
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 99 |
-
|
|
|
|
| 100 |
logger.info("β
Loaded existing VectorStoreIndex from Qdrant")
|
| 101 |
|
| 102 |
# --- CELL 5: Define Hybrid Retriever & Reranker ---
|
| 103 |
-
Settings.llm = None #
|
| 104 |
|
| 105 |
class HybridRetriever(BaseRetriever):
|
| 106 |
def __init__(self, dense, bm25):
|
|
@@ -210,7 +212,7 @@ def build_prompt(query, context, history):
|
|
| 210 |
"<|begin_of_text|>"
|
| 211 |
f"{HDR['sys']}\n{SYSTEM_PROMPT}{HDR['eot']}"
|
| 212 |
f"{hist_str}"
|
| 213 |
-
f"{HDR['usr']}\nContext:\n{context_str}\n\nQuestion: {query}{HDR['eot']}"
|
| 214 |
f"{HDR['ast']}\n"
|
| 215 |
)
|
| 216 |
return prompt, "rag"
|
|
@@ -259,6 +261,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="π¬ Level 0 IT Support Chatbot")
|
|
| 259 |
top_p_slider = gr.Slider(0.0, 1.0, value=0.9, step=0.01, label="Top-p")
|
| 260 |
with gr.Accordion("Show Retrieved Context", open=False):
|
| 261 |
context_display = gr.Textbox(label="Retrieved Context", interactive=False, lines=10)
|
|
|
|
| 262 |
def respond(message, history, k, temp, top_p):
|
| 263 |
global chat_history
|
| 264 |
dense_retriever.similarity_top_k = k
|
|
@@ -266,14 +269,16 @@ with gr.Blocks(theme=gr.themes.Soft(), title="π¬ Level 0 IT Support Chatbot")
|
|
| 266 |
reply, context_nodes = chat(message, temperature=temp, top_p=top_p)
|
| 267 |
ctx_text = "\n\n---\n\n".join([
|
| 268 |
f"**Source {i+1} (Score: {node.score:.4f})**\n{node.text}"
|
| 269 |
-
for i,node in enumerate(context_nodes)
|
| 270 |
])
|
| 271 |
history.append([message, reply])
|
| 272 |
return "", history, ctx_text
|
|
|
|
| 273 |
def clear_chat():
|
| 274 |
global chat_history
|
| 275 |
chat_history = []
|
| 276 |
return [], None
|
|
|
|
| 277 |
inp.submit(respond, [inp, chatbot, k_slider, temp_slider, top_p_slider], [inp, chatbot, context_display])
|
| 278 |
send_btn.click(respond, [inp, chatbot, k_slider, temp_slider, top_p_slider], [inp, chatbot, context_display])
|
| 279 |
clear_btn.click(clear_chat, None, [chatbot, context_display], queue=False)
|
|
|
|
| 40 |
)
|
| 41 |
logger = logging.getLogger(__name__)
|
| 42 |
|
| 43 |
+
# Apply nest_asyncio for environments like notebooks
|
| 44 |
+
nest_asyncio.apply()
|
| 45 |
|
| 46 |
# Reproducibility
|
| 47 |
SEED = 42
|
|
|
|
| 97 |
prefer_grpc=False
|
| 98 |
)
|
| 99 |
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 100 |
+
# Use from_storage_context to load existing index
|
| 101 |
+
index = VectorStoreIndex.from_storage_context(storage_context)
|
| 102 |
logger.info("β
Loaded existing VectorStoreIndex from Qdrant")
|
| 103 |
|
| 104 |
# --- CELL 5: Define Hybrid Retriever & Reranker ---
|
| 105 |
+
Settings.llm = None # Use our own LLM pipeline
|
| 106 |
|
| 107 |
class HybridRetriever(BaseRetriever):
|
| 108 |
def __init__(self, dense, bm25):
|
|
|
|
| 212 |
"<|begin_of_text|>"
|
| 213 |
f"{HDR['sys']}\n{SYSTEM_PROMPT}{HDR['eot']}"
|
| 214 |
f"{hist_str}"
|
| 215 |
+
f"{HDR['usr']}\nContext:\n{context_str}\n\nQuestion: {query}{ HDR['eot']}"
|
| 216 |
f"{HDR['ast']}\n"
|
| 217 |
)
|
| 218 |
return prompt, "rag"
|
|
|
|
| 261 |
top_p_slider = gr.Slider(0.0, 1.0, value=0.9, step=0.01, label="Top-p")
|
| 262 |
with gr.Accordion("Show Retrieved Context", open=False):
|
| 263 |
context_display = gr.Textbox(label="Retrieved Context", interactive=False, lines=10)
|
| 264 |
+
|
| 265 |
def respond(message, history, k, temp, top_p):
|
| 266 |
global chat_history
|
| 267 |
dense_retriever.similarity_top_k = k
|
|
|
|
| 269 |
reply, context_nodes = chat(message, temperature=temp, top_p=top_p)
|
| 270 |
ctx_text = "\n\n---\n\n".join([
|
| 271 |
f"**Source {i+1} (Score: {node.score:.4f})**\n{node.text}"
|
| 272 |
+
for i, node in enumerate(context_nodes)
|
| 273 |
])
|
| 274 |
history.append([message, reply])
|
| 275 |
return "", history, ctx_text
|
| 276 |
+
|
| 277 |
def clear_chat():
|
| 278 |
global chat_history
|
| 279 |
chat_history = []
|
| 280 |
return [], None
|
| 281 |
+
|
| 282 |
inp.submit(respond, [inp, chatbot, k_slider, temp_slider, top_p_slider], [inp, chatbot, context_display])
|
| 283 |
send_btn.click(respond, [inp, chatbot, k_slider, temp_slider, top_p_slider], [inp, chatbot, context_display])
|
| 284 |
clear_btn.click(clear_chat, None, [chatbot, context_display], queue=False)
|