Skip to main content

Convenience Client for Hugging Face Text Embeddings Inference (TEI) with synchronous and asynchronous HTTP/gRPC support

Project description

tei-client

PyPI Version Supported Python Versions

Convenience Client for Hugging Face Text Embeddings Inference (TEI) with synchronous and asynchronous HTTP/gRPC support.

Implements the API defined in TEI Swagger.

Installation

You can easily install tei-client via pip:

pip install tei-client

Grpc Support

If you want to use grpc, you need to install tei-client with grpc support:

pip install tei-client[grpc]

Usage

Creating a Client

HTTP Example

To create an instance of the client, you can do the following:

from tei_client import HTTPClient

url = 'http://localhost:8080'
client = HTTPClient(url)
Example docker server
docker run -p 8080:80 -v ./tei_data:/data ghcr.io/huggingface/text-embeddings-inference:cpu-latest --model-id sentence-transformers/all-MiniLM-L6-v2

gRPC Example

Alternatively, you can use gRPC to connect to your server:

import grpc
from tei_client import GrpcClient

channel = grpc.insecure_channel('localhost:8080')
client = GrpcClient(channel)
Example docker server
docker run -p 8080:80 -v ./tei_data:/data ghcr.io/huggingface/text-embeddings-inference:cpu-latest-grpc --model-id sentence-transformers/all-MiniLM-L6-v2

Embedding

To generate embeddings, you can use the following methods:

Single Embedding Generation

You can generate a single embedding using the embed method:

result = client.embed("This is an example sentence")
print(result[0])

Batch Embedding Generation

To generate multiple embeddings in batch mode, use the embed method with a list of sentences:

results = client.embed(["This is an example sentence", "This is another example sentence"])
for result in results:
    print(result)

Asynchronous Embedding Generation

For asynchronous embedding generation, you can use the async_embed method:

result = await client.async_embed("This is an example sentence")

Classification

To generate classification results for a given text, you can use the following methods:

Basic Classification

You can classify a single text using the classify method:

result = client.classify("This is an example sentence")
print(result[0].scores)

NLI Style Classification

For Natural Language Inference (NLI) style classification, you can use the classify method with tuples as input. The first element of the tuple represents the premise and the second element represents the hypothesis.

premise = "This is an example sentence"
hypothesis = "An example was given"

result = client.classify((premise, hypothesis))
print(result[0].scores)

Asynchronous and Batched Classification

The classify method also supports batched requests by passing a list of tuples or strings. For asynchronous classification, you can use the async_classify method.

# Classify multiple texts in batch mode
results = client.classify(["This is an example sentence", "This is another example sentence"])
for result in results:
    print(result.scores)

# Asynchronous classification
result = await client.async_classify("This is an example sentence")

Reranking

Reranking allows you to refine the order of search results based on additional information. This feature is supported by the rerank method.

Basic Reranking

You can use the rerank method to rerank search results with the following syntax:

result = client.rerank(
    query="What is Deep Learning?",  # Search query
    texts=["Lore ipsum", "Deep Learning is ..."]  # List of text snippets
)
print(result)

Asynchronous Reranking

For asynchronous reranking, use the async_rerank method:

result = await client.async_rerank(
    query="What is Deep Learning?",  # Search query
    texts=["Lore ipsum", "Deep Learning is ..."]  # List of text snippets
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tei_client-0.1.0.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

tei_client-0.1.0-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file tei_client-0.1.0.tar.gz.

File metadata

  • Download URL: tei_client-0.1.0.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for tei_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 960cae534d0310dd8f9decb8af94f67d4f70d7cc586a2d7e6df0aa2b18d311a2
MD5 4885568c291d2684b8a63a735fcfd3f4
BLAKE2b-256 247063e55b89bc390170251eeb9f514479acc90c66e31447a383e5e30a134365

See more details on using hashes here.

File details

Details for the file tei_client-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tei_client-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for tei_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dbb39ba03625c45d086e012fe12d3153bec0cef15c19630d9bbcdfdc790b4e37
MD5 5761d8f91657e3d806994d6ae6f0be64
BLAKE2b-256 0194d1eb6ac20e845cc1ba5ed03ef9419ad604e4bc72cb85df1a42f6cff5877e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page