forked from IgorKolodziej/QuantumRAG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
95 lines (75 loc) · 3.88 KB
/
Copy pathapp.py
File metadata and controls
95 lines (75 loc) · 3.88 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
import streamlit as st
import json
import pandas as pd
from src.components.ContextRetriever import ContextRetriever
from src.components.GroverTopK import GroverTopK
from src.components.AgentHandler import AgentHandler
#from gpt_final_answer import generate_answer_from_contexts
grover_top_k = GroverTopK()
MODEL_NAME = 'mixedbread-ai/mxbai-embed-large-v1'
QUERY_PREFIX = "Represent this sentence for searching relevant passages: "
TOP_K_FIRST = 10
TOP_K_FINAL = 3
EMBEDDINGS_FILE = "saved_embeddings/squad_embeddings_mixedbread_ai_mxbai_embed_large_v1.npy"
DOCS_LIST_FILE = "saved_embeddings/squad_docs_mixedbread_ai_mxbai_embed_large_v1.json"
# Context retriever
retriever = ContextRetriever(
model_name=MODEL_NAME,
embeddings_file=EMBEDDINGS_FILE,
docs_list_file=DOCS_LIST_FILE
)
with open(DOCS_LIST_FILE, 'r', encoding='utf-8') as f:
docs = json.load(f)
retriever.build_index(docs)
# Streamlit UI
st.title("QA with Grover and LLMs")
st.markdown("Ask a question and get an answer using Grover and LLM.")
user_question = st.text_input("Your question:", placeholder="Type your question here...")
if user_question:
query = QUERY_PREFIX + user_question
with st.spinner("Searching for relevant contexts..."):
# Use the retriever to get the top 10 results
top_10_results = retriever.search(query, top_k=TOP_K_FIRST)
if not top_10_results:
st.error("No relevant contexts found.")
else:
with st.spinner("Searching for relevant contexts with Grover..."):
# Use Grover to get the top k contexts
# grover_output = grover_top_k(top_10_results, k=TOP_K_FINAL)
grover_output=grover_top_k.select(top_10_results, k=TOP_K_FINAL)
top_k_contexts = grover_output["contexts"]
with st.spinner("Generating answer..."):
# Generate the final answer from the top k contexts
agent_handler = AgentHandler()
agents_output = agent_handler.compare_all(user_question, top_k_contexts)
st.subheader("Answer:")
if agents_output is None:
st.error("No answer found.")
else:
st.success("Answer found!")
# st.write(final_answer)
# Prepare data for DataFrame
for model_key, outputs in agents_output.items():
with st.expander(f"Model: {model_key}", expanded=True):
col1, col2 = st.columns(2)
with col1:
st.markdown("**✅ With Context:**")
st.info(outputs["with_context"])
with col2:
st.markdown("**❓ Without Context:**")
st.warning(outputs["without_context"])
# Add a small comparison summary
match_percentage = len(set(outputs["with_context"].split()) & set(outputs["without_context"].split())) / max(len(set(outputs["with_context"].split())), len(set(outputs["without_context"].split()))) * 100
st.caption(f"Word overlap: ~{match_percentage:.1f}% similarity between responses")
st.markdown(f"**Treshold:** {grover_output['threshold']:.4f}")
with st.expander("Show top 3 contexts"):
for i, ctx in enumerate(top_k_contexts):
st.markdown(f"**Context {i+1} (score: {ctx['similarity_score']:.4f})**")
st.write(ctx["document"])
with st.expander("Show all top 10 contexts"):
for res in top_10_results:
st.markdown(f"**Rank {res['rank']} (score: {res['similarity_score']:.4f})**")
st.write(res["document"][:500] + "...")
st.markdown("### Grover info:")
st.markdown(f"**Qiskit version:** {grover_output['qiskit_version']}")
st.markdown(f"**Runtime version:** {grover_output['runtime_version']}")