可嵌入的矢量數據庫,用於具有色度樣界面和零第三方依賴性。可選持久性內存。
由於chromem-go是可嵌入的,因此您可以在GO應用中添加檢索增強生成(RAG)和類似的基於嵌入的功能,而無需運行單獨的數據庫。就像使用SQLITE代替PostgreSQL/MySQL/等時一樣。
它不是連接到色度的庫,也不是在GO中重新實現的。這是一個數據庫。
重點不是比例(數百萬個文檔)或功能數量,而是最常見用例的簡單性和性能。在2020 Intel筆記本電腦CPU中,您可以在0.3 ms中查詢1,000個文檔,並在40毫秒內查詢100,000個文檔,而記憶分配很少,並且很少。有關詳細信息,請參見基準。
配x 該項目是在Beta的,在重大結構下,可能會在v1.0.0之前引入發行版的破壞變化。所有更改均在CHANGELOG中記錄。
使用矢量數據庫,您可以做各種事情:
讓我們更詳細地看一下抹布用例:
大型語言模型(LLMS)的知識 - 即使是300億,700億參數以及更多的知識。他們對訓練結束後發生的事情一無所知,他們對未接受過培訓的數據一無所知(例如您公司的Intranet,Jira / Bug Tracker,Wiki或其他類型的知識庫),甚至他們知道他們經常無法完全重現的數據,而是開始幻覺。
微調LLM可以有所幫助,但它的意圖是改善有關特定主題的推理的LLM,或重現書面文本或代碼的樣式。微調不會在模型中添加知識1:1 。詳細信息丟失或混合。知識截止(關於微調後發生的任何事情)也沒有解決。
=>矢量數據庫可以充當LLM的最新,精確的知識:
text-embedding-3-small提供或可以創建它們。chromem-go可以為您做到這一點,並支持多個嵌入式提供商和模型。查看示例代碼以查看它的操作!
我們最初的靈感是Chroma界面,其核心API是以下內容(取自他們的README):
import chromadb
# setup Chroma in-memory, for easy prototyping. Can add persistence easily!
client = chromadb . Client ()
# Create collection. get_collection, get_or_create_collection, delete_collection also available!
collection = client . create_collection ( "all-my-documents" )
# Add docs to the collection. Can also update and delete. Row-based API coming soon!
collection . add (
documents = [ "This is document1" , "This is document2" ], # we handle tokenization, embedding, and indexing automatically. You can skip that and add your own embeddings as well
metadatas = [{ "source" : "notion" }, { "source" : "google-docs" }], # filter on these!
ids = [ "doc1" , "doc2" ], # unique for each doc
)
# Query/search 2 most similar results. You can also .get by id
results = collection . query (
query_texts = [ "This is a query document" ],
n_results = 2 ,
# where={"metadata_field": "is_equal_to_this"}, # optional filter
# where_document={"$contains":"search_string"} # optional filter
)我們的GO庫公開了相同的界面:
package main
import "github.com/philippgille/chromem-go"
func main () {
// Set up chromem-go in-memory, for easy prototyping. Can add persistence easily!
// We call it DB instead of client because there's no client-server separation. The DB is embedded.
db := chromem . NewDB ()
// Create collection. GetCollection, GetOrCreateCollection, DeleteCollection also available!
collection , _ := db . CreateCollection ( "all-my-documents" , nil , nil )
// Add docs to the collection. Update and delete will be added in the future.
// Can be multi-threaded with AddConcurrently()!
// We're showing the Chroma-like method here, but more Go-idiomatic methods are also available!
_ = collection . Add ( ctx ,
[] string { "doc1" , "doc2" }, // unique ID for each doc
nil , // We handle embedding automatically. You can skip that and add your own embeddings as well.
[] map [ string ] string {{ "source" : "notion" }, { "source" : "google-docs" }}, // Filter on these!
[] string { "This is document1" , "This is document2" },
)
// Query/search 2 most similar results. You can also get by ID.
results , _ := collection . Query ( ctx ,
"This is a query document" ,
2 ,
map [ string ] string { "metadata_field" : "is_equal_to_this" }, // optional filter
map [ string ] string { "$contains" : "search_string" }, // optional filter
)
}最初, chromem-go僅從四個核心方法開始,但隨著時間的推移,我們添加了更多。不過,我們故意不想覆蓋Chroma API表面的100%。
我們提供的一些替代方法更像是偶然的。
有關完整界面,請參見Godoc:https://pkg.go.dev/github.com/philippgille/chromem-go
chromem.EmbeddingFunc )chromem-go創建它們$contains , $not_contains io.Writer / io.Reader的方法EmbeddingFunc$and , $or等)go get github.com/philippgille/chromem-go@latest
請參閱Godoc的參考:https://pkg.go.dev/github.com/philippgille/chromem-go
有關完整的工作示例,請使用矢量數據庫進行檢索增強生成(RAG)和語義搜索,並使用OpenAI或本地運行嵌入式模型和LLM(在Ollama中),請參見示例代碼。
這是從“最小”示例中獲取的:
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 n Similarity: %v n Content: %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.
在2024-03-17的基準下進行:
$ go test -benchmem -run=^$ -bench .
goos: linux
goarch: amd64
pkg: github.com/philippgille/chromem-go
cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
BenchmarkCollection_Query_NoContent_100-8 13164 90276 ns/op 5176 B/op 95 allocs/op
BenchmarkCollection_Query_NoContent_1000-8 2142 520261 ns/op 13558 B/op 141 allocs/op
BenchmarkCollection_Query_NoContent_5000-8 561 2150354 ns/op 47096 B/op 173 allocs/op
BenchmarkCollection_Query_NoContent_25000-8 120 9890177 ns/op 211783 B/op 208 allocs/op
BenchmarkCollection_Query_NoContent_100000-8 30 39574238 ns/op 810370 B/op 232 allocs/op
BenchmarkCollection_Query_100-8 13225 91058 ns/op 5177 B/op 95 allocs/op
BenchmarkCollection_Query_1000-8 2226 519693 ns/op 13552 B/op 140 allocs/op
BenchmarkCollection_Query_5000-8 550 2128121 ns/op 47108 B/op 173 allocs/op
BenchmarkCollection_Query_25000-8 100 10063260 ns/op 211705 B/op 205 allocs/op
BenchmarkCollection_Query_100000-8 30 39404005 ns/op 810295 B/op 229 allocs/op
PASS
ok github.com/philippgille/chromem-go 28.402s go build ./...go test -v -race -count 1 ./...go test -benchmem -run=^$ -bench . (添加> bench.out或類似地寫入文件)go test -benchmem -run ^$ -cpuprofile cpu.out -bench .-cpuprofile , -memprofile , -blockprofile , -mutexprofile )benchstat : go install golang.org/x/perf/cmd/benchstat@latestbenchstat before.out after.out 在2023年12月,當我想在GO程序中進行檢索增強發電(RAG)時,我尋找了一個可以嵌入GO程序中的矢量數據庫,就像您嵌入SQLITE一樣,以便不需要任何單獨的DB設置和維護。考慮到GO生態系統中大量嵌入式鑰匙值商店,我感到驚訝。
當時,大多數流行的矢量數據庫,例如Pinecone,Qdrant,Milvus,Chroma,Weaviate等,或者根本不可嵌入Python或JavaScript/Typescript中。
然後,我找到了 @Eliben的博客文章和示例代碼,該文章表明,幾乎沒有GO代碼,您可以創建一個非常基本的矢量數據庫POC。
那時,我決定構建自己的矢量數據庫,該數據庫可在Chromadb接口的啟發下嵌入GO中。 Chromadb因可嵌入(在Python)而脫穎而出,並通過在其網站和網站的著陸頁上顯示其4個命令中的核心API。