iT邦幫忙

2025 iThome 鐵人賽

DAY 14
0
佛心分享-IT 人自學之術

學習 LLM系列 第 14

Day14 畫 RAG 流程圖

  • 分享至 

  • xImage
  •  

在 Colab 上製作流程圖

# Colab-friendly: 安裝 Graphviz
!apt-get install -y graphviz > /dev/null 2>&1
!pip install -q graphviz


from graphviz import Digraph
from IPython.display import Image, display
import os


dot = Digraph(name="RAG_Workflow", format="png")
dot.attr(rankdir='LR', fontsize='12')


# 定義節點
dot.node('U', 'User Query', shape='oval', style='filled', fillcolor='#FFEECC')
dot.node('QE', 'Query Encoder\n(query -> vector)', shape='box', style='rounded,filled', fillcolor='#E8F6FF')
dot.node('IDX', 'Vector DB / Index\n(FAISS / Chroma)', shape='cylinder', style='filled', fillcolor='#E8FFE8')
dot.node('RET', 'Retrieve top-k\n(similarity search)', shape='box', style='rounded,filled', fillcolor='#FFF2CC')
dot.node('RR', 'Reranker (opt)\n(cross-encoder)', shape='box', style='rounded', fillcolor='#FDE2E2')
dot.node('CB', 'Context Builder\n(assemble top-k as context)', shape='box', style='rounded,filled', fillcolor='#EDE7FF')
dot.node('PROMPT', 'Prompt Template\n+ User Query', shape='note', style='filled', fillcolor='#FFF0F5')
dot.node('LLM', 'LLM Generator\n(e.g. GPT / local model)', shape='box3d', style='filled', fillcolor='#FFEFD5')
dot.node('A', 'Answer (with sources)', shape='oval', style='filled', fillcolor='#CCFFFB')
dot.node('LOG', 'Logging / Feedback', shape='note', fillcolor='#F0F0F0')


# 連線
dot.edge('U', 'QE', label='encode')
dot.edge('QE', 'IDX', label='vector query')
dot.edge('IDX', 'RET', label='top-k candidates')
dot.edge('RET', 'RR', label='(optional)')
dot.edge('RR', 'CB', label='ranked candidates')
dot.edge('RET', 'CB', label='or directly top-k')
dot.edge('CB', 'PROMPT')
dot.edge('PROMPT', 'LLM', label='inference')
dot.edge('LLM', 'A')
dot.edge('A', 'LOG', style='dashed')


# 儲存並顯示
out_path = "rag_workflow"
dot.render(out_path, cleanup=True)  # 會產生 rag_workflow.png
display(Image(out_path + ".png"))

https://ithelp.ithome.com.tw/upload/images/20250928/20169173HN3JBbke9M.png

流程圖說明

  1. User Query:使用者輸入自然語言問題
  2. Query Encoder:把 query 編碼成向量(sentence-transformers 等)。
  3. Vector DB / Index:把文件的 embeddings 存在向量資料庫(FAISS / Chroma 等),支援快速近似搜尋(ANN)
  4. Retrieve top-k:用相似度(cosine / inner-product)找出 k 個最相關的段落或 FAQ
  5. Reranker(可選):用更精準的模型(cross-encoder)對 top-k 做重排,提高精準度
  6. Context Builder:把 top-n 的文件片段(或 FAQ answer)格式化、加上來源標註,組成 prompt 的 context
  7. Prompt Template + LLM:把 context 與 user query 插入 prompt 模板,交給 LLM 生成回答(可加約束:格式、字數、引用來源)
  8. Answer & Logging:回傳答案並記錄檢索來源、分數與使用者反饋,用於後續優化(閾值、增強資料)

上一篇
Day13 設計實作專案
下一篇
Day15 設計 10 條 FAQ
系列文
學習 LLM15
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言