Skip to main content
This example goes over how to use AI21SemanticTextSplitter in LangChain.

Installation

Environment setup

We’ll need to get a AI21 API key and set the AI21_API_KEY environment variable:

Example usages

Splitting text by semantic meaning

This example shows how to use AI21SemanticTextSplitter to split a text into chunks based on semantic meaning.

Splitting text by semantic meaning with merge

This example shows how to use AI21SemanticTextSplitter to split a text into chunks based on semantic meaning, then merging the chunks based on chunk_size.

Splitting text to documents

This example shows how to use AI21SemanticTextSplitter to split a text into Documents based on semantic meaning. The metadata will contain a type for each document.

Creating documents with metadata

This example shows how to use AI21SemanticTextSplitter to create Documents from texts, and adding custom Metadata to each Document.

Splitting text to documents with start index

This example shows how to use AI21SemanticTextSplitter to split a text into Documents based on semantic meaning. The metadata will contain a start index for each document. Note that the start index provides an indication of the order of the chunks rather than the actual start index for each chunk.

Splitting documents

This example shows how to use AI21SemanticTextSplitter to split a list of Documents into chunks based on semantic meaning.