Skip to content

Vector Stores

ChromaStore

ChromaStore()

ChromaStore persistence interface.

Purpose

Adds LangChain documents to a persistent Chroma vector collection.

Attributes:

Name Type Description
documents List[Document]

Documents for the current write.

embedder Embeddings | None

Embedding adapter.

collection_name str

Chroma collection name.

persist_directory str

Persistent collection directory.

vector_store Chroma | None

Active Chroma vector-store instance.

Initialize Chroma vector storage state without connecting to a provider.

Purpose

Initialize Chroma vector storage state without connecting to a provider.

Returns:

Name Type Description
None None

Completes the operation through side effects.

Source code in storage.py
def __init__( self ) -> None:
	"""Initialize Chroma vector storage state without connecting to a provider.

	Purpose:
		Initialize Chroma vector storage state without connecting to a provider.

	Returns:
		None: Completes the operation through side effects.
	"""
	self.documents: List[ Document ] = [ ]
	self.embedder: Embeddings | None = None
	self.collection_name = ''
	self.persist_directory = ''
	self.vector_store: Chroma | None = None

Methods:

create

create(
    documents: List[Document],
    embedder: Embeddings,
    collection_name: str,
    persist_directory: str,
) -> Chroma

Store LangChain document chunks in a persistent Chroma collection.

Purpose

Store LangChain document chunks in a persistent Chroma collection.

Parameters:

Name Type Description Default
documents List[Document]

LangChain documents for vector storage.

required
embedder Embeddings

LangChain embedding implementation.

required
collection_name str

Chroma collection name.

required
persist_directory str

Chroma persistence directory.

required

Returns:

Name Type Description
Chroma Chroma

Result of the create operation.

Source code in storage.py
def create( self, documents: List[ Document ], embedder: Embeddings, collection_name: str,
	persist_directory: str ) -> Chroma:
	"""Store LangChain document chunks in a persistent Chroma collection.

	Purpose:
		Store LangChain document chunks in a persistent Chroma collection.

	Args:
		documents (List[ Document ]): LangChain documents for vector storage.
		embedder (Embeddings): LangChain embedding implementation.
		collection_name (str): Chroma collection name.
		persist_directory (str): Chroma persistence directory.

	Returns:
		Chroma: Result of the create operation.
	"""
	try:
		throw_if( 'documents', documents )
		throw_if( 'embedder', embedder )
		throw_if( 'collection_name', collection_name )
		throw_if( 'persist_directory', persist_directory )
		if not isinstance( documents, list ) or not all(
				isinstance( document, Document ) for document in documents ):
			raise TypeError( 'documents must be a list of LangChain Documents.' )
		if not isinstance( embedder, Embeddings ):
			raise TypeError( 'embedder must implement LangChain Embeddings.' )
		if not isinstance( collection_name, str ) or not isinstance( persist_directory, str ):
			raise TypeError( 'collection_name and persist_directory must be strings.' )
		self.documents = documents
		self.embedder = embedder
		self.collection_name = collection_name
		self.persist_directory = persist_directory
		ids = vector_document_ids( self.documents )
		Path( self.persist_directory ).mkdir( parents=True, exist_ok=True )
		store = Chroma( collection_name=self.collection_name, embedding_function=self.embedder,
			persist_directory=self.persist_directory )
		store.add_documents( documents=self.documents, ids=ids )
		self.vector_store = store
		return self.vector_store
	except Exception as e:
		exception = Error( e )
		exception.module = 'storage'
		exception.cause = 'ChromaStore'
		exception.method = "create( self, **args ) -> Chroma"
		Logger( ).write( exception )
		raise exception from e

PineconeStore

PineconeStore()

PineconeStore persistence interface.

Purpose

Adds LangChain documents to an existing managed Pinecone index.

Attributes:

Name Type Description
documents List[Document]

Documents for the current write.

embedder Embeddings | None

