💡 本文同步發表於 Medium:原文連結
https://medium.com/@simon3458/chromadb-introduction-27ed114d8d1d
點擊查看連結詳情...
[ 向量資料庫 ] 一個好用的開源 Embedding 應用資料庫— ChromaDB
看完文章後歡迎按鼓勵,訂閱,並分享給所有想知道此類知識的所有人!
如內容有任何錯誤,歡迎以各種方式讓我知道,讓我能夠持續正向進步!

[ 向量資料庫 ] 一個好用的開源 Embedding 應用資料庫 — ChromaDB
生成式 AI 模型很強大,但是,因為模型非常大,導致更新週期沒有辦法即時的更新,所以知識的範圍受到限制,再來,部分的企業和個人的知識其實是機密的,因此在訓練過程中,這些廠商也無法取得相關知識,也因此, RAG 的技術就興起了,詳細的 RAG 文章請見此處:
https://blog.infuseai.io/rag-retrieval-augmented-generation-introduction-a5854cb6393e
點擊查看連結詳情...
那今天將這樣的知識轉換成機器讀得懂的 Embedding Vector 之後,必須要有一個儲存的地方讓我們在需要使用這些知識時,可以有方法所以各家的資料庫儲存廠商就開始推出自家的 Embedding Vector Database,那今天這篇文章,將會帶來開源的向量資料庫 ChromaDB(以下會簡稱 Chroma )。
Chroma 是什麼?
基本上,Chroma 如同前面所說的,開源資料庫、主要用於存取和查詢向量數據,讓查詢知識變得更容易。
Chroma 特點包含:
- 輕量級開源資料庫
- 容易取得和使用:透過 python 套件安裝,即可直接使用。
- 功能豐富且足夠應付專案:Embedding 儲存、取用、查詢、檢索等。
- 支援多種服務的模型:OpenAI, Gemini, Ollama 等皆可。
- 可以透過 LangChain 和 LlamaIndex 來進行使用。
怎麼使用 Chroma?
- 安裝 Pythton 套件:Chroma 使用上其實很簡單,你只要使用 pipe 就可以進行安裝即可。
pip install chromadb
- 連接 Chroma: 目前 Chroma 支援三種方式進行連線,看是要跟儲存在地端的 SQLite 資料庫, memory 或者 http 都可以。
- 地端 SQLite Database:
import chromadb
chroma_client = chromadb.PersistentClient(path=".")
- Memory:
import chromadb
chroma_client = chromadb.Client()
- http:
import chromadb
chroma_client = chromadb.HttpClient(host = 'localhost', port = 8000)
- 建立 Embedding function
在這邊,我們示範 Ollama 的 Embedding 功能,因此我們需要建立一個 Chroma 的 Embedding function。
import chromadb.utils.embedding_functions as embedding_functions
ollama_ef = embedding_functions.OllamaEmbeddingFunction(
url="http://localhost:11434/api/embeddings",
model_name="chatfire/bge-m3:q8_0",
)
- 建立 Collection (像是一般資料庫的 table):
collection = chroma_client.create_collection(
name="collection",
embedding_function = ollama_ef,
metadata={"hnsw:space": "cosine"} # l2 is the default
)
其中我們可以看到,在這邊設定跟 embedding_function 進行連接,並且設定你要使用哪一種評估方式來了解後續詢問的問題,是否接近知識點,我們這邊選擇 Cosine Distance 來作為計算。
常見的 Distance similarity 計算如下:
- Cosine — Useful for text similarity
- Euclidean (L2) — Useful for text similarity, more sensitive to noise than
cosine - Inner Product (IP) — Recommender systems
- 加入知識列表,成為 RAG Embedding 知識庫:
透過以下程式碼,將 documents 的文字知識,加入其中。
collection.add(
documents = documents,
ids=[f'id{i}' for i in range(1, len(documents)+1)]
)
- 找尋相似知識:
當今天想要詢問問題時,你只要在 query_texts 裡面放入相關問題或者字句,就可以很快速的 query 出最終的解答出來。
results = collection.query(
query_texts=["..."], # Chroma will embed this for you
n_results=5, # how many results to return
include=["documents", "distances"],
)
results
Chroma 可以應用在哪邊?
目前這樣的工具,其實可以搭配著其他的工具一起使用,來串成一個服務,以下是簡單的舉例:
- 透過 PyMuPDF + Embedding Model + Chroma ,你就可以實現 pdf 轉成 Embedding 的方式,並且針對文件進行問答工作。
- 透過 LangChain 的 MarkdownHeaderTextSplitter + Embedding Model + Chroma,你就可以實踐 Notion 或者相關 markdown 文件來進行知識檢索工作。
- 因為是可以將資料儲存在 SQLite 中,因此 Chroma 放到輕量化的 Edge 硬體端來使用,也是很方便的。
Chroma 相關文件
- 官方網站:
Chroma - open-source search infrastructure for AI
Open-source search infrastructure for AI

- 官方文件:
Introduction - Chroma Docs
Chroma is the open-source data infrastructure for AI. It comes with everything you need to get started built-in.
- LangChain — Chroma:
LangChain overview - Docs by LangChain
LangChain provides create_agent: a minimal, highly configurable agent harness. Compose exactly the agent your use case needs from model, tools, prompt, and middleware.
結論
總結來說,ChromaDB 作為一個開源的向量資料庫,提供了輕量級且功能豐富的解決方案,方便用戶存取和查詢向量數據。無論是支持多種服務模型、便於安裝和使用,還是能夠與各種工具和框架進行整合,Chroma 都展現了其強大的應用潛力。這些特點使其成為建立 RAG 知識庫的一個理想選擇。
I am Simon
大家好,我是 Simon 劉育維,目前是一位 AI 工程師,幫助企業導入人工智慧相關技術。如果這篇文章對您有幫助,請在 Medium 上按一下鼓勵並追蹤我的個人帳號,這樣您就可以隨時閱讀我所撰寫的文章。歡迎在我的 Linkedin 上留言提供意見,並與我一起討論有關人工智慧的主題,期待能夠對大家有所幫助!

My Personal Website: <https://simonliuyuwei-4ndgcf4.gamma.site/>
這篇文章有幫上忙嗎?