在 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"))
流程圖說明