Embedding adapter.

index_name str

Pinecone index name.

namespace str

Pinecone namespace.

api_key str

Pinecone credential.

client Any

Pinecone client.

vector_store Any

Active Pinecone vector-store adapter.

Initialize Pinecone vector storage state without connecting to a provider.

Purpose

Initialize Pinecone vector storage state without connecting to a provider.

Returns:

Name Type Description
None None

Completes the operation through side effects.

Source code in storage.py
def __init__( self ) -> None:
	"""Initialize Pinecone vector storage state without connecting to a provider.

	Purpose:
		Initialize Pinecone vector storage state without connecting to a provider.

	Returns:
		None: Completes the operation through side effects.
	"""
	self.documents: List[ Document ] = [ ]
	self.embedder: Embeddings | None = None
	self.index_name = ''
	self.namespace = ''
	self.api_key = ''
	self.client: Any = None
	self.vector_store: Any = None

Methods:

create

create(
    documents: List[Document],
    embedder: Embeddings,
    index_name: str,
    namespace: str,
    api_key: str,
) -> Any

Store LangChain document chunks in an existing Pinecone index.

Purpose

Store LangChain document chunks in an existing Pinecone index.

Parameters:

Name Type Description Default
documents List[Document]

LangChain documents for vector storage.

required
embedder Embeddings

LangChain embedding implementation.

required
index_name str

Existing Pinecone index name.

required
namespace str

Optional cache namespace.

required
api_key str

Pinecone API credential.

required

Returns:

Name Type Description
Any Any

Result of the create operation.

Source code in storage.py
def create( self, documents: List[ Document ], embedder: Embeddings, index_name: str,
	namespace: str, api_key: str ) -> Any:
	"""Store LangChain document chunks in an existing Pinecone index.

	Purpose:
		Store LangChain document chunks in an existing Pinecone index.

	Args:
		documents (List[ Document ]): LangChain documents for vector storage.
		embedder (Embeddings): LangChain embedding implementation.
		index_name (str): Existing Pinecone index name.
		namespace (str): Optional cache namespace.
		api_key (str): Pinecone API credential.

	Returns:
		Any: Result of the create operation.
	"""
	try:
		throw_if( 'documents', documents )
		throw_if( 'embedder', embedder )
		throw_if( 'index_name', index_name )
		throw_if( 'api_key', api_key )
		if not isinstance( documents, list ) or not all(
				isinstance( document, Document ) for document in documents ):
			raise TypeError( 'documents must be a list of LangChain Documents.' )
		if not isinstance( embedder, Embeddings ):
			raise TypeError( 'embedder must implement LangChain Embeddings.' )
		if not isinstance( index_name, str ) or not isinstance( api_key, str ):
			raise TypeError( 'index_name and api_key must be strings.' )
		if namespace is None:
			throw_if( 'namespace', namespace )
		if not isinstance( namespace, str ):
			raise TypeError( 'namespace must be a string; empty is allowed.' )
		self.documents = documents
		self.embedder = embedder
		self.index_name = index_name
		self.namespace = namespace
		self.api_key = api_key
		ids = vector_document_ids( self.documents )
		from langchain_pinecone import PineconeVectorStore
		from pinecone import Pinecone
		client = Pinecone( api_key=self.api_key )
		if not client.indexes.exists( self.index_name ):
			raise ValueError( f'Pinecone index does not exist: {self.index_name}' )
		index = client.index( name=self.index_name )
		store = PineconeVectorStore( index=index, embedding=self.embedder, namespace=self.namespace or None )
		store.add_documents( documents=self.documents, ids=ids )
		self.client = client
		self.vector_store = store
		return self.vector_store
	except Exception as e:
		exception = Error( e )
		exception.module = 'storage'
		exception.cause = 'PineconeStore'
		exception.method = "create( self, **args ) -> Any"
		Logger( ).write( exception )
		raise exception from e