Home / Function/ init_embeddings() — langchain Function Reference

init_embeddings() — langchain Function Reference

Architecture documentation for the init_embeddings() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  2954192c_8016_8ddf_9323_43285db4aa1b["init_embeddings()"]
  76380b72_77fe_25f0_4fc9_0d116ee2433c["base.py"]
  2954192c_8016_8ddf_9323_43285db4aa1b -->|defined in| 76380b72_77fe_25f0_4fc9_0d116ee2433c
  e9cf050d_cf9c_221c_0569_7b52c7831196["_infer_model_and_provider()"]
  2954192c_8016_8ddf_9323_43285db4aa1b -->|calls| e9cf050d_cf9c_221c_0569_7b52c7831196
  a5eb1357_0d90_510f_8e72_385214be9195["_check_pkg()"]
  2954192c_8016_8ddf_9323_43285db4aa1b -->|calls| a5eb1357_0d90_510f_8e72_385214be9195
  80a66df6_2383_888c_83e7_83324720ae54["_get_provider_list()"]
  2954192c_8016_8ddf_9323_43285db4aa1b -->|calls| 80a66df6_2383_888c_83e7_83324720ae54
  style 2954192c_8016_8ddf_9323_43285db4aa1b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/embeddings/base.py lines 128–245

def init_embeddings(
    model: str,
    *,
    provider: str | None = None,
    **kwargs: Any,
) -> Embeddings | Runnable[Any, list[float]]:
    """Initialize an embeddings model from a model name and optional provider.

    !!! note
        Must have the integration package corresponding to the model provider
        installed.

    Args:
        model: Name of the model to use.

            Can be either:

            - A model string like `"openai:text-embedding-3-small"`
            - Just the model name if the provider is specified separately or can be
                inferred.

            See supported providers under the `provider` arg description.
        provider: Optional explicit provider name. If not specified, will attempt to
            parse from the model string in the `model` arg.

            Supported providers:

            - `openai`                  -> [`langchain-openai`](https://docs.langchain.com/oss/python/integrations/providers/openai)
            - `azure_openai`            -> [`langchain-openai`](https://docs.langchain.com/oss/python/integrations/providers/openai)
            - `bedrock`                 -> [`langchain-aws`](https://docs.langchain.com/oss/python/integrations/providers/aws)
            - `cohere`                  -> [`langchain-cohere`](https://docs.langchain.com/oss/python/integrations/providers/cohere)
            - `google_genai`            -> [`langchain-google-genai`](https://docs.langchain.com/oss/python/integrations/providers/google)
            - `google_vertexai`         -> [`langchain-google-vertexai`](https://docs.langchain.com/oss/python/integrations/providers/google)
            - `huggingface`             -> [`langchain-huggingface`](https://docs.langchain.com/oss/python/integrations/providers/huggingface)
            - `mistralai`               -> [`langchain-mistralai`](https://docs.langchain.com/oss/python/integrations/providers/mistralai)
            - `ollama`                  -> [`langchain-ollama`](https://docs.langchain.com/oss/python/integrations/providers/ollama)

        **kwargs: Additional model-specific parameters passed to the embedding model.
            These vary by provider, see the provider-specific documentation for details.

    Returns:
        An `Embeddings` instance that can generate embeddings for text.

    Raises:
        ValueError: If the model provider is not supported or cannot be determined
        ImportError: If the required provider package is not installed

    ???+ note "Example Usage"

        ```python
        # Using a model string
        model = init_embeddings("openai:text-embedding-3-small")
        model.embed_query("Hello, world!")

        # Using explicit provider
        model = init_embeddings(model="text-embedding-3-small", provider="openai")
        model.embed_documents(["Hello, world!", "Goodbye, world!"])

        # With additional parameters
        model = init_embeddings("openai:text-embedding-3-small", api_key="sk-...")
        ```

    !!! version-added "Added in `langchain` 0.3.9"

    """
    if not model:
        providers = _SUPPORTED_PROVIDERS.keys()
        msg = (
            f"Must specify model name. Supported providers are: {', '.join(providers)}"
        )
        raise ValueError(msg)

    provider, model_name = _infer_model_and_provider(model, provider=provider)
    pkg = _SUPPORTED_PROVIDERS[provider]
    _check_pkg(pkg)

    if provider == "openai":
        from langchain_openai import OpenAIEmbeddings

        return OpenAIEmbeddings(model=model_name, **kwargs)
    if provider == "azure_openai":

Domain

Subdomains

Frequently Asked Questions

What does init_embeddings() do?
init_embeddings() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/embeddings/base.py.
Where is init_embeddings() defined?
init_embeddings() is defined in libs/langchain/langchain_classic/embeddings/base.py at line 128.
What does init_embeddings() call?
init_embeddings() calls 3 function(s): _check_pkg, _get_provider_list, _infer_model_and_provider.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free