Encoders#

class flexrag.models.EncoderBase(cfg)[源代码]#
async async_encode(texts)[源代码]#

The async version of encode.

encode(texts)[源代码]#

Encode the given texts into embeddings.

参数:

texts (list[str] | str) -- A batch of texts.

返回:

A batch of embeddings.

返回类型:

np.ndarray

encode_batch(texts, batch_size=None)[源代码]#

Encode the given texts into embeddings in batches.

参数:
  • texts (list[str] | str) -- A batch of texts.

  • batch_size (int) -- The size of each batch. Defaults to self.batch_size.

返回:

A batch of embeddings.

返回类型:

np.ndarray

class flexrag.models.EncoderConfig(encoder_type=None, cohere_config=<factory>, hf_config=<factory>, hf_clip_config=<factory>, jina_config=<factory>, ollama_config=<factory>, openai_config=<factory>, sentence_transformer_config=<factory>)#

Configuration class for encoder (name: EncoderConfig, default: None).

参数:

Local Encoders#

class flexrag.models.HFEncoderConfig(batch_size=32, log_interval=1000, model_path=None, tokenizer_path=None, trust_remote_code=False, device_id=<factory>, load_dtype='auto', max_encode_length=512, encode_method='mean', normalize=False, prompt='', task='')[源代码]#

Configuration for HFEncoder.

参数:
  • max_encode_length (int) -- The maximum length of the input sequence. Default is 512.

  • encode_method (str) -- The method to get the embedding. Default is "mean". Available choices are "cls", "mean".

  • normalize (bool) -- Whether to normalize the embedding. Default is False.

  • prompt (str) -- The prefix to use. Default is "".

  • task (str) -- The task to use. Default is "".

dump(path)#

Dump the dataclass to a YAML file.

dumps()#

Dump the dataclass to a YAML string.

classmethod load(path)#

Load the dataclass from a YAML file.

classmethod loads(s)#

Load the dataclass from a YAML string.

class flexrag.models.HFEncoder(cfg)[源代码]#

基类:EncoderBase

class flexrag.models.HFClipEncoderConfig(batch_size=32, log_interval=1000, model_path=None, tokenizer_path=None, trust_remote_code=False, device_id=<factory>, load_dtype='auto', max_encode_length=512, normalize=False, convert_to_rgb=False)[源代码]#

Configuration for HFClipEncoder.

参数:
  • max_encode_length (int) -- The maximum length of the input sequence. Default is 512.

  • normalize (bool) -- Whether to normalize the embedding. Default is False.

  • convert_to_rgb (bool) -- Whether to convert the image to RGB. Default is False.

dump(path)#

Dump the dataclass to a YAML file.

dumps()#

Dump the dataclass to a YAML string.

classmethod load(path)#

Load the dataclass from a YAML file.

classmethod loads(s)#

Load the dataclass from a YAML string.

class flexrag.models.HFClipEncoder(cfg)[源代码]#

基类:EncoderBase

class flexrag.models.OllamaEncoderConfig(batch_size=32, log_interval=1000, model_name=None, base_url='http://localhost:11434/', prompt=None, verbose=False, embedding_size=768, allow_parallel=True)[源代码]#

Configuration for the OllamaEncoder.

参数:
  • model_name (str) -- The name of the model to use. Required.

  • base_url (str) -- The base URL of the Ollama server. Default is 'http://localhost:11434/'.

  • prompt (Optional[str]) -- The prompt to use. Default is None.

  • verbose (bool) -- Whether to show verbose logs. Default is False.

  • embedding_size (int) -- The size of the embeddings. Default is 768.

  • allow_parallel -- Whether to allow parallel generation. Default is True.

dump(path)#

Dump the dataclass to a YAML file.

dumps()#

Dump the dataclass to a YAML string.

classmethod load(path)#

Load the dataclass from a YAML file.

classmethod loads(s)#

Load the dataclass from a YAML string.

class flexrag.models.OllamaEncoder(cfg)[源代码]#

基类:EncoderBase

class flexrag.models.SentenceTransformerEncoderConfig(batch_size=32, log_interval=1000, model_path=None, device_id=<factory>, trust_remote_code=False, task=None, prompt_name=None, prompt=None, prompt_dict=None, normalize=False, model_kwargs=<factory>)[源代码]#

Configuration for SentenceTransformerEncoder.

