Arthur0126's picture
Create app.py
d556d3a verified
Raw History Blame Contribute Delete
14.8 kB
"""
Streamlit + Groq API - 8種 RAG 策略 PDF 問答系統
需要安装: pip install streamlit groq pypdf sentence-transformers numpy faiss-cpu scikit-learn
執行方式: streamlit run app.py
"""
import streamlit as st
from groq import Groq
import numpy as np
from sentence_transformers import SentenceTransformer
import faiss
from pypdf import PdfReader
import re
from sklearn.feature_extraction.text import TfidfVectorizer
# ==================== 核心 RAG 類別 (邏輯完全保留) ====================
class MultiStrategyRAG:
def __init__(self):
self.client = None # 初始不設定 API Key,等待使用者輸入
self.embedding_model = SentenceTransformer(
'sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2'
)
self.chunks = []
self.embeddings = None
self.index = None
self.tfidf_vectorizer = None
self.tfidf_matrix = None
def verify_and_set_api_key(self, api_key):
"""驗證並設定 API Key"""
if not api_key:
return False, "⚠️ 請輸入 API Key"
try:
temp_client = Groq(api_key=api_key)
temp_client.models.list()
self.client = temp_client
return True, "✅ 驗證成功!可以開始使用了。"
except Exception as e:
self.client = None
error_msg = str(e)
if "invalid_api_key" in error_msg.lower():
return False, "❌ 驗證失敗:API Key 無效或格式錯誤。"
return False, f"❌ 驗證失敗: {error_msg}"
def load_pdf(self, pdf_file):
"""載入 PDF 檔案"""
try:
reader = PdfReader(pdf_file)
full_text = ""
for page in reader.pages:
text = page.extract_text()
full_text += text + "\n"
# 分割文本
self.chunks = self._split_text(full_text, chunk_size=800, overlap=150)
# 生成嵌入向量
self.embeddings = self.embedding_model.encode(
self.chunks,
convert_to_numpy=True
)
# 建立 FAISS 索引
dimension = self.embeddings.shape[1]
self.index = faiss.IndexFlatL2(dimension)
self.index.add(self.embeddings.astype('float32'))
# 建立 TF-IDF 索引
self.tfidf_vectorizer = TfidfVectorizer(max_features=1000)
self.tfidf_matrix = self.tfidf_vectorizer.fit_transform(self.chunks)
return True, f"✅ 成功載入 PDF!共 {len(reader.pages)} 頁,分割為 {len(self.chunks)} 個片段"
except Exception as e:
return False, f"❌ 載入失敗: {str(e)}"
def _split_text(self, text, chunk_size, overlap):
"""分割文本"""
chunks = []
start = 0
text_length = len(text)
while start < text_length:
end = start + chunk_size
chunk = text[start:end]
chunk = re.sub(r'\s+', ' ', chunk).strip()
if chunk:
chunks.append(chunk)
start += chunk_size - overlap
return chunks
# ==================== 8種 RAG 策略 ====================
def strategy_1_basic_similarity(self, query, top_k=3):
query_vector = self.embedding_model.encode([query])
distances, indices = self.index.search(query_vector.astype('float32'), top_k)
return [self.chunks[idx] for idx in indices[0]]
def strategy_2_tfidf(self, query, top_k=3):
query_vector = self.tfidf_vectorizer.transform([query])
similarities = (self.tfidf_matrix * query_vector.T).toarray().flatten()
top_indices = similarities.argsort()[-top_k:][::-1]
return [self.chunks[idx] for idx in top_indices]
def strategy_3_hybrid(self, query, top_k=3):
query_vector = self.embedding_model.encode([query])
distances, sem_indices = self.index.search(query_vector.astype('float32'), top_k * 2)
query_tfidf = self.tfidf_vectorizer.transform([query])
tfidf_scores = (self.tfidf_matrix * query_tfidf.T).toarray().flatten()
tfidf_indices = tfidf_scores.argsort()[-top_k * 2:][::-1]
combined = list(set(sem_indices[0].tolist() + tfidf_indices.tolist()))
return [self.chunks[idx] for idx in combined[:top_k]]
def strategy_4_reranking(self, query, top_k=3):
candidates = self.strategy_1_basic_similarity(query, top_k=top_k * 2)
reranked = []
for chunk in candidates:
prompt = f"問題:{query}\n\n文本:{chunk[:200]}...\n\n這段文本與問題的相關度(0-10):"
try:
response = self.client.chat.completions.create(
model="llama-3.1-8b-instant",
messages=[{"role": "user", "content": prompt}],
max_tokens=10, temperature=0
)
score = response.choices[0].message.content.strip()
score = float(re.findall(r'\d+', score)[0]) if re.findall(r'\d+', score) else 0
reranked.append((chunk, score))
except:
reranked.append((chunk, 0))
reranked.sort(key=lambda x: x[1], reverse=True)
return [chunk for chunk, score in reranked[:top_k]]
def strategy_5_multi_query(self, query, top_k=3):
expansion_prompt = f"將以下問題改寫成3個相關但不同角度的問題,用換行分隔:\n{query}"
try:
response = self.client.chat.completions.create(
model="llama-3.1-8b-instant",
messages=[{"role": "user", "content": expansion_prompt}],
max_tokens=200, temperature=0.7
)
queries = [query] + response.choices[0].message.content.strip().split('\n')[:3]
except:
queries = [query]
all_chunks = []
for q in queries:
all_chunks.extend(self.strategy_1_basic_similarity(q, top_k=2))
return list(dict.fromkeys(all_chunks))[:top_k]
def strategy_6_contextual_compression(self, query, top_k=3):
chunks = self.strategy_1_basic_similarity(query, top_k=top_k)
compressed = []
for chunk in chunks:
compress_prompt = f"從以下文本中提取與問題「{query}」最相關的1-2句話:\n\n{chunk}"
try:
response = self.client.chat.completions.create(
model="llama-3.1-8b-instant",
messages=[{"role": "user", "content": compress_prompt}],
max_tokens=150, temperature=0
)
compressed.append(response.choices[0].message.content.strip())
except:
compressed.append(chunk[:300])
return compressed
def strategy_7_parent_child(self, query, top_k=3):
small_chunks = self._split_text(' '.join(self.chunks), chunk_size=300, overlap=50)
small_embeddings = self.embedding_model.encode(small_chunks, convert_to_numpy=True)
small_index = faiss.IndexFlatL2(small_embeddings.shape[1])
small_index.add(small_embeddings.astype('float32'))
query_vector = self.embedding_model.encode([query])
distances, indices = small_index.search(query_vector.astype('float32'), top_k)
results = []
for idx in indices[0]:
for big_chunk in self.chunks:
if small_chunks[idx] in big_chunk:
results.append(big_chunk)
break
return list(dict.fromkeys(results))[:top_k]
def strategy_8_hypothetical_answer(self, query, top_k=3):
hyde_prompt = f"請對以下問題給出一個假設性的答案(即使不確定):\n{query}"
try:
response = self.client.chat.completions.create(
model="llama-3.1-8b-instant",
messages=[{"role": "user", "content": hyde_prompt}],
max_tokens=200, temperature=0.7
)
hypothetical_answer = response.choices[0].message.content
except:
hypothetical_answer = query
query_vector = self.embedding_model.encode([hypothetical_answer])
distances, indices = self.index.search(query_vector.astype('float32'), top_k)
return [self.chunks[idx] for idx in indices[0]]
def generate_answer(self, query, strategy, top_k=3):
if not self.client:
return False, "❌ 請先在左側輸入並驗證 Groq API Key!", ""
if not self.chunks:
return False, "❌ 請先上傳 PDF 檔案!", ""
strategies = {
"1. 基礎語意搜尋": self.strategy_1_basic_similarity,
"2. TF-IDF 關鍵詞": self.strategy_2_tfidf,
"3. 混合搜尋": self.strategy_3_hybrid,
"4. 重新排序": self.strategy_4_reranking,
"5. 多查詢擴展": self.strategy_5_multi_query,
"6. 上下文壓縮": self.strategy_6_contextual_compression,
"7. 父子文檔": self.strategy_7_parent_child,
"8. 假設性答案 (HyDE)": self.strategy_8_hypothetical_answer,
}
retrieval_func = strategies.get(strategy, self.strategy_1_basic_similarity)
relevant_chunks = retrieval_func(query, top_k)
context = "\n\n---\n\n".join(relevant_chunks)
prompt = f"""請根據以下上下文回答問題。如果上下文中沒有相關資訊,請說明無法回答。
上下文:
{context}
問題:{query}
請用繁體中文詳細回答:"""
try:
response = self.client.chat.completions.create(
model="llama-3.1-8b-instant",
messages=[
{"role": "system", "content": "你是專業的文件分析助手。"},
{"role": "user", "content": prompt}
],
max_tokens=1024,
temperature=0.3
)
answer = response.choices[0].message.content
source_info = f"📚 使用策略:{strategy}\n📄 檢索片段數:{len(relevant_chunks)}\n\n" + \
"=" * 50 + "\n相關文本片段:\n" + "=" * 50 + "\n\n" + context
return True, answer, source_info
except Exception as e:
return False, f"❌ 生成答案失敗: {str(e)}", ""
# ==================== Streamlit 介面 ====================
def main():
st.set_page_config(page_title="多策略 RAG PDF 問答系統", page_icon="🤖", layout="wide")
# 1. 初始化 Session State (讓系統記住狀態)
if "rag" not in st.session_state:
st.session_state.rag = MultiStrategyRAG()
if "api_verified" not in st.session_state:
st.session_state.api_verified = False
st.title("🤖 多策略 RAG PDF 問答系統")
st.markdown("採用 **8 種不同的 RAG 策略**,為您的 PDF 文件提供智能問答服務!")
st.markdown("---")
# 建立左右兩欄佈局
col1, col2 = st.columns([1, 2])
with col1:
st.header("🔑 步驟 0: 設定 API Key")
api_key = st.text_input("輸入您的 Groq API Key", type="password", placeholder="gsk_...")
if st.button("✅ 驗證 API Key", use_container_width=True):
with st.spinner("驗證中..."):
success, msg = st.session_state.rag.verify_and_set_api_key(api_key)
if success:
st.session_state.api_verified = True
st.success(msg)
else:
st.session_state.api_verified = False
st.error(msg)
st.header("📤 步驟 1: 上傳 PDF")
uploaded_file = st.file_uploader("選擇 PDF 檔案", type=["pdf"])
if uploaded_file is not None:
if st.button("🚀 載入文件", use_container_width=True):
with st.spinner("正在解析與向量化文件,請稍候..."):
# 提示:Streamlit 的 uploaded_file 本身就是一個類似檔案的物件,可以直接給 PyPDF 讀取
success, msg = st.session_state.rag.load_pdf(uploaded_file)
if success:
st.success(msg)
else:
st.error(msg)
st.header("⚙️ 步驟 2: 選擇 RAG 策略")
strategy_options = [
"1. 基礎語意搜尋", "2. TF-IDF 關鍵詞", "3. 混合搜尋", "4. 重新排序",
"5. 多查詢擴展", "6. 上下文壓縮", "7. 父子文檔", "8. 假設性答案 (HyDE)"
]
selected_strategy = st.selectbox("RAG 策略", strategy_options)
top_k = st.slider("檢索片段數量 (Top-K)", min_value=1, max_value=10, value=3, step=1)
with st.expander("📖 策略說明"):
st.markdown("""
1. **基礎語意搜尋**: 使用向量相似度
2. **TF-IDF 關鍵詞**: 基於詞頻統計
3. **混合搜尋**: 結合語意與關鍵詞
4. **重新排序**: LLM 重新評分
5. **多查詢擴展**: 生成多個相關問題
6. **上下文壓縮**: 提取最相關部分
7. **父子文檔**: 小片段檢索大上下文
8. **假設性答案**: 先生成答案再搜尋
""")
with col2:
st.header("💬 步驟 3: 提問")
user_query = st.text_area("輸入您的問題", placeholder="例如:這份文件的主要內容是什麼?", height=100)
# 範例問題快速輸入機制
st.markdown("**💡 範例問題** (可以直接複製):\n`這份文件的主要內容是什麼?` | `文件中提到哪些重要概念?` | `文件的結論是什麼?`")
if st.button("🔍 提問", type="primary", use_container_width=True):
if not st.session_state.api_verified:
st.warning("⚠️ 請先在左側完成 API Key 驗證!")
elif not user_query.strip():
st.warning("⚠️ 請輸入問題!")
else:
with st.spinner("AI 正在思考中... (某些策略可能需要較多時間)"):
success, answer, source_info = st.session_state.rag.generate_answer(
user_query, selected_strategy, top_k
)
if success:
st.subheader("💡 AI 回答")
st.info(answer)
with st.expander("📚 查看檢索到的文本片段"):
st.text(source_info)
else:
st.error(answer) # 失敗時 answer 會包含錯誤訊息
if __name__ == "__main__":
main()