Un cliente simple de la base de datos de Chroma Vector escrito en GO
Funciona con la versión de Chroma: V0.4.3 - V0.5.x
Invitamos a los usuarios a visitar el sitio de documentos para la biblioteca para obtener información más profunda: Chroma Go Docs
0.2.0 +, también admitimos el modelo predeterminado all-MiniLM-L6-v2 que se ejecuta en ONNX Runtime (ORT). De la versión 0.2.0 el cliente Chroma GO también admite funciones de rehabilitación. Los siguientes son compatibles:
Importante
Hay muchos cambios nuevos que conducen a v0.2.0 , como se documenta a continuación. Si desea usarlos, instale la última versión del cliente.
go get github.com/amikos-tech/chroma-go@maingo get github.com/amikos-tech/chroma-goImportar:
import (
chroma "github.com/amikos-tech/chroma-go"
)Asegúrese de tener una instancia en ejecución de Chroma Running. Recomendamos una de las dos opciones siguientes:
Docker , minikube y kubectl instalados)La configuración (nube-nativa):
minikube start --profile chromago
minikube profile chromago
helm repo add chroma https://amikos-tech.github.io/chromadb-chart/
helm repo update
helm install chroma chroma/chromadb --set chromadb.allowReset=true,chromadb.apiVersion=0.4.5 | Nota: para eliminar el clúster de minikube: minikube delete --profile chromago
Considere el siguiente ejemplo donde:
package main
import (
"context"
"fmt"
"log"
"os"
chroma "github.com/amikos-tech/chroma-go"
"github.com/amikos-tech/chroma-go/collection"
openai "github.com/amikos-tech/chroma-go/pkg/embeddings/openai"
"github.com/amikos-tech/chroma-go/types"
)
func main () {
// Create a new Chroma client
client , err := chroma . NewClient ( chroma . WithBasePath ( "http://localhost:8000" ))
if err != nil {
log . Fatalf ( "Error creating client: %s n " , err )
return
}
// Close the client to release any resources such as local embedding functions
defer func () {
err = client . Close ()
if err != nil {
log . Fatalf ( "Error closing client: %s n " , err )
}
}()
// Create a new collection with options. We don't provide an embedding function here, so the default embedding function will be used
newCollection , err := client . NewCollection (
context . TODO (),
"test-collection" ,
collection . WithMetadata ( "key1" , "value1" ),
collection . WithHNSWDistanceFunction ( types . L2 ),
)
if err != nil {
log . Fatalf ( "Error creating collection: %s n " , err )
}
// Create a new record set with to hold the records to insert
rs , err := types . NewRecordSet (
types . WithEmbeddingFunction ( newCollection . EmbeddingFunction ), // we pass the embedding function from the collection
types . WithIDGenerator ( types . NewULIDGenerator ()),
)
if err != nil {
log . Fatalf ( "Error creating record set: %s n " , err )
}
// Add a few records to the record set
rs . WithRecord ( types . WithDocument ( "My name is John. And I have two dogs." ), types . WithMetadata ( "key1" , "value1" ))
rs . WithRecord ( types . WithDocument ( "My name is Jane. I am a data scientist." ), types . WithMetadata ( "key2" , "value2" ))
// Build and validate the record set (this will create embeddings if not already present)
_ , err = rs . BuildAndValidate ( context . TODO ())
if err != nil {
log . Fatalf ( "Error validating record set: %s n " , err )
}
// Add the records to the collection
_ , err = newCollection . AddRecords ( context . Background (), rs )
if err != nil {
log . Fatalf ( "Error adding documents: %s n " , err )
}
// Count the number of documents in the collection
countDocs , qrerr := newCollection . Count ( context . TODO ())
if qrerr != nil {
log . Fatalf ( "Error counting documents: %s n " , qrerr )
}
// Query the collection
fmt . Printf ( "countDocs: %v n " , countDocs ) //this should result in 2
qr , qrerr := newCollection . Query ( context . TODO (), [] string { "I love dogs" }, 5 , nil , nil , nil )
if qrerr != nil {
log . Fatalf ( "Error querying documents: %s n " , qrerr )
}
fmt . Printf ( "qr: %v n " , qr . Documents [ 0 ][ 0 ]) //this should result in the document about dogs
}make buildmake testmake generate make lint-fixNota: Docker debe estar instalado
make server