Embedding
Create text embeddings with any provider
The embedding and aembedding functions create vector embeddings from text using a unified interface across all providers that support embeddings.
any_llm.embedding()
def embedding(
model: str,
inputs: str | list[str],
*,
provider: str | LLMProvider | None = None,
api_key: str | None = None,
api_base: str | None = None,
client_args: dict[str, Any] | None = None,
**kwargs: Any,
) -> CreateEmbeddingResponseany_llm.aembedding()
Async variant with the same parameters.
async def aembedding(
model: str,
inputs: str | list[str],
*,
provider: str | LLMProvider | None = None,
api_key: str | None = None,
api_base: str | None = None,
client_args: dict[str, Any] | None = None,
**kwargs: Any,
) -> CreateEmbeddingResponseParameters
model
str
required
Model identifier. Recommended: Use with separate provider parameter (e.g., model='gpt-4', provider='openai'). Alternative: Combined format 'provider:model' (e.g., 'openai:gpt-4'). Legacy format 'provider/model' is also supported but deprecated.
inputs
str | list[str]
required
The input text to embed
provider
str | LLMProvider | None
None
Recommended: Provider name to use for the request (e.g., 'openai', 'mistral'). When provided, the model parameter should contain only the model name.
api_key
str | None
None
API key for the provider
api_base
str | None
None
Base URL for the provider API
client_args
dict[str, Any] | None
None
Additional provider-specific arguments that will be passed to the provider's client instantiation.
**kwargs
Any
required
Additional provider-specific arguments that will be passed to the provider's API call.
Return Value
Returns a CreateEmbeddingResponse containing:
data-- list ofEmbeddingobjects, each with anembeddingvector (list[float]) and anindex.model-- the model used.usage-- token usage information withprompt_tokensandtotal_tokens.
Usage
Single text
Batch embedding
Async
Last updated