Skip to content

Commit

Permalink
Add and improve Godoc
Browse files Browse the repository at this point in the history
  • Loading branch information
philippgille committed Dec 30, 2023
1 parent 0bd196e commit c3a4db9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
11 changes: 10 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@ import (
"sync"
)

// EmbeddingFunc is a function that creates embeddings for a given document.
// chromem-go will use OpenAI`s ada v2 model by default, but you can provide your
// own function, using any model you like.
type EmbeddingFunc func(ctx context.Context, document string) ([]float32, error)

// Client is the chromem-go client. It holds collections, which hold documents.
//
// +--------+ 1-n +------------+ n-n +----------+
// | Client |-----------| Collection |-----------| Document |
// +--------+ +------------+ +----------+
type Client struct {
collections map[string]*Collection
collectionsLock sync.RWMutex
}

// NewClient creates a new chromem-go client.
func NewClient() *Client {
return &Client{
collections: make(map[string]*Collection),
Expand All @@ -22,7 +31,7 @@ func NewClient() *Client {
//
// - name: The name of the collection to create.
// - metadata: Optional metadata to associate with the collection.
// - embedding_function: Optional function to use to embed documents.
// - embeddingFunc: Optional function to use to embed documents.
// Uses the default embedding function if not provided.
func (c *Client) CreateCollection(name string, metadata map[string]string, embeddingFunc EmbeddingFunc) *Collection {
if embeddingFunc == nil {
Expand Down
3 changes: 3 additions & 0 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"sync"
)

// Collection represents a collection of documents.
// It also has a configured embedding function, which is used when adding documents
// that don't have embeddings yet.
type Collection struct {
Name string
Metadata map[string]string
Expand Down
11 changes: 11 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,30 @@ import (

var supportedFilters = []string{"$contains", "$not_contains"}

// Result represents a single result from a query.
type Result struct {
ID string
Embedding []float32
Metadata map[string]string
Document string

// The cosine similarity between the query and the document.
// The higher the value, the more similar the document is to the query.
// The value is in the range [-1, 1].
Similarity float32
}

// Performs a nearest neighbors query on a collection specified by UUID.
//
// - queryText: The text to search for.
// - nResults: The number of results to return. Must be > 0.
// - where: Conditional filtering on metadata. Optional.
// - whereDocument: Conditional filtering on documents. Optional.
func (c *Collection) Query(ctx context.Context, queryText string, nResults int, where, whereDocument map[string]string) ([]Result, error) {
if nResults == 0 {
return nil, errors.New("nResults must be > 0")
}

// Validate whereDocument operators
for k := range whereDocument {
if !slices.Contains(supportedFilters, k) {
Expand Down

0 comments on commit c3a4db9

Please sign in to comment.