SemanticChunker#

class langchain_experimental.text_splitter.SemanticChunker(embeddings: Embeddings, buffer_size: int = 1, add_start_index: bool = False, breakpoint_threshold_type: Literal['percentile', 'standard_deviation', 'interquartile', 'gradient'] = 'percentile', breakpoint_threshold_amount: float | None = None, number_of_chunks: int | None = None, sentence_split_regex: str = '(?<=[.?!])\\s+')[source]#

Split the text based on semantic similarity.

Taken from Greg Kamradt’s wonderful notebook: FullStackRetrieval-com/RetrievalTutorials

All credits to him.

At a high level, this splits into sentences, then groups into groups of 3 sentences, and then merges one that are similar in the embedding space.

Methods

__init__(embeddings[,Β buffer_size,Β ...])

atransform_documents(documents,Β **kwargs)

Asynchronously transform a list of documents.

create_documents(texts[,Β metadatas])

Create documents from a list of texts.

split_documents(documents)

Split documents.

split_text(text)

transform_documents(documents,Β **kwargs)

Transform sequence of documents by splitting them.

Parameters:
  • embeddings (Embeddings) –

  • buffer_size (int) –

  • add_start_index (bool) –

  • breakpoint_threshold_type (Literal['percentile', 'standard_deviation', 'interquartile', 'gradient']) –

  • breakpoint_threshold_amount (float | None) –

  • number_of_chunks (int | None) –

  • sentence_split_regex (str) –

__init__(embeddings: Embeddings, buffer_size: int = 1, add_start_index: bool = False, breakpoint_threshold_type: Literal['percentile', 'standard_deviation', 'interquartile', 'gradient'] = 'percentile', breakpoint_threshold_amount: float | None = None, number_of_chunks: int | None = None, sentence_split_regex: str = '(?<=[.?!])\\s+')[source]#
Parameters:
  • embeddings (Embeddings) –

  • buffer_size (int) –

  • add_start_index (bool) –

  • breakpoint_threshold_type (Literal['percentile', 'standard_deviation', 'interquartile', 'gradient']) –

  • breakpoint_threshold_amount (float | None) –

  • number_of_chunks (int | None) –

  • sentence_split_regex (str) –

async atransform_documents(documents: Sequence[Document], **kwargs: Any) β†’ Sequence[Document]#

Asynchronously transform a list of documents.

Parameters:
  • documents (Sequence[Document]) – A sequence of Documents to be transformed.

  • kwargs (Any) –

Returns:

A sequence of transformed Documents.

Return type:

Sequence[Document]

create_documents(texts: List[str], metadatas: List[dict] | None = None) β†’ List[Document][source]#

Create documents from a list of texts.

Parameters:
  • texts (List[str]) –

  • metadatas (List[dict] | None) –

Return type:

List[Document]

split_documents(documents: Iterable[Document]) β†’ List[Document][source]#

Split documents.

Parameters:

documents (Iterable[Document]) –

Return type:

List[Document]

split_text(text: str) β†’ List[str][source]#
Parameters:

text (str) –

Return type:

List[str]

transform_documents(documents: Sequence[Document], **kwargs: Any) β†’ Sequence[Document][source]#

Transform sequence of documents by splitting them.

Parameters:
  • documents (Sequence[Document]) –

  • kwargs (Any) –

Return type:

Sequence[Document]

Examples using SemanticChunker