pydantic_ai.embeddings
EmbeddingModel
Bases: ABC
Abstract class for a model.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/base.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 | |
__init__
__init__(
*, settings: EmbeddingSettings | None = None
) -> None
Initialize the model with optional settings and profile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
EmbeddingSettings | None
|
Model-specific settings that will be used as defaults for this model. |
None
|
Source code in pydantic_ai_slim/pydantic_ai/embeddings/base.py
13 14 15 16 17 18 19 20 21 22 23 | |
prepare_embed
prepare_embed(
inputs: str | Sequence[str],
settings: EmbeddingSettings | None = None,
) -> tuple[list[str], EmbeddingSettings]
Prepare the inputs and settings for the embedding.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/base.py
53 54 55 56 57 58 59 60 61 | |
max_input_tokens
async
max_input_tokens() -> int | None
Get the maximum number of tokens that can be input to the model.
None means unknown.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/base.py
63 64 65 66 67 68 | |
InstrumentedEmbeddingModel
dataclass
Bases: WrapperEmbeddingModel
Embedding model which wraps another model so that requests are instrumented with OpenTelemetry.
See the Debugging and Monitoring guide for more info.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/instrumented.py
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
instrumentation_settings
instance-attribute
instrumentation_settings: InstrumentationSettings = (
options or InstrumentationSettings()
)
Instrumentation settings for this model.
instrument_embedding_model
instrument_embedding_model(
model: EmbeddingModel,
instrument: InstrumentationSettings | bool,
) -> EmbeddingModel
Instrument an embedding model with OpenTelemetry/logfire.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/instrumented.py
30 31 32 33 34 35 36 37 38 | |
EmbeddingResult
dataclass
The result of an embedding operation.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/result.py
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 | |
__getitem__
Get the embedding for an input or input index.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/result.py
39 40 41 42 43 44 | |
cost
cost() -> PriceCalculation
Calculate the cost of the usage.
Uses genai-prices.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/result.py
46 47 48 49 50 51 52 53 54 55 56 57 | |
EmbeddingSettings
Bases: TypedDict
Settings to configure an embedding model.
Here we include only settings which apply to multiple models / model providers, though not all of these settings are supported by all models.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/settings.py
4 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 | |
dimensions
instance-attribute
dimensions: int
The number of dimensions the resulting output embeddings should have.
Supported by:
- OpenAI
- Cohere
extra_headers
instance-attribute
Extra headers to send to the model.
Supported by:
- OpenAI
- Cohere
extra_body
instance-attribute
extra_body: object
Extra body to send to the model.
Supported by:
- OpenAI
- Cohere
merge_embedding_settings
merge_embedding_settings(
base: EmbeddingSettings | None,
overrides: EmbeddingSettings | None,
) -> EmbeddingSettings | None
Merge two sets of embedding settings, preferring the overrides.
A common use case is: merge_embedding_settings(
Source code in pydantic_ai_slim/pydantic_ai/embeddings/settings.py
39 40 41 42 43 44 45 46 47 48 49 50 | |
WrapperEmbeddingModel
dataclass
Bases: EmbeddingModel
Embedding model which wraps another embedding model.
Does nothing on its own, used as a base class.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/wrapper.py
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 | |
wrapped
instance-attribute
wrapped: EmbeddingModel = (
infer_embedding_model(wrapped)
if isinstance(wrapped, str)
else wrapped
)
The underlying embedding model being wrapped.
settings
property
settings: EmbeddingSettings | None
Get the settings from the wrapped embedding model.
KnownEmbeddingModelName
module-attribute
KnownEmbeddingModelName = TypeAliasType(
"KnownEmbeddingModelName",
Literal[
"openai:text-embedding-ada-002",
"openai:text-embedding-3-small",
"openai:text-embedding-3-large",
"cohere:embed-v4.0",
"cohere:embed-english-v3.0",
"cohere:embed-english-light-v3.0",
"cohere:embed-multilingual-v3.0",
"cohere:embed-multilingual-light-v3.0",
],
)
Known model names that can be used with the model parameter of Embedder.
KnownEmbeddingModelName is provided as a concise way to specify an embedding model.
infer_embedding_model
infer_embedding_model(
model: EmbeddingModel | KnownEmbeddingModelName | str,
*,
provider_factory: Callable[
[str], Provider[Any]
] = infer_provider
) -> EmbeddingModel
Infer the model from the name.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/__init__.py
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 | |
Embedder
dataclass
TODO: Docstring.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/__init__.py
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
__init__
__init__(
model: EmbeddingModel | KnownEmbeddingModelName | str,
*,
settings: EmbeddingSettings | None = None,
defer_model_check: bool = True,
instrument: InstrumentationSettings | bool | None = None
) -> None
Initialize an Embedder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
EmbeddingModel | KnownEmbeddingModelName | str
|
The embedding model to use - can be a model instance, model name, or string. |
required |
settings
|
EmbeddingSettings | None
|
Optional embedding settings to use as defaults. |
None
|
defer_model_check
|
bool
|
Whether to defer model validation until first use. |
True
|
instrument
|
InstrumentationSettings | bool | None
|
OpenTelemetry instrumentation settings. Set to |
None
|
Source code in pydantic_ai_slim/pydantic_ai/embeddings/__init__.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
instrument
instance-attribute
instrument: InstrumentationSettings | bool | None = (
instrument
)
Options to automatically instrument with OpenTelemetry.
Set to True to use default instrumentation settings, which will use Logfire if it's configured.
Set to an instance of InstrumentationSettings to customize.
If this isn't set, then the last value set by
Embedder.instrument_all()
will be used, which defaults to False.
See the Debugging and Monitoring guide for more info.
instrument_all
staticmethod
instrument_all(
instrument: InstrumentationSettings | bool = True,
) -> None
Set the instrumentation options for all embedders where instrument is not set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instrument
|
InstrumentationSettings | bool
|
Instrumentation settings to use as the default. Set to |
True
|
Source code in pydantic_ai_slim/pydantic_ai/embeddings/__init__.py
142 143 144 145 146 147 148 149 150 | |
OpenAIEmbeddingModelName
module-attribute
OpenAIEmbeddingModelName = str | EmbeddingModel
Possible OpenAI embeddings model names.
OpenAIEmbeddingSettings
Bases: EmbeddingSettings
Settings used for an OpenAI embedding model request.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/openai.py
32 33 | |
OpenAIEmbeddingModel
dataclass
Bases: EmbeddingModel
OpenAI embedding model.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/openai.py
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 | |
__init__
__init__(
model_name: OpenAIEmbeddingModelName,
*,
provider: (
OpenAIEmbeddingsCompatibleProvider
| Literal["openai"]
| Provider[AsyncOpenAI]
) = "openai",
settings: EmbeddingSettings | None = None
)
Initialize an OpenAI embedding model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
OpenAIEmbeddingModelName
|
The name of the OpenAI model to use. List of model names available here. |
required |
provider
|
OpenAIEmbeddingsCompatibleProvider | Literal['openai'] | Provider[AsyncOpenAI]
|
The provider to use for authentication and API access. Can be either the string
'openai' or an instance of |
'openai'
|
settings
|
EmbeddingSettings | None
|
Model-specific settings that will be used as defaults for this model. |
None
|
Source code in pydantic_ai_slim/pydantic_ai/embeddings/openai.py
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 | |
LatestCohereEmbeddingModelNames
module-attribute
LatestCohereEmbeddingModelNames = Literal[
"embed-v4.0",
"embed-english-v3.0",
"embed-english-light-v3.0",
"embed-multilingual-v3.0",
"embed-multilingual-light-v3.0",
]
Latest Cohere embeddings models.
CohereEmbeddingModelName
module-attribute
CohereEmbeddingModelName = (
str | LatestCohereEmbeddingModelNames
)
Possible Cohere embeddings model names.
CohereEmbeddingSettings
Bases: EmbeddingSettings
Settings used for a Cohere embedding model request.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/cohere.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
cohere_max_tokens
instance-attribute
cohere_max_tokens: int
The maximum number of tokens to generate before stopping.
cohere_input_type
instance-attribute
cohere_input_type: EmbedInputType
The input type to use for the embedding model. Overrides the input_type argument which only takes query and document.
cohere_truncate
instance-attribute
cohere_truncate: V2EmbedRequestTruncate
The truncation strategy to use for the embedding model:
NONE(default): Do not truncate the input text and raise an error if the input text is too long.END: Truncate the input text to the maximum number of tokens.START: Truncate the start of the input text.
CohereEmbeddingModel
dataclass
Bases: EmbeddingModel
Cohere embedding model.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/cohere.py
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
__init__
__init__(
model_name: CohereEmbeddingModelName,
*,
provider: (
Literal["cohere"] | Provider[AsyncClientV2]
) = "cohere",
settings: EmbeddingSettings | None = None
)
Initialize an Cohere model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
CohereEmbeddingModelName
|
The name of the Cohere model to use. List of model names available here. |
required |
provider
|
Literal['cohere'] | Provider[AsyncClientV2]
|
The provider to use for authentication and API access. Can be either the string
'cohere' or an instance of |
'cohere'
|
settings
|
EmbeddingSettings | None
|
Model-specific settings that will be used as defaults for this model. |
None
|
Source code in pydantic_ai_slim/pydantic_ai/embeddings/cohere.py
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 | |
SentenceTransformersEmbeddingSettings
Bases: EmbeddingSettings
Settings used for a Sentence-Transformers embedding model request.
All fields are sentence_transformers_-prefixed so settings can be merged across providers safely.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/sentence_transformers.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
sentence_transformers_device
instance-attribute
sentence_transformers_device: str
Device to run inference on, e.g. "cpu", "cuda", "cuda:0", "mps".
sentence_transformers_normalize_embeddings
instance-attribute
sentence_transformers_normalize_embeddings: bool
Whether to L2-normalize embeddings. Mirrors normalize_embeddings in SentenceTransformer.encode.
sentence_transformers_batch_size
instance-attribute
sentence_transformers_batch_size: int
Batch size to use during encoding.
SentenceTransformerEmbeddingModel
dataclass
Bases: EmbeddingModel
Local embeddings using sentence-transformers models.
Example models include "all-MiniLM-L6-v2" and many others hosted on Hugging Face.
Source code in pydantic_ai_slim/pydantic_ai/embeddings/sentence_transformers.py
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 | |
__init__
__init__(
model: SentenceTransformer | str,
*,
settings: EmbeddingSettings | None = None
) -> None
Initialize a Sentence-Transformers embedding model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
SentenceTransformer | str
|
The model name or local path to load with |
required |
settings
|
EmbeddingSettings | None
|
Model-specific settings that will be used as defaults for this model. |
None
|
Source code in pydantic_ai_slim/pydantic_ai/embeddings/sentence_transformers.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |