隨州公司做網(wǎng)站網(wǎng)址大全
在人工智能大模型領(lǐng)域, 離不開NLP技術(shù),在NLP中詞向量
是一種基本元素,如何存儲(chǔ)這些元素呢? 可以使用向量數(shù)據(jù)庫ChromeDB
Chroma
Chroma 是 AI 原生開源矢量數(shù)據(jù)庫。Chroma 通過為 LLM 提供知識(shí)、事實(shí)和技能,使構(gòu)建 LLM 應(yīng)用程序變得容易。同時(shí)也是實(shí)現(xiàn)大模型RAG技術(shù)方案的一種有效工具。
簡介
-
Chrome提供以下能力:
- 存儲(chǔ)嵌入類型數(shù)據(jù)(embeddings)和其元數(shù)據(jù)
- 嵌入(embed)文檔和查詢
- 對(duì)嵌入類型的檢索
-
Chrome 的原則:
- 對(duì)用戶的簡單性,并保障開發(fā)效率
- 同時(shí)擁有較好的性能
-
Chroma 作為服務(wù)器運(yùn)行,同時(shí)提供客戶端的SDK(支持Java, Go,Python, Rust等多種語言)。
安裝與運(yùn)行
- 首先要確保有安裝有
Python
運(yùn)行環(huán)境 - 安裝
Chroma
模塊pip install chromadb
- 創(chuàng)建數(shù)據(jù)庫存儲(chǔ)目錄
mkdir db_data
- 運(yùn)行
Chroma
服務(wù)并指定路徑chroma run --path db_data
如圖所示,Chroma
服務(wù)就成功啟動(dòng)啦!😄
將Chroma
作為服務(wù)常態(tài)化運(yùn)行
將chromadb.service
配置文件放在/etc/systemd/system/
目錄并用命令systemctl start chromadb
啟動(dòng)服務(wù)即可。
附贈(zèng)一份配置模板,具體參數(shù)按實(shí)際情況配置即可。
[Unit]
Description=ChromaDB Service
After=network-online.target[Service]
ExecStart=/root/anachonda3/bin/chroma run --path /chromadb/db_data
User=root
Group=root
Restart=always
RestartSec=3
export CHROMA_SERVER_HOST=127.0.0.1
Environment=CHROMA_SERVER_HTTP_PORT=8881
ANONYMIZED_TELEMETRY=False
[Install]
WantedBy=default.target
Python客戶端使用指南
- 導(dǎo)入模塊并創(chuàng)建數(shù)據(jù)庫連接
import chromadb chroma_client = chromadb.Client() # chroma_client = chromadb.HttpClient(host='localhost', port=8000)
- 創(chuàng)建數(shù)據(jù)庫
集合
(collection)
因?yàn)?code>Chroma 在 url 中使用集合名稱,因此命名有一些限制:collection = chroma_client.create_collection(name="my_collection") #chroma_client = chromadb.PersistentClient(path="/path/to/save/to") # 設(shè)置持久化路徑
- 名稱的長度必須介于 3 到 63 個(gè)字符之間。
- 名稱必須以小寫字母或數(shù)字開頭和結(jié)尾,并且中間可以包含點(diǎn)、破折號(hào)和下劃線。
- 名稱不得包含兩個(gè)連續(xù)的點(diǎn)。
- 名稱不得是有效的 IP 地址。
- 集合的一些便捷方法
# 返回集合中前10項(xiàng)的一個(gè)列表
collection.peek()
# 返回集合中的項(xiàng)目個(gè)數(shù)
collection.count()
# 重命名集合
collection.modify(name="new_name")
- 添加
文檔
(documents)到集合
(collection)中collection.add( embeddings=[[1.2, 2.3, 4.5], [6.7, 8.2, 9.2]], documents=["This is a document", "This is another document"], metadatas=[{"source": "my_source"}, {"source": "my_source"}], ids=["id1", "id2"] )
- 查詢文檔 n 個(gè)最相近的結(jié)果
results = collection.query( query_texts=["This is a query document"], n_results=2 )
- 便捷方法
chroma_client.heartbeat() # 納秒級(jí)心跳,確保與服務(wù)端連接狀態(tài) chroma_client.reset() # 重置數(shù)據(jù)庫,清除已有信息
查詢集合
使用.query
方法查詢集合
collection.query(query_embeddings=[[11.1, 12.1, 13.1],[1.1, 2.3, 3.2], ...],n_results=10,where={"metadata_field": "is_equal_to_this"},where_document={"$contains":"search_string"}
)
更新集合數(shù)據(jù)
使用.update
方法更新集合
collection.update(ids=["id1", "id2", "id3", ...],embeddings=[[1.1, 2.3, 3.2], [4.5, 6.9, 4.4], [1.1, 2.3, 3.2], ...],metadatas=[{"chapter": "3", "verse": "16"}, {"chapter": "3", "verse": "5"}, {"chapter": "29", "verse": "11"}, ...],documents=["doc1", "doc2", "doc3", ...],
)
使用upsert
更新數(shù)據(jù),若不存在則新增。
collection.upsert(ids=["id1", "id2", "id3", ...],embeddings=[[1.1, 2.3, 3.2], [4.5, 6.9, 4.4], [1.1, 2.3, 3.2], ...],metadatas=[{"chapter": "3", "verse": "16"}, {"chapter": "3", "verse": "5"}, {"chapter": "29", "verse": "11"}, ...],documents=["doc1", "doc2", "doc3", ...],
)
從集合中刪除數(shù)據(jù)
使用delete
方法刪除數(shù)據(jù)
collection.delete(ids=["id1", "id2", "id3",...],where={"chapter": "20"}
)
總結(jié)
通過這次學(xué)習(xí),了解到了使用ChromeDB
的基本方法,真是太好啦。
歡迎關(guān)注 公-眾-號(hào)【編程之舞】,獲取更多技術(shù)資源。