...

Source file src/gitlab.hexacode.org/go-libs/chromem-go/document.go

Documentation: gitlab.hexacode.org/go-libs/chromem-go

     1  package chromem
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  )
     7  
     8  // Document represents a single document.
     9  type Document struct {
    10  	ID        string
    11  	Metadata  map[string]string
    12  	Embedding []float32
    13  	Content   string
    14  
    15  	// ⚠️ When adding unexported fields here, consider adding a persistence struct
    16  	// version of this in [DB.Export] and [DB.Import].
    17  }
    18  
    19  // NewDocument creates a new document, including its embeddings.
    20  // Metadata is optional.
    21  // If the embeddings are not provided, they are created using the embedding function.
    22  // You can leave the content empty if you only want to store embeddings.
    23  // If embeddingFunc is nil, the default embedding function is used.
    24  //
    25  // If you want to create a document without embeddings, for example to let [Collection.AddDocuments]
    26  // create them concurrently, you can create a document with `chromem.Document{...}`
    27  // instead of using this constructor.
    28  func NewDocument(ctx context.Context, id string, metadata map[string]string, embedding []float32, content string, embeddingFunc EmbeddingFunc) (Document, error) {
    29  	if id == "" {
    30  		return Document{}, errors.New("id is empty")
    31  	}
    32  	if len(embedding) == 0 && content == "" {
    33  		return Document{}, errors.New("either embedding or content must be filled")
    34  	}
    35  	if embeddingFunc == nil {
    36  		embeddingFunc = NewEmbeddingFuncDefault()
    37  	}
    38  
    39  	if len(embedding) == 0 {
    40  		var err error
    41  		embedding, err = embeddingFunc(ctx, content)
    42  		if err != nil {
    43  			return Document{}, err
    44  		}
    45  	}
    46  
    47  	return Document{
    48  		ID:        id,
    49  		Metadata:  metadata,
    50  		Embedding: embedding,
    51  		Content:   content,
    52  	}, nil
    53  }
    54  

View as plain text