chromem-go:Go 語言 RAG 應用的高效輕量級向量數據庫

前言

在開發 RAGRetrieval-Augmented Generation)應用時,起初你可能更傾向於選擇一款輕量級的向量數據庫,而非複雜的大型數據庫。例如,在關係型數據庫的選擇上,許多人會更願意使用 SQLite 而不是 PostgreSQL 或 MySQL,以減少額外的配置和維護成本。

在 Go 語言中,chromem-go 提供了一種簡潔高效的解決方案——它是一款可嵌入到 Go 程序中的輕量級向量數據庫,無需單獨部署數據庫服務,極大地降低了使用門檻。

本文將深入探討 chromem-go 的使用方法,幫助你快速上手並應用到實際項目中。

準備好了嗎?準備一杯你最喜歡的咖啡或茶,隨着本文一探究竟吧。

chromem-go

介紹

chromem-go 是一個 Go 語言中可嵌入的向量數據庫,提供類似 Chroma 的接口且無第三方依賴。支持內存存儲,並可選持久化功能。

由於 chromem-go 是可嵌入的,因此你能夠在 Go 應用中添加檢索增強生成(Retrieval Augmented GenerationRAG)以及類似的基於嵌入(embeddings)的功能,而無需運行一個單獨的數據庫。這就像使用 SQLite 而不是 PostgreSQL/MySQL 等數據庫一樣。

這是一款獨立的向量數據庫,專爲 Go 語言設計,並非用於連接 Chroma 的庫,也不是 Chroma 的 Go 語言重新實現。

安裝

在 Go 項目根目錄下,執行以下命令:

go get github.com/philippgille/chromem-go

快速開始

package main

import (
"context"
"fmt"
"runtime"

"github.com/philippgille/chromem-go"
)

func main() {
 ctx := context.Background()

 db := chromem.NewDB()

 c, err := db.CreateCollection("knowledge-base", nil, nil)
if err != nil {
panic(err)
 }

 err = c.AddDocuments(ctx, []chromem.Document{
  {
   ID:      "1",
   Content: "The sky is blue because of Rayleigh scattering.",
  },
  {
   ID:      "2",
   Content: "Leaves are green because chlorophyll absorbs red and blue light.",
  },
 }, runtime.NumCPU())
if err != nil {
panic(err)
 }

 res, err := c.Query(ctx, "Why is the sky blue?", 1, nil, nil)
if err != nil {
panic(err)
 }

 fmt.Printf("ID: %v\nSimilarity: %v\nContent: %v\n", res[0].ID, res[0].Similarity, res[0].Content)
}

輸出:

ID: 1
Similarity: 0.6833369
Content: The sky is blue because of Rayleigh scattering.

注意事項:

在使用 db.CreateCollection 創建集合時,如果未顯式指定 EmbeddingFunc 參數,chromem-go 將默認使用 OpenAI 的嵌入實現。這意味着,你需要在環境變量中設置 OPENAI_API_KEY,否則數據插入操作將會失敗。

數據庫的創建

chromem-go 支持兩種數據庫的創建方式:內存數據庫 和 持久化的數據庫

db := chromem.NewDB()
db, err := chromem.NewPersistentDB("./chromem-go"true)

第一個參數爲保存數據庫文件的路徑參數,如果傳遞 "",那麼默認值爲 "./chromem-go"

第二個參數表示是否使用 gzip 格式壓縮數據。

集合操作

創建集合

c, err := db.CreateCollection("knowledge-base", nil, nil)

"knowledge-base":集合的名稱。

其餘的參數分別表示集合的 metadata 參數以及 Embedding 構建器的函數實現參數。

獲取集合

c := db.GetCollection("knowledge-base", nil)

第二個參數爲 Embedding 構建器的函數實現。

c, err := db.GetOrCreateCollection("knowledge-base", nil, nil)

獲取指定的集合,如果集合不存在則會創建新集合。該方法的參數和 db.CreateCollection 一樣。

collections := db.ListCollections()

該方法的返回值類型爲 map[string]*Collection

刪除集合

err := db.DeleteCollection("knowledge-base")

集合數據操作

添加集合數據

err := c.AddDocument(ctx, chromem.Document{})
err = c.AddDocuments(ctx, []chromem.Document{
 {
  ID:      "1",
  Content: "The sky is blue because of Rayleigh scattering.",
 },
 {
  ID:      "2",
  Content: "Leaves are green because chlorophyll absorbs red and blue light.",
 },
}, runtime.NumCPU())

AddDocument 和 AddDocuments 的作用分別爲添加單條和多條集合數據。chromem.Document 對象除了 Content 參數以外,還有 Embedding 參數,如果指定了 Embedding 參數,則不會調用 embedding function 對 Content 參數進行轉換。

查詢集合數據

c.GetByID(ctx, "1")
c.Query(ctx, "Why is the sky blue?", 1, nil, nil)
c.QueryEmbedding(ctx, []float32{0.1, 0.2, 0.3}, 1, nil, nil)

chromem-go 提供了三種查詢集合數據的方法,我們可以在不同的場景下選擇合適的方法進行使用。

刪除集合數據

c.Delete(ctx, metadataFilterMap, documentFilterMap, ids)

小結

本文詳細介紹了 chromem-go 的使用方法,包括但不僅限於 chromem-go 的介紹安裝方式 以及 數據庫的創建 等內容。如果你正嘗試開發一個 RAG 應用練練手時,相信 chromem-go 將是你向量數據庫的最佳選擇。

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/sJcaFg4MMkXoXHZ_V_Bqzg