File size: 3,113 Bytes
8f842e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb3ce56
8f842e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ff7bad
 
 
 
8f842e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
---
title: Text Embedding
emoji: 🚀
colorFrom: purple
colorTo: yellow
sdk: docker
pinned: false
license: apache-2.0
---

# Embedding API

API to call an embedding model ([intfloat/multilingual-e5-large](https://huggingface.co/intfloat/multilingual-e5-large)) for generating multilingual text embeddings.<br>
The embedding model takes a text string and converts it into 1024 dimension vector.<br>
Using a `POST` request to the `/embed` endpoint with a list of texts, the API returns their corresponding embeddings.<br>
A maximum of 2000 characters per text is enforced to avoid truncation, and thereby loss of information, by the tokenizer.<br>
Each text must start with either "query: " or "passage: ".<br>


The API is deployed at a Hugging Face Docker space where the Swagger UI can be acccessed at:<br>
[https://emilbm-text-embedding.hf.space/docs](https://emilbm-text-embedding.hf.space/docs)

## Features

- FastAPI-based REST API
- `/embed` endpoint for generating embeddings from a list of texts
- `/health` endpoint for checking the API status
- Uses HuggingFace Transformers and PyTorch
- Includes linting and unit tests
- Dockerfile for containerization
- CI/CD with GitHub Actions to build, lint, test, and deploy to Hugging Face

## Local Development
### Requirements

- Python 3.12+
- [UV](https://docs.astral.sh/uv/)
- (Optional) Docker
### Installation

1. **Clone the repository:**
	 ```sh
	 git clone https://github.com/EmilbMadsen/embedding-api.git
	 cd embedding-api
	 ```

2. **Create a virtual environment and activate it:**
	 ```sh
	 uv venv
	 source .venv/bin/activate
	 ```

3. **Install dependencies:**
	 ```sh
	 uv sync
	 ```

### Formatting, Linting and Unit Tests
- **Formatting (with Black and Ruff) and linting (with Black, Ruff, and MyPy):**
	```sh
	make format
	make lint
	```
- **Run unit tests:**
	```sh
	make test
	```

### Running Locally (without Docker)

Start the API server with Uvicorn:

```sh
uvicorn app.main:app --reload --port 7860
```

### Running Locally (with Docker)
Build and start the API server with Docker:

```sh
docker build -t embedding-api .
docker run -p 7860:7860 embedding-api
```
### Test the endpoint
Test the endpoint with either:
```sh
curl -X 'POST' \
  'http://127.0.0.1:7860/embed' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "texts": [
    "query: what is the capital of France?",
    "passage: Paris is the capital of France."
  ]
}'
```
Or through the Swagger UI.



## Usage

### Embed Endpoint

- **POST** `/embed`
- **Request Body:**
	```json
	{
		"texts": [
			"query: what is the capital of France?",
			"passage: Paris is the capital of France."
		]
	}
	```
- **Response:**
	```json
	{
		"embeddings": [[...], [...]]
	}
	```

### Health Endpoint

- **GET** `/health`
- **Response:**
	```json
	{
		"status": "ok"
	}
	```

## Project Structure

```
app/
		main.py            # FastAPI app
		embeddings.py      # Embedding logic
		models.py          # Request/response models
		logger.py          # Logging setup
tests/
		test_api.py        # API tests
		test_embeddings.py # Embedding tests
```