Embeddings#

Users have a few options to choose from when it comes to embeddings.

  • OpenAIEmbedding: the default embedding class. Defaults to “text-embedding-ada-002”

  • HuggingFaceEmbedding: a generic wrapper around HuggingFace’s transformers models.

  • OptimumEmbedding: support for usage and creation of ONNX models from Optimum and HuggingFace.

  • InstructorEmbedding: a wrapper around Instructor embedding models.

  • LangchainEmbedding: a wrapper around Langchain’s embedding models.

  • GoogleUnivSentEncoderEmbedding: a wrapper around Google’s Universal Sentence Encoder.

  • AdapterEmbeddingModel: an adapter around any embedding model.

OpenAIEmbedding#

pydantic model llama_index.embeddings.openai.OpenAIEmbedding#

OpenAI class for embeddings.

Parameters
  • mode (str) –

    Mode for embedding. Defaults to OpenAIEmbeddingMode.TEXT_SEARCH_MODE. Options are:

    • OpenAIEmbeddingMode.SIMILARITY_MODE

    • OpenAIEmbeddingMode.TEXT_SEARCH_MODE

  • model (str) –

    Model for embedding. Defaults to OpenAIEmbeddingModelType.TEXT_EMBED_ADA_002. Options are:

    • OpenAIEmbeddingModelType.DAVINCI

    • OpenAIEmbeddingModelType.CURIE

    • OpenAIEmbeddingModelType.BABBAGE

    • OpenAIEmbeddingModelType.ADA

    • OpenAIEmbeddingModelType.TEXT_EMBED_ADA_002

Show JSON schema
{
   "title": "OpenAIEmbedding",
   "description": "OpenAI class for embeddings.\n\nArgs:\n    mode (str): Mode for embedding.\n        Defaults to OpenAIEmbeddingMode.TEXT_SEARCH_MODE.\n        Options are:\n\n        - OpenAIEmbeddingMode.SIMILARITY_MODE\n        - OpenAIEmbeddingMode.TEXT_SEARCH_MODE\n\n    model (str): Model for embedding.\n        Defaults to OpenAIEmbeddingModelType.TEXT_EMBED_ADA_002.\n        Options are:\n\n        - OpenAIEmbeddingModelType.DAVINCI\n        - OpenAIEmbeddingModelType.CURIE\n        - OpenAIEmbeddingModelType.BABBAGE\n        - OpenAIEmbeddingModelType.ADA\n        - OpenAIEmbeddingModelType.TEXT_EMBED_ADA_002",
   "type": "object",
   "properties": {
      "model_name": {
         "title": "Model Name",
         "description": "The name of the embedding model.",
         "default": "unknown",
         "type": "string"
      },
      "embed_batch_size": {
         "title": "Embed Batch Size",
         "description": "The batch size for embedding calls.",
         "default": 10,
         "exclusiveMinimum": 0,
         "lte": 2048,
         "type": "integer"
      },
      "callback_manager": {
         "title": "Callback Manager"
      },
      "additional_kwargs": {
         "title": "Additional Kwargs",
         "description": "Additional kwargs for the OpenAI API.",
         "type": "object"
      },
      "api_key": {
         "title": "Api Key",
         "description": "The OpenAI API key.",
         "type": "string"
      },
      "api_base": {
         "title": "Api Base",
         "description": "The base URL for OpenAI API.",
         "type": "string"
      },
      "api_version": {
         "title": "Api Version",
         "description": "The version for OpenAI API.",
         "type": "string"
      },
      "max_retries": {
         "title": "Max Retries",
         "description": "Maximum number of retries.",
         "default": 10,
         "gte": 0,
         "type": "integer"
      },
      "timeout": {
         "title": "Timeout",
         "description": "Timeout for each request.",
         "default": 60.0,
         "gte": 0,
         "type": "number"
      },
      "default_headers": {
         "title": "Default Headers",
         "description": "The default headers for API requests.",
         "type": "object",
         "additionalProperties": {
            "type": "string"
         }
      },
      "reuse_client": {
         "title": "Reuse Client",
         "description": "Reuse the OpenAI client between requests. When doing anything with large volumes of async API calls, setting this to false can improve stability.",
         "default": true,
         "type": "boolean"
      },
      "dimensions": {
         "title": "Dimensions",
         "description": "The number of dimensions on the output embedding vectors. Works only with v3 embedding models.",
         "type": "integer"
      },
      "class_name": {
         "title": "Class Name",
         "type": "string",
         "default": "OpenAIEmbedding"
      }
   },
   "required": [
      "api_key",
      "api_base",
      "api_version"
   ]
}

Config
  • arbitrary_types_allowed: bool = True

Fields
Validators
  • _validate_callback_manager » callback_manager

field additional_kwargs: Dict[str, Any] [Optional]#

Additional kwargs for the OpenAI API.

field api_base: str [Required]#

The base URL for OpenAI API.

field api_key: str [Required]#

The OpenAI API key.

field api_version: str [Required]#

The version for OpenAI API.

field default_headers: Optional[Dict[str, str]] = None#

The default headers for API requests.

field dimensions: Optional[int] = None#