参数:
  • model_path (str) -- The path to the model. Required.

  • device_id (list[int]) -- The device id to use. [] for CPU. Defaults to [].

  • trust_remote_code (bool) -- Whether to trust remote code. Defaults to False.

  • task (Optional[str]) -- The task to use. Defaults to None.

  • prompt_name (Optional[str]) -- The prompt name to use. Defaults to None.

  • prompt (Optional[str]) -- The prompt to use. Defaults to None.

  • prompt_dict (Optional[dict]) -- The prompt dictionary to use. Defaults to None.

  • normalize (bool) -- Whether to normalize embeddings. Defaults to False.

  • model_kwargs (dict[str, Any]) -- Additional keyword arguments for loading the model. Defaults to {}.

dump(path)#

Dump the dataclass to a YAML file.

dumps()#

Dump the dataclass to a YAML string.

classmethod load(path)#

Load the dataclass from a YAML file.

classmethod loads(s)#

Load the dataclass from a YAML string.

class flexrag.models.SentenceTransformerEncoder(config)[源代码]#

基类:EncoderBase

Oneline Encoders#

class flexrag.models.CohereEncoderConfig(batch_size=32, log_interval=1000, model='embed-v4.0', input_type='search_document', embedding_size='1536', base_url=None, api_key=None, proxy=None)[源代码]#

Configuration for CohereEncoder.

参数:
  • model (str) -- The model to use. Default is "embed-v4.0".

  • input_type (str) -- Specifies the type of input passed to the model. Required for embedding models v3 and higher. Default is "search_document". Available options are "search_document", "search_query", "classification", "clustering", "image".

  • embedding_size (str) -- The size of the embedding. Default is "1536". Available options are "256", "512", "1024", "1536". This option is only used for embedding models v4 and newer.

  • base_url (Optional[str]) -- The base URL of the API. Default is None.

  • api_key (str) -- The API key for the Cohere API. If not provided, it will use the environment variable COHERE_API_KEY. Defaults to None.

  • proxy (Optional[str]) -- The proxy to use. Default is None.

dump(path)#

Dump the dataclass to a YAML file.

dumps()#

Dump the dataclass to a YAML string.

classmethod load(path)#

Load the dataclass from a YAML file.

classmethod loads(s)#

Load the dataclass from a YAML string.

class flexrag.models.CohereEncoder(cfg)[源代码]#

基类:EncoderBase

class flexrag.models.JinaEncoderConfig(batch_size=32, log_interval=1000, model='jina-embeddings-v3', base_url='https://api.jina.ai/v1/embeddings', api_key=None, embedding_size=1024, task=None, proxy=None)[源代码]#

Configuration for JinaEncoder.

参数:
  • model (str) -- The model to use. Default is "jina-embeddings-v3".

  • base_url (str) -- The base URL of the Jina embeddings API. Default is "https://api.jina.ai/v1/embeddings".

  • api_key (str) -- The API key for the Jina embeddings API. If not provided, it will use the environment variable JINA_API_KEY. Defaults to None.

  • embedding_size (int) -- The dimension of the embeddings. Default is 1024.

  • task (str) -- The task for the embeddings. Default is None. Available options are "retrieval.query", "retrieval.passage", "separation", "classification", and "text-matching".

  • proxy (Optional[str]) -- The proxy to use. Defaults to None.

dump(path)#

Dump the dataclass to a YAML file.

dumps()#

Dump the dataclass to a YAML string.

classmethod load(path)#

Load the dataclass from a YAML file.

classmethod loads(s)#

Load the dataclass from a YAML string.

class flexrag.models.JinaEncoder(cfg)[源代码]#

基类:EncoderBase

class flexrag.models.OpenAIEncoderConfig(batch_size=32, log_interval=1000, is_azure=False, model_name=None, base_url=None, api_key='EMPTY', api_version='2024-07-01-preview', verbose=False, proxy=None, embedding_size=None)[源代码]#

基类:OpenAIConfig, EncoderBaseConfig

Configuration for OpenAI Encoder.

参数:

embedding_size (Optional[int]) -- The size of the embedding vector. If None, it will be determined from the model. Default is None.

dump(path)#

Dump the dataclass to a YAML file.

dumps()#

Dump the dataclass to a YAML string.

classmethod load(path)#

Load the dataclass from a YAML file.

classmethod loads(s)#

Load the dataclass from a YAML string.

class flexrag.models.OpenAIEncoder(cfg)[源代码]#

基类:EncoderBase