Embeddings

Embeddings

class ibm_watsonx_ai.foundation_models.embeddings.Embeddings(*, model_id, params=None, credentials=None, project_id=None, space_id=None, api_client=None, verify=None, persistent_connection=True, batch_size=1000)[source]

Bases: BaseEmbeddings, WMLResource

Instantiate the embeddings service.

Parameters:
  • model_id (str, optional) – the type of model to use

  • params (dict, optional) – parameters to use during generate requests, use ibm_watsonx_ai.metanames.EmbedTextParamsMetaNames().show() to view the list of MetaNames

  • credentials (dict, optional) – credentials for the Watson Machine Learning instance

  • project_id (str, optional) – ID of the Watson Studio project

  • space_id (str, optional) – ID of the Watson Studio space

  • api_client (APIClient, optional) – initialized APIClient object with a set project ID or space ID. If passed, credentials and project_id/space_id are not required.

  • verify (bool or str, optional) –

    You can pass one of following as verify:

    • the path to a CA_BUNDLE file

    • the path of a directory with certificates of trusted CAs

    • True - default path to truststore will be taken

    • False - no verification will be made

  • persistent_connection (bool, optional) – defines whether to keep a persistent connection when evaluating the generate, ‘embed_query’, and ‘embed_documents` methods with one prompt or batch of prompts that meet the length limit. For more details, see Generate embeddings. To close the connection, run embeddings.close_persistent_connection(), defaults to True. Added in 1.1.2.

  • batch_size (int, optional) – Number of elements to be embedded sending in one call, defaults to 1000

Note

When the credentials parameter is passed, one of these parameters is required: [project_id, space_id].

Hint

You can copy the project_id from the Project’s Manage tab (Project -> Manage -> General -> Details).

Example:

 from ibm_watsonx_ai import Credentials
 from ibm_watsonx_ai.foundation_models import Embeddings
 from ibm_watsonx_ai.metanames import EmbedTextParamsMetaNames as EmbedParams
 from ibm_watsonx_ai.foundation_models.utils.enums import EmbeddingTypes

embed_params = {
     EmbedParams.TRUNCATE_INPUT_TOKENS: 3,
     EmbedParams.RETURN_OPTIONS: {
     'input_text': True
     }
 }

 embedding = Embeddings(
     model_id=EmbeddingTypes.IBM_SLATE_30M_ENG,
     params=embed_params,
     credentials=Credentials(
         api_key = "***",
         url = "https://us-south.ml.cloud.ibm.com"),
     project_id="*****"
     )
close_persistent_connection()[source]

Only applicable if persistent_connection was set to True in Embeddings initialization.

embed_documents(texts, params=None, concurrency_limit=5)[source]

Returns list of embedding vectors for provided texts.

Parameters:
  • texts (list[str]) – list of texts for which embedding vectors will be generated

  • params (ParamsType | None, optional) – MetaProps for the embedding generation, use ibm_watsonx_ai.metanames.EmbedTextParamsMetaNames().show() to view the list of MetaNames, defaults to None

  • concurrency_limit (int, optional) – number of requests to be sent in parallel, max is 10, defaults to 5

Returns:

list of embedding vectors

Return type:

list[list[float]]

Example:

q = [
    "What is a Generative AI?",
    "Generative AI refers to a type of artificial intelligence that can original content."
    ]

embedding_vectors = embedding.embed_documents(texts=q)
print(embedding_vectors)
embed_query(text, params=None)[source]

Returns an embedding vector for a provided text.

Parameters:
  • text (str) – text for which embedding vector will be generated

  • params (ParamsType | None, optional) – MetaProps for the embedding generation, use ibm_watsonx_ai.metanames.EmbedTextParamsMetaNames().show() to view the list of MetaNames, defaults to None

Returns:

embedding vector

Return type:

list[float]

Example:

q = "What is a Generative AI?"
embedding_vector = embedding.embed_query(text=q)
print(embedding_vector)
generate(inputs, params=None, concurrency_limit=5)[source]

Generate embeddings vectors for the given input with the given parameters. Returns a REST API response.

Parameters:
  • inputs (list[str]) – list of texts for which embedding vectors will be generated

  • params (ParamsType | None, optional) – MetaProps for the embedding generation, use ibm_watsonx_ai.metanames.EmbedTextParamsMetaNames().show() to view the list of MetaNames, defaults to None

  • concurrency_limit (int, optional) – number of requests to be sent in parallel, max is 10, defaults to 5

Returns:

scoring results containing generated embeddings vectors

Return type:

dict

to_dict()[source]

Serialize Embeddings.

Returns:

serializes this Embeddings so that it can be reconstructed by from_dict class method.

Return type:

dict

BaseEmbeddings

class ibm_watsonx_ai.foundation_models.embeddings.base_embeddings.BaseEmbeddings[source]

Bases: ABC

LangChain-like embedding function interface.

abstract embed_documents(texts)[source]

Embed search docs.

abstract embed_query(text)[source]

Embed query text.

classmethod from_dict(data)[source]

Deserialize BaseEmbeddings into a concrete one using arguments.

Returns:

concrete Embeddings or None if data is incorrect

Return type:

BaseEmbeddings | None

to_dict()[source]

Serialize Embeddings.

Returns:

serializes this Embeddings so that it can be reconstructed by from_dict class method.

Return type:

dict

Enums

class metanames.EmbedTextParamsMetaNames[source]

Set of MetaNames for Foundation Model Embeddings Parameters.

Available MetaNames:

MetaName

Type

Required

Example value

TRUNCATE_INPUT_TOKENS

int

N

2

RETURN_OPTIONS

dict

N

{'input_text': True}

class EmbeddingModels

Bases: StrEnum

This represents a dynamically generated Enum for Embedding Models.

Example of getting EmbeddingModels

# GET EmbeddingModels ENUM
client.foundation_models.EmbeddingModels

# PRINT dict of Enums
client.foundation_models.EmbeddingModels.show()

Example Output:

{'SLATE_125M_ENGLISH_RTRVR': 'ibm/slate-125m-english-rtrvr',
...
'SLATE_30M_ENGLISH_RTRVR': 'ibm/slate-30m-english-rtrvr'}

Example of initialising Embeddings with EmbeddingModels Enum:

from ibm_watsonx_ai.foundation_models import Embeddings

embeddings = Embeddings(
    model_id=client.foundation_models.EmbeddingModels.SLATE_30M_ENGLISH_RTRVR,
    credentials=Credentials(...),
    project_id=project_id,
)
class ibm_watsonx_ai.foundation_models.utils.enums.EmbeddingTypes(value)[source]

Bases: Enum

Deprecated since version 1.0.5: Use EmbeddingModels() instead.

Supported embedding models.

Note

You can check the current list of supported embeddings model types of various environments with get_embeddings_model_specs() or by referring to the watsonx.ai documentation.