The number of dimensions on the output embedding vectors. Works only with v3 embedding models.

field max_retries: int = 10#

Maximum number of retries.

field reuse_client: bool = True#

Reuse the OpenAI client between requests. When doing anything with large volumes of async API calls, setting this to false can improve stability.

field timeout: float = 60.0#

Timeout for each request.

classmethod class_name() str#

Get the class name, used as a unique ID in serialization.

This provides a key that makes serialization robust against actual class name changes.

HuggingFaceEmbedding#

pydantic model llama_index.embeddings.huggingface.HuggingFaceEmbedding#

Show JSON schema
{
   "title": "HuggingFaceEmbedding",
   "description": "Base class for embeddings.",
   "type": "object",
   "properties": {
      "model_name": {
         "title": "Model Name",
         "description": "The name of the embedding model.",
         "default": "unknown",
         "type": "string"
      },
      "embed_batch_size": {
         "title": "Embed Batch Size",
         "description": "The batch size for embedding calls.",
         "default": 10,
         "exclusiveMinimum": 0,
         "lte": 2048,
         "type": "integer"
      },
      "callback_manager": {
         "title": "Callback Manager"
      },
      "tokenizer_name": {
         "title": "Tokenizer Name",
         "description": "Tokenizer name from HuggingFace.",
         "type": "string"
      },
      "max_length": {
         "title": "Max Length",
         "description": "Maximum length of input.",
         "default": 512,
         "exclusiveMinimum": 0,
         "type": "integer"
      },
      "pooling": {
         "description": "Pooling strategy.",
         "allOf": [
            {
               "$ref": "#/definitions/Pooling"
            }
         ]
      },
      "normalize": {
         "title": "Normalize",
         "description": "Normalize embeddings or not.",
         "default": true,
         "type": "boolean"
      },
      "query_instruction": {
         "title": "Query Instruction",
         "description": "Instruction to prepend to query text.",
         "type": "string"
      },
      "text_instruction": {
         "title": "Text Instruction",
         "description": "Instruction to prepend to text.",
         "type": "string"
      },
      "cache_folder": {
         "title": "Cache Folder",
         "description": "Cache folder for huggingface files.",
         "type": "string"
      },
      "class_name": {
         "title": "Class Name",
         "type": "string",
         "default": "HuggingFaceEmbedding"
      }
   },
   "required": [
      "tokenizer_name"
   ],
   "definitions": {
      "Pooling": {
         "title": "Pooling",
         "description": "Enum of possible pooling choices with pooling behaviors.",
         "enum": [
            "cls",
            "mean"
         ],
         "type": "string"
      }
   }
}

Config
  • arbitrary_types_allowed: bool = True

Fields
Validators
  • _validate_callback_manager » callback_manager

field cache_folder: Optional[str] = None#

Cache folder for huggingface files.

field max_length: int = 512#

Maximum length of input.

Constraints
  • exclusiveMinimum = 0

field normalize: bool = True#

Normalize embeddings or not.

field pooling: Pooling = None#

Pooling strategy.

field query_instruction: Optional[str] = None#

Instruction to prepend to query text.

field text_instruction: Optional[str] = None#

Instruction to prepend to text.

field tokenizer_name: str [Required]#

Tokenizer name from HuggingFace.

classmethod class_name() str#

Get the class name, used as a unique ID in serialization.

This provides a key that makes serialization robust against actual class name changes.

OptimumEmbedding#

pydantic model llama_index.embeddings.huggingface_optimum.OptimumEmbedding#

Show JSON schema
{
   "title": "OptimumEmbedding",
   "description": "Base class for embeddings.",
   "type": "object",
   "properties": {
      "model_name": {
         "title": "Model Name",
         "description": "The name of the embedding model.",
         "default": "unknown",
         "type": "string"
      },
      "embed_batch_size": {
         "title": "Embed Batch Size",
         "description": "The batch size for embedding calls.",
         "default": 10,
         "exclusiveMinimum": 0,
         "lte": 2048,
         "type": "integer"
      },
      "callback_manager": {
         "title": "Callback Manager"
      },
      "folder_name": {
         "title": "Folder Name",
         "description": "Folder name to load from.",
         "type": "string"
      },
      "max_length": {
         "title": "Max Length",
         "description": "Maximum length of input.",
         "type": "integer"
      },
      "pooling": {
         "title": "Pooling",
         "description": "Pooling strategy. One of ['cls', 'mean'].",
         "type": "string"
      },
      "normalize": {
         "title": "Normalize",
         "description": "Normalize embeddings or not.",
         "default": true,
         "type": "string"
      },
      "query_instruction": {
         "title": "Query Instruction",
         "description": "Instruction to prepend to query text.",
         "type": "string"
      },
      "text_instruction": {
         "title": "Text Instruction",
         "description": "Instruction to prepend to text.",
         "type": "string"
      },
      "cache_folder": {
         "title": "Cache Folder",
         "description": "Cache folder for huggingface files.",
         "type": "string"
      },
      "class_name": {
         "title": "Class Name",
         "type": "string",
         "default": "OptimumEmbedding"
      }
   },
   "required": [
      "folder_name",
      "max_length",
      "pooling"
   ]
}

