tei-client
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
)
Metadata
Release files for tei-client 0.2.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| tei_client-0.2.4.tar.gz | 18.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tei_client-0.2.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 36.2 kB
Release files / tei_client-0.2.4.tar.gz
| Download URL | tei_client-0.2.4.tar.gz |
|---|---|
| Size | 18.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
30c779ad7884b697c8e8b34cf0e10b4f30c1e25b340046a519d8d1211a49b2f6
|
|
BLAKE2b-256 checksum How to use checksums |
69cf7727fbfcc5e3289020304beab08ec1595bfae091b52bad8a1efaa81046ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.23
|
Release files / tei_client-0.2.4-py3-none-any.whl
| Download URL | tei_client-0.2.4-py3-none-any.whl |
|---|---|
| Size | 17.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
679c00fe414b0c82ea92c28a3ba5e9bed4361777d94b2d81112ef0aa49c15775
|
|
BLAKE2b-256 checksum How to use checksums |
9a93d98326422dcc2919e12ce1a48e3e91070b1a63adc8cc2170a708dbf36055
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.23
|