mongovector

package
v0.1.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package mongovector implements a vector store using MongoDB as the backend.

The mongovector package provides a way to store and retrieve document embeddings using MongoDB's vector search capabilities. It implements the VectorStore interface from the vectorstores package, allowing it to be used interchangeably with other vector store implementations.

Key features:

  • Store document embeddings in MongoDB
  • Perform similarity searches on stored embeddings
  • Configurable index and path settings
  • Support for custom embedding functions

Main types:

  • Store: The main type that implements the VectorStore interface
  • Option: A function type for configuring the Store

Usage:

import (
    "github.com/czc09/langchaingo/vectorstores/mongovector"
    "go.mongodb.org/mongo-driver/mongo"
)

// Create a new Store
coll := // ... obtain a *mongo.Collection
embedder := // ... obtain an embeddings.Embedder
store := mongovector.New(coll, embedder)

// Add documents
docs := []schema.Document{
    {PageContent: "Document 1"},
    {PageContent: "Document 2"},
}
ids, err := store.AddDocuments(context.Background(), docs)

// Perform similarity search
results, err := store.SimilaritySearch(context.Background(), "query", 5)

The package also provides options for customizing the Store:

  • WithIndex: Set a custom index name
  • WithPath: Set a custom path for the vector field
  • WithNumCandidates: Set the number of candidates for similarity search

For more detailed information, see the documentation for individual types and functions.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmbedderWrongNumberVectors = errors.New("number of vectors from embedder does not match number of documents")
	ErrUnsupportedOptions         = errors.New("unsupported options")
	ErrInvalidScoreThreshold      = errors.New("score threshold must be between 0 and 1")
)

Functions

This section is empty.

Types

type Option

type Option func(p *Store)

Option sets mongovector-specific options when constructing a Store.

func WithIndex

func WithIndex(index string) Option

WithIndex will set the default index to use when adding or searching documents with the store.

Atlas Vector Search doesn't return results if you misspell the index name or if the specified index doesn't already exist on the cluster.

The index can be update at the operation level with the NameSpace vectorstores option.

func WithNumCandidates

func WithNumCandidates(numCandidates int) Option

WithNumCandidates sets the number of nearest neighbors to use during a similarity search. By default this value is 10 times the number of documents (or limit) passed as an argument to SimilaritySearch.

func WithPath

func WithPath(path string) Option

WithPath will set the path parameter used by the Atlas Search operators to specify the field or fields to be searched.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store wraps a Mongo collection for writing to and searching an Atlas vector database.

func New

func New(coll *mongo.Collection, embedder embeddings.Embedder, opts ...Option) Store

New returns a Store that can read and write to the vector store.

func (*Store) AddDocuments

func (store *Store) AddDocuments(
	ctx context.Context,
	docs []schema.Document,
	opts ...vectorstores.Option,
) ([]string, error)

AddDocuments will create embeddings for the given documents using the user-specified embedding model, then insert that data into a vector store.

func (*Store) SimilaritySearch

func (store *Store) SimilaritySearch(
	ctx context.Context,
	query string,
	numDocuments int,
	opts ...vectorstores.Option,
) ([]schema.Document, error)

SimilaritySearch searches a vector store from the vector transformed from the query by the user-specified embedding model.

This method searches the store-wrapped collection with an optionally provided index at instantiation, with a default index of "vector_index". Since multiple indexes can be defined for a collection, the options.NameSpace value can be used here to change the search index. The priority is options.NameSpace > Store.index > defaultIndex.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL