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)[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 to 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 set project or space ID. If passed, credentials and project_id/space_id are not required.

  • verify (bool or str, optional) –

    user can pass as verify one of following:

    • the path to a CA_BUNDLE file

    • the path of directory with certificates of trusted CAs

    • True - default path to truststore will be taken

    • False - no verification will be made

Note

One of these parameters is required: [project_id, space_id] when credentials parameter passed.

Hint

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

Example

 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={
         "apikey": "***",
         "url": "https://us-south.ml.cloud.ibm.com"
     },
     project_id="*****"
     )
embed_documents(texts, params=None, concurrency_limit=10)[source]#

Return 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) – meta props for 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 that will be sent in parallel, max is 10, defaults to 10

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]#

Return embedding vector for provided text.

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

  • params (ParamsType | None, optional) – meta props for 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=10)[source]#

Generates embeddings vectors for the given input with the given parameters and returns a REST API response.

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

  • params (ParamsType | None, optional) – meta props for 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 that will be sent in parallel, max is 10, defaults to 10

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 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}