Skip to main content

Introduction

Modern encoders have more than one stage of encoding. Developers manually create pipes for converting their text in vector. But some steps are atomic bricks and can be reuseful. Beside it some encoding takes a lot of time and we reinvent caches. Encoder library provide simple way initialization and pipeline construction.

Install

pip install encoder-lib[bert_embedded,bert_client]

Get started

Let's create bert thin client for bert-as-service

from encoders.encoder_factory import EncoderFactory

encoder_conf_dict = {
    "default": {
        "type": "bert_client",
        "input_dim": 1,
        "output_dim": 768,
        "params": {
            "port": 5555,
            "port_out": 5556,
            "ip": "localhost",
            "timeout": 5000, 
        }
   }
}
encoder_factory = EncoderFactory(encoder_conf_dict)

encoder = encoder_factory.get_encoder("default")
documents_list = ["Hello World!"]
vectors = encoder.encode(documents_list)

Coll, we have encoder, but each request over network takes time. Let's enhance the encoder and add simple in-memory in cache

from encoders.encoder_factory import EncoderFactory

encoder_conf_dict = {
    "default": {
        "type": "bert_client",
        "input_dim": 1,
        "output_dim": 768,
        "params": {
            "port": 5555,
            "port_out": 5556,
            "ip": "localhost",
            "timeout": 5000, 
        },
        "cache": {
            "type": "simple"
        }
   }
}
encoder_factory = EncoderFactory(encoder_conf_dict)

encoder = encoder_factory.get_encoder("default")
documents_list = ["Hello World!"]
# Encoder sends request over network
vectors = encoder.encode(documents_list)
# This call takes vector from cache 
vectors = encoder.encode(documents_list)

Simple cache stores data in memory without any memory restriction. Beside it we can keep time on warming up and load pre-computed vectors from file:

encoder_conf_dict = {
    "default": {
        "type": "bert_client",
        "input_dim": 1,
        "output_dim": 768,
        "params": {
            "port": 5555,
            "port_out": 5556,
            "ip": "localhost",
            "timeout": 5000, 
        },
        "cache": {
            "type": "simple",
            "params": {
                "path_desc": {
                    "type": "absolute",
                    "file": "/cache/bert_cache.pkl"
                }
            }
        }
   }
}

Path object

Path object is flexible description of file location. Current path object version supports:

  1. Absolute path - allow to specify full path to file

    path_desc:
      type: absolute
      file: full_file_path
    
  2. Relative path - allow to specify relative path to file. We separate full file name on two parts relative and base. Relative part is stored in param "file". Base part is stored in OS environment variable and make you config transferable to other computers.

    path_desc:
      type: relative
      file: relative_file_name
      os_env: ENV_VAR
    

    Examples

    path_desc:
      type: relative
      os_env: BERT_HOME
      file: "cache/bert_cache.pkl"
    

Supported vectorisers

  1. Bert-as-Service client
  2. Bert embedded
  3. TF-IDF
  4. Composite vectoriser
example_bert_client:
  type: bert_client
  input_dim: 1
  output_dim: 768
  params:
    port: 5555
    port_out: 5556
    ip: localhost
    timeout: 5000

example_bert_embedded:
  type: bert_embedded
  verbose: True
  input_dim: 1
  output_dim: 768
  params:
    seq_len: 25
    graph:
      path_desc:
        type: relative
        os_env: BERT_HOME
        file: model_for_inference.pbtxt
    vocab:
      path_desc:
        type: relative
        os_env: BERT_HOME
        file: vocab.txt

example_composite:
  type: composite
  params:
    encoders:
      - example_bert_client

example_tf_idf:
    type: tfidf
    params:
      path_desc:
        type: absolute
        file: /dumped_tf_idf/model.pkl

Release notes

1.3

  1. Added parameter seq_len for BERTFeatureExtractor

1.2

  1. Added parameter verbose for BaseEncoder and all child classes
  2. Added method simple_dump_to_pickle for dumping EncoderCache.

1.0

  1. Added base functionality for Bert and TF-IDF encoders

Metadata

Release files for encoder-lib 1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for encoder-lib 1.3
File Size Uploaded Provenance Actions
encoder_lib-1.3.tar.gz 5.8 kB Without provenance Details for encoder_lib-1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for encoder-lib 1.3
File Interpreter ABI Platform Actions
encoder_lib-1.3-py3-none-any.whl Python 3 none any Details for encoder_lib-1.3-py3-none-any.whl

Total release size: 14.7 kB

Release files / encoder_lib-1.3.tar.gz

Download URL encoder_lib-1.3.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
02799e54fcfc96b8dd12b047955eed2232242a0f5acb0125f39d60778c7a59f3
BLAKE2b-256 checksum
How to use checksums
51e51d51cdef84cc3579d1242b8420a3575622ca9d1cd9056a1a6b5f3df6f11a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

Release files / encoder_lib-1.3-py3-none-any.whl

Download URL encoder_lib-1.3-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8cc86167669f7a1fbf80ca01a2e68d21b6448dd374101b95670c11c9158e0da5
BLAKE2b-256 checksum
How to use checksums
f71b91390f1bad4fa9a3e5d873e912fc2089cc5b54ba25a86623266342f2bf62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

Security for encoder-lib 1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page