This is a simple FastAPI application that generates text embeddings using the all-MiniLM-L6-v2
model from the sentence-transformers
library.
- POST /embeddings: Accepts a text input and returns its embeddings as a list.
- GET /: Returns a welcome message with basic API information.
- Python 3.8+
- FastAPI
- Pydantic
- Sentence Transformers
- NumPy
- Clone the repository:
git clone <repository-url> cd <repository-directory>
- Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install fastapi uvicorn sentence-transformers numpy
-
Run the FastAPI server:
uvicorn main:app --reload
The server will start at
http://127.0.0.1:8000
. -
Access the API:
-
GET Welcome Message:
curl http://127.0.0.1:8000/
Response:
{"message": "Welcome to the Text Embeddings API. Use POST /embeddings to get embeddings."}
-
POST Text Embeddings:
curl -X POST http://127.0.0.1:8000/embeddings -H "Content-Type: application/json" -d '{"text": "This is a sample text"}'
Response:
{"embeddings": [0.12, -0.34, ..., 0.56]}
-
-
Interactive API documentation is available at
http://127.0.0.1:8000/docs
.
- main.py: Contains the FastAPI application with two endpoints:
GET /
: Returns a welcome message.POST /embeddings
: Takes a text input and returns its embeddings.
- Model: Uses
all-MiniLM-L6-v2
fromsentence-transformers
to generate embeddings. - Input Validation: Uses Pydantic's
BaseModel
to validate incoming text input.
- If an error occurs during embedding generation, the API returns a 500 status code with a detailed error message.