Config
  • arbitrary_types_allowed: bool = True

Fields
Validators
  • _validate_callback_manager » callback_manager

field cache_folder: Optional[str] = None#

Cache folder for huggingface files.

field folder_name: str [Required]#

Folder name to load from.

field max_length: int [Required]#

Maximum length of input.

field normalize: str = True#

Normalize embeddings or not.

field pooling: str [Required]#

Pooling strategy. One of [‘cls’, ‘mean’].

field query_instruction: Optional[str] = None#

Instruction to prepend to query text.

field text_instruction: Optional[str] = None#

Instruction to prepend to text.

classmethod class_name() str#

Get the class name, used as a unique ID in serialization.

This provides a key that makes serialization robust against actual class name changes.

classmethod create_and_save_optimum_model(model_name_or_path: str, output_path: str, export_kwargs: Optional[dict] = None) None#

InstructorEmbedding#

pydantic model llama_index.embeddings.instructor.InstructorEmbedding#

Show JSON schema
{
   "title": "InstructorEmbedding",
   "description": "Base class for embeddings.",
   "type": "object",
   "properties": {
      "model_name": {
         "title": "Model Name",
         "description": "The name of the embedding model.",
         "default": "unknown",
         "type": "string"
      },
      "embed_batch_size": {
         "title": "Embed Batch Size",
         "description": "The batch size for embedding calls.",
         "default": 10,
         "exclusiveMinimum": 0,
         "lte": 2048,
         "type": "integer"
      },
      "callback_manager": {
         "title": "Callback Manager"
      },
      "query_instruction": {
         "title": "Query Instruction",
         "description": "Instruction to prepend to query text.",
         "type": "string"
      },
      "text_instruction": {
         "title": "Text Instruction",
         "description": "Instruction to prepend to text.",
         "type": "string"
      },
      "cache_folder": {
         "title": "Cache Folder",
         "description": "Cache folder for huggingface files.",
         "type": "string"
      },
      "class_name": {
         "title": "Class Name",
         "type": "string",
         "default": "InstructorEmbedding"
      }
   }
}

Config
  • arbitrary_types_allowed: bool = True

Fields
Validators
  • _validate_callback_manager » callback_manager

field cache_folder: Optional[str] = None#

Cache folder for huggingface files.

field query_instruction: Optional[str] = None#

Instruction to prepend to query text.

field text_instruction: Optional[str] = None#

Instruction to prepend to text.

classmethod class_name() str#

Get the class name, used as a unique ID in serialization.

This provides a key that makes serialization robust against actual class name changes.

LangchainEmbedding#

pydantic model llama_index.embeddings.langchain.LangchainEmbedding#

External embeddings (taken from Langchain).

Parameters

langchain_embedding (langchain.embeddings.Embeddings) – Langchain embeddings class.

Show JSON schema
{
   "title": "LangchainEmbedding",
   "description": "External embeddings (taken from Langchain).\n\nArgs:\n    langchain_embedding (langchain.embeddings.Embeddings): Langchain\n        embeddings class.",
   "type": "object",
   "properties": {
      "model_name": {
         "title": "Model Name",
         "description": "The name of the embedding model.",
         "default": "unknown",
         "type": "string"
      },
      "embed_batch_size": {
         "title": "Embed Batch Size",
         "description": "The batch size for embedding calls.",
         "default": 10,
         "exclusiveMinimum": 0,
         "lte": 2048,
         "type": "integer"
      },
      "callback_manager": {
         "title": "Callback Manager"
      },
      "class_name": {
         "title": "Class Name",
         "type": "string",
         "default": "LangchainEmbedding"
      }
   }
}

Config
  • arbitrary_types_allowed: bool = True

Fields

Validators
  • _validate_callback_manager » callback_manager

classmethod class_name() str#

Get the class name, used as a unique ID in serialization.

This provides a key that makes serialization robust against actual class name changes.

GoogleUnivSentEncoderEmbedding#

pydantic model llama_index.embeddings.google.GoogleUnivSentEncoderEmbedding#

Show JSON schema
{
   "title": "GoogleUnivSentEncoderEmbedding",
   "description": "Base class for embeddings.",
   "type": "object",
   "properties": {
      "model_name": {
         "title": "Model Name",
         "description": "The name of the embedding model.",
         "default": "unknown",
         "type": "string"
      },
      "embed_batch_size": {
         "title": "Embed Batch Size",
         "description": "The batch size for embedding calls.",
         "default": 10,
         "exclusiveMinimum": 0,
         "lte": 2048,
         "type": "integer"
      },
      "callback_manager": {
         "title": "Callback Manager"
      },
      "class_name": {
         "title": "Class Name",
         "type": "string",
         "default": "GoogleUnivSentEncoderEmbedding"
      }
   }
}

Config
  • arbitrary_types_allowed: bool = True

Fields

Validators
  • _validate_callback_manager » callback_manager

classmethod class_name() str#

Get the class name, used as a unique ID in serialization.

This provides a key that makes serialization robust against actual class name changes.