i> 安裝 Gradio
!pip install -q gradio chromadb sentence-transformers
ii> 建立一個簡單的 Gradio 介面
import gradio as gr
def gradio_interface(query):
answer, retrieved = rag_answer(query, k=3)
refs_text = "\n\n".join([f"Q: {r['doc']}\nA: {r['answer']} (score={r['score']:.3f})" for r in retrieved])
return answer, refs_text
with gr.Blocks() as demo:
gr.Markdown("## 🧠 FAQ 檢索 + RAG Demo")
gr.Markdown("輸入你的問題,系統會根據 FAQ 找出最相關的答案")
with gr.Row():
inp = gr.Textbox(label="輸入你的問題", placeholder="例如:我要怎麼退貨?")
btn = gr.Button("查詢")
with gr.Row():
out_answer = gr.Textbox(label="系統回答", lines=3)
with gr.Accordion("檢索結果細節", open=False):
out_retrieved = gr.Textbox(label="檢索段落", lines=8)
btn.click(fn=gradio_interface, inputs=inp, outputs=[out_answer, out_retrieved])
demo.launch(share=True) # 在 Colab 用 share=True 開公開連結
會顯示 top-k 檢索到的問題 + 相似度分數