High-performance semantic search microservice built with FastAPI + Pinecone.
- FastAPI async API with lifecycle init for Pinecone + embedding provider.
- Embeddings: pluggable provider (
openaiorsentence-transformers). - Pinecone holds vectors and metadata; the service auto-creates index if missing.
- Ingestion is batched and concurrent for throughput; search is optimized for low latency.
- POST
/ingest/bulk- Bulk upsert vectors- Body:
{ "items": [{ "id": "1", "text": "hello", "metadata": {"k":"v"} }, ...] } - Response:
{ "upserted": n, "batch_count": m, "errors": [] }
- Body:
- POST
/search- Semantic search- Body:
{ "query": "search text", "top_k": 10 } - Response:
{ "results": [{ "id": "...", "score": 0.9, "metadata": {...} }, ...] }
- Body:
- GET
/health- Liveness - GET
/ready- Readiness
Ingest:
curl -X POST http://localhost:8000/ingest/bulk -H "Content-Type: application/json" \
-d '{"items":[{"id":"1","text":"Hello world","metadata":{"source":"test"}}]}'Search:
curl -X POST http://localhost:8000/search -H "Content-Type: application/json" \
-d '{"query":"hello","top_k":5}'- Create an API key at Pinecone.
- Set
PINECONE_API_KEY,PINECONE_ENV, andPINECONE_INDEXin your environment or.env. - On startup the service will create the index if it does not exist (uses
DIMENSIONandPINECONE_METRIC).
PINECONE_API_KEY,PINECONE_ENV,PINECONE_INDEXEMBEDDING_PROVIDER=openai|sentence-transformersOPENAI_API_KEY(required ifEMBEDDING_PROVIDER=openai)TRANSFORMER_MODELwhen using sentence-transformersBATCH_SIZE,BATCH_CONCURRENCY,DIMENSION,PINECONE_METRIC
- Batching and concurrent upserts reduce overhead for large bulk ingest.
- Keep the embedding provider choice in mind: local transformer models add startup time and memory; OpenAI delegates compute to the API but adds network latency.
- Horizontal scaling: run multiple replicas behind a load balancer. Pinecone handles vector storage scaling; keep clients stateless.
- For <100ms latency target for search, prefer OpenAI embeddings (fast network) or colocate transformer models on the same region. Tweak
top_kand useinclude_valuescarefully.
- Copy
env.exampleto.envand fill values. - Create a virtualenv and install:
pip install -r requirements.txt
- Run:
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
Build:
docker build -t scalable-vector-search:latest .Run (example):
docker run --rm -p 8000:8000 \
-e PINECONE_API_KEY=... \
-e PINECONE_ENV=... \
-e PINECONE_INDEX=svc-index \
-e EMBEDDING_PROVIDER=openai \
-e OPENAI_API_KEY=... \
scalable-vector-search:latest- The service uses threadpool offloading for blocking SDK calls to keep FastAPI event loop responsive.
sentence-transformersmay load large ML models; on Docker deployments, prefer usingEMBEDDING_PROVIDER=openaifor smaller images unless you provision GPUs and memory.- Ensure your Pinecone index
DIMENSIONmatches the embedding dimension of your chosen model.