Skip to content

FastAPI service for generating sentence embeddings and token counts using the E5 model. Docker-ready.

Notifications You must be signed in to change notification settings

AGRFesta/e5-embedding-service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

E5 Embedder API

Version

This is a simple FastAPI-based service that provides two main endpoints:

  • /embed: Generates normalized sentence embeddings using the intfloat/e5-large model.
  • /count-tokens: Counts the number of tokens for each input sentence using the Hugging Face tokenizer.

πŸ”§ Requirements


πŸ“¦ Clone and Prepare the Model

To include the model in the build context, clone the Hugging Face repository inside the app folder:

git clone https://huggingface.co/intfloat/e5-large app/e5_model

Make sure the directory structure is like this:

.
β”œβ”€β”€ app
β”‚   β”œβ”€β”€ main.py
β”‚   β”œβ”€β”€ requirements.txt
β”‚   └── e5_model
β”‚       β”œβ”€β”€ config.json
...

πŸ›  Build the Docker Image

You can build the Docker image and assign it a specific version (e.g. 0.2.0):

docker build -t e5-embedder:0.2.0 .

▢️ Run the Container

To run the service and expose it on port 8000:

docker run -p 8000:8000 e5-embedder:0.2.0

The API will be accessible at: http://localhost:8000


πŸ“¬ Available Endpoints

POST /embed

Request:

{
  "sentences": ["What is the capital of France?", "Tell me about Python."]
}

Response:

{
  "vectors": [[...], [...]]
}

Each vector is a normalized embedding of the input sentence.


POST /count-tokens

Request:

{
  "sentences": ["Hello world!", "This is a test."]
}

Response:

{
  "token_counts": [
    {"sentence": "Hello world!", "token_count": 4},
    {"sentence": "This is a test.", "token_count": 6}
  ]
}

πŸ“ Project Structure

.
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ app
β”‚   β”œβ”€β”€ main.py
β”‚   β”œβ”€β”€ requirements.txt
β”‚   └── e5_model/         # Cloned model files here

🧼 Clean Up

To remove the container after testing:

docker ps -a          # Find container ID
docker rm <container_id>

To remove the image:

docker rmi e5-embedder:0.2.0

About

FastAPI service for generating sentence embeddings and token counts using the E5 model. Docker-ready.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published