Backends¶
BaseEmbedder
¶
The Base Embedder used for creating embedding models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding_model |
The main embedding model to be used for extracting document and word embedding |
None
|
Source code in bertopic\backend\_base.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
embed(documents, verbose=False)
¶
Embed a list of n documents/words into an n-dimensional matrix of embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents |
List[str]
|
A list of documents or words to be embedded |
required |
verbose |
bool
|
Controls the verbosity of the process |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Document/words embeddings with shape (n, m) with |
ndarray
|
that each have an embeddings size of |
Source code in bertopic\backend\_base.py
16 17 18 19 20 21 22 23 24 25 26 27 28 | |
embed_documents(document, verbose=False)
¶
Embed a list of n documents into an n-dimensional matrix of embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document |
List[str]
|
A list of documents to be embedded |
required |
verbose |
bool
|
Controls the verbosity of the process |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Document embeddings with shape (n, m) with |
ndarray
|
that each have an embeddings size of |
Source code in bertopic\backend\_base.py
48 49 50 51 52 53 54 55 56 57 58 59 60 | |
embed_words(words, verbose=False)
¶
Embed a list of n words into an n-dimensional matrix of embeddings.
Words are the short texts BERTopic compares with documents or topics, such as a search term, a zero-shot label or a keyword. They are embedded as documents, unless a backend embeds them differently, such as without a prompt meant for documents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
words |
List[str]
|
A list of words to be embedded |
required |
verbose |
bool
|
Controls the verbosity of the process |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Word embeddings with shape (n, m) with |
ndarray
|
that each have an embeddings size of |
Source code in bertopic\backend\_base.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
CohereBackend
¶
Bases: BaseEmbedder
Cohere Embedding Model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client |
A |
required | |
embedding_model |
str
|
A Cohere model. Default is "large". For an overview of models see: https://docs.cohere.ai/docs/generation-card |
'large'
|
delay_in_seconds |
float | None
|
If a |
None
|
batch_size |
int | None
|
The size of each batch. |
None
|
embed_kwargs |
Mapping[str, Any] | None
|
Kwargs passed to |
None
|
Examples:
import cohere
from bertopic.backend import CohereBackend
client = cohere.Client("APIKEY")
cohere_model = CohereBackend(client)
If you want to specify input_type:
cohere_model = CohereBackend(
client,
embedding_model="embed-english-v3.0",
embed_kwargs={"input_type": "clustering"}
)
Source code in bertopic\backend\_cohere.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
embed(documents, verbose=False)
¶
Embed a list of n documents/words into an n-dimensional matrix of embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents |
List[str]
|
A list of documents or words to be embedded |
required |
verbose |
bool
|
Controls the verbosity of the process |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Document/words embeddings with shape (n, m) with |
ndarray
|
that each have an embeddings size of |
Source code in bertopic\backend\_cohere.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
FastEmbedBackend
¶
Bases: BaseEmbedder
FastEmbed embedding model.
The FastEmbed embedding model used for generating sentence embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding_model |
str
|
A FastEmbed embedding model |
'BAAI/bge-small-en-v1.5'
|
Examples: To create a model, you can load in a string pointing to a supported FastEmbed model:
from bertopic.backend import FastEmbedBackend
sentence_model = FastEmbedBackend("BAAI/bge-small-en-v1.5")
Source code in bertopic\backend\_fastembed.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
embed(documents, verbose=False)
¶
Embed a list of n documents/words into an n-dimensional matrix of embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents |
List[str]
|
A list of documents or words to be embedded |
required |
verbose |
bool
|
Controls the verbosity of the process |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Document/words embeddings with shape (n, m) with |
ndarray
|
that each have an embeddings size of |
Source code in bertopic\backend\_fastembed.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
LangChainBackend
¶
Bases: BaseEmbedder
LangChain Embedding Model.
This class uses the LangChain Embedding class to embed the documents. Argument: embedding_model: A LangChain Embedding Instance.
Examples:
from langchain_community.embeddings import HuggingFaceInstructEmbeddings
from bertopic.backend import LangChainBackend
hf_embedding = HuggingFaceInstructEmbeddings()
langchain_embedder = LangChainBackend(hf_embedding)
Source code in bertopic\backend\_langchain.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
embed(documents, verbose=False)
¶
Embed a list of n documents/words into an n-dimensional matrix of embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents |
List[str]
|
A list of documents or words to be embedded |
required |
verbose |
bool
|
Controls the verbosity of the process |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Document/words embeddings with shape (n, m) with |
ndarray
|
that each have an embeddings size of |
Source code in bertopic\backend\_langchain.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
Model2VecBackend
¶
Bases: BaseEmbedder
Model2Vec embedding model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding_model |
Union[str, StaticModel]
|
Either a model2vec model or a string pointing to a model2vec model |
required |
distill |
bool
|
Indicates whether to distill a sentence-transformers compatible model.
The distillation will happen during fitting of the topic model.
NOTE: Only works if |
False
|
distill_kwargs |
dict | None
|
Keyword arguments to pass to the distillation process
of |
None
|
distill_vectorizer |
str | None
|
A CountVectorizer used for creating a custom vocabulary
based on the same documents used for topic modeling.
NOTE: If "vocabulary" is in |
None
|
Examples: To create a model, you can load in a string pointing to a model2vec model:
from bertopic.backend import Model2VecBackend
sentence_model = Model2VecBackend("minishlab/potion-base-8M")
or you can instantiate a model yourself:
from bertopic.backend import Model2VecBackend
from model2vec import StaticModel
embedding_model = StaticModel.from_pretrained("minishlab/potion-base-8M")
sentence_model = Model2VecBackend(embedding_model)
If you want to distill a sentence-transformers model with the vocabulary of the documents, run the following:
from bertopic.backend import Model2VecBackend
sentence_model = Model2VecBackend("sentence-transformers/all-MiniLM-L6-v2", distill=True)
Source code in bertopic\backend\_model2vec.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
embed(documents, verbose=False)
¶
Embed a list of n documents/words into an n-dimensional matrix of embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents |
List[str]
|
A list of documents or words to be embedded |
required |
verbose |
bool
|
Controls the verbosity of the process |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Document/words embeddings with shape (n, m) with |
ndarray
|
that each have an embeddings size of |
Source code in bertopic\backend\_model2vec.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
MultiModalBackend
¶
Bases: BaseEmbedder
Multimodal backend using Sentence-transformers.
One model may cover every modality, or each may be given its own. A modality without
its own model falls back to embedding_model.
Note that rows of different modalities are only comparable when a single model embeds them all, since two separately trained encoders do not share a vector space. Per-modality models are therefore for corpora of one modality; mixed corpora want a joint model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding_model |
Union[str, SentenceTransformer]
|
A sentence-transformers embedding model covering every modality that has no model of its own, text included. |
required |
image_model |
Union[str, SentenceTransformer]
|
A sentence-transformers embedding model used to embed only images. |
None
|
audio_model |
Union[str, SentenceTransformer]
|
A sentence-transformers embedding model used to embed only audio. |
None
|
video_model |
Union[str, SentenceTransformer]
|
A sentence-transformers embedding model used to embed only video. |
None
|
code_model |
Union[str, SentenceTransformer]
|
A sentence-transformers embedding model used to embed only code. |
None
|
batch_size |
int
|
The number of items to embed at a time. Images, audio and video are capped at smaller batches of their own to prevent using too much memory. |
32
|
Examples: To create a model, you can load in a string pointing to a sentence-transformers model:
from bertopic.backend import MultiModalBackend
sentence_model = MultiModalBackend("google/embeddinggemma-2")
or you can instantiate a model yourself:
from bertopic.backend import MultiModalBackend
from sentence_transformers import SentenceTransformer
embedding_model = SentenceTransformer("google/embeddinggemma-2", default_prompt_name="Clustering")
embedding_model.max_seq_length = 1024
sentence_model = MultiModalBackend(embedding_model)
Source code in bertopic\backend\_multimodal.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
embed(documents, verbose=False)
¶
Embed a list of n documents/words into an n-dimensional matrix of embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents |
List[str]
|
A list of documents or words to be embedded |
required |
verbose |
bool
|
Controls the verbosity of the process |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Document/words embeddings with shape (n, m) with |
ndarray
|
that each have an embeddings size of |
Source code in bertopic\backend\_multimodal.py
91 92 93 94 95 96 97 98 99 100 101 102 | |
embed_media(media, modality=Modality.TEXT, verbose=False)
¶
Embed a list of n items sharing one modality, with the model that handles it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
media |
List
|
A list of items, all of the same modality |
required |
modality |
Modality
|
Which modality the items are, choosing the model that embeds them |
TEXT
|
verbose |
bool
|
Controls the verbosity of the process |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Embeddings with shape (n, m) with |
ndarray
|
embeddings size of |
Source code in bertopic\backend\_multimodal.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
embed_words(words, verbose=False)
¶
Embed a list of n words into an n-dimensional matrix of embeddings.
A clustering prompt is meant for texts that are clustered together, and on a few words it outweighs what they say, so words are embedded without the model's prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
words |
List[str]
|
A list of words to be embedded |
required |
verbose |
bool
|
Controls the verbosity of the process |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Word embeddings with shape (n, m) with |
ndarray
|
that each have an embeddings size of |
Source code in bertopic\backend\_multimodal.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
OpenAIBackend
¶
Bases: BaseEmbedder
OpenAI Embedding Model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client |
OpenAI
|
A |
required |
embedding_model |
str
|
An OpenAI model. Default is For an overview of models see: https://platform.openai.com/docs/models/embeddings |
'text-embedding-ada-002'
|
delay_in_seconds |
float | None
|
If a |
None
|
batch_size |
int | None
|
The size of each batch. |
None
|
generator_kwargs |
Mapping[str, Any] | None
|
Kwargs passed to |
None
|
Examples:
import openai
from bertopic.backend import OpenAIBackend
client = openai.OpenAI(api_key="sk-...")
openai_embedder = OpenAIBackend(client, "text-embedding-ada-002")
Source code in bertopic\backend\_openai.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
embed(documents, verbose=False)
¶
Embed a list of n documents/words into an n-dimensional matrix of embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents |
List[str]
|
A list of documents or words to be embedded |
required |
verbose |
bool
|
Controls the verbosity of the process |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Document/words embeddings with shape (n, m) with |
ndarray
|
that each have an embeddings size of |
Source code in bertopic\backend\_openai.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |