Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update qdrant and lancedb versions and dockerfiles #45

Merged
merged 5 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,4 @@ dmypy.json
data/*.json
data/*.jsonl
dbs/meilisearch/meili_data
*/*/onnx_model/onnx
*/*/lancedb/*.lance
dbs/lancedb/winemag
2 changes: 1 addition & 1 deletion dbs/lancedb/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
LANCEDB_DIR = "lancedb"
LANCEDB_DIR = "winemag"
API_PORT = 8006
EMBEDDING_MODEL_CHECKPOINT = "sentence-transformers/multi-qa-MiniLM-L6-cos-v1"

Expand Down
3 changes: 2 additions & 1 deletion dbs/lancedb/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.10-slim-bullseye
FROM python:3.11-slim-bullseye

WORKDIR /wine

Expand All @@ -7,6 +7,7 @@ COPY ./requirements.txt /wine/requirements.txt
RUN pip install --no-cache-dir -U pip wheel setuptools
RUN pip install --no-cache-dir -r /wine/requirements.txt

COPY ./winemag /wine/winemag
COPY ./api /wine/api
COPY ./schemas /wine/schemas

Expand Down
14 changes: 10 additions & 4 deletions dbs/lancedb/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

import lancedb
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from sentence_transformers import SentenceTransformer

from api.config import Settings
from api.routers.rest import router

model_type = "sbert"


@lru_cache()
def get_settings():
Expand All @@ -24,9 +23,8 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
settings = get_settings()
model_checkpoint = settings.embedding_model_checkpoint
app.model = SentenceTransformer(model_checkpoint)
app.model_type = "sbert"
# Define LanceDB client
db = lancedb.connect("./lancedb")
db = lancedb.connect("./winemag")
app.table = db.open_table("wines")
print("Successfully connected to LanceDB")
yield
Expand All @@ -52,3 +50,11 @@ async def root():

# Attach routes
app.include_router(router, prefix="/wine", tags=["wine"])

# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:8000"],
allow_methods=["GET"],
allow_headers=["*"],
)
7 changes: 3 additions & 4 deletions dbs/lancedb/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ version: "3.9"

services:
fastapi:
platform: linux/x86_64
image: lancedb_wine_fastapi:${TAG}
build:
context: .
Expand All @@ -12,6 +11,6 @@ services:
- .env
ports:
- ${API_PORT}:8000
volumes:
- ./:/winex
command: uvicorn api.main:app --host 0.0.0.0 --port 8000 --reload
environment:
- LANCEDB_CONFIG_DIR=/wine
command: uvicorn api.main:app --host 0.0.0.0 --port 8000 --reload
4 changes: 2 additions & 2 deletions dbs/lancedb/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
lancedb~=0.2.0
lancedb~=0.3.0
transformers~=4.28.0
sentence-transformers~=2.2.0
pydantic~=2.3.0
pydantic-settings~=2.0.0
python-dotenv>=1.0.0
fastapi~=0.100.0
fastapi~=0.104.0
httpx>=0.24.0
aiohttp>=3.8.4
uvicorn>=0.21.0, <1.0.0
Expand Down
7 changes: 4 additions & 3 deletions dbs/lancedb/scripts/bulk_index_sbert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
from codetiming import Timer
from dotenv import load_dotenv
from lancedb.pydantic import pydantic_to_schema
from sentence_transformers import SentenceTransformer
from tqdm import tqdm

sys.path.insert(1, os.path.realpath(Path(__file__).resolve().parents[1]))
from api.config import Settings
from schemas.wine import LanceModelWine, Wine
from sentence_transformers import SentenceTransformer


load_dotenv()
# Custom types
Expand Down Expand Up @@ -97,7 +98,7 @@ def embed_batches(tbl: str, validated_data: list[JsonBlob]) -> pd.DataFrame:


def main(data: list[JsonBlob]) -> None:
DB_NAME = "../lancedb"
DB_NAME = f"../{get_settings().lancedb_dir}"
TABLE = "wines"
db = lancedb.connect(DB_NAME)

Expand All @@ -115,7 +116,7 @@ def main(data: list[JsonBlob]) -> None:
with Timer(name="Create index", text="Created IVF-PQ index in {:.4f} sec"):
# Creating index (choose num partitions as a power of 2 that's closest to len(dataset) // 5000)
# In this case, we have 130k datapoints, so the nearest power of 2 is 130000//5000 ~ 32)
tbl.create_index(metric="cosine", num_partitions=32, num_sub_vectors=96)
tbl.create_index(metric="cosine", num_partitions=4, num_sub_vectors=32)


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions dbs/qdrant/.env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
QDRANT_VERSION = "v1.3.0"
QDRANT_VERSION = "v1.6.1"
QDRANT_PORT = 6333
QDRANT_HOST = "localhost"
QDRANT_SERVICE = "qdrant"
API_PORT = 8005
API_PORT = 8000
EMBEDDING_MODEL_CHECKPOINT = "sentence-transformers/multi-qa-MiniLM-L6-cos-v1"
ONNX_MODEL_FILENAME = "model_optimized_quantized.onnx"

Expand Down
12 changes: 11 additions & 1 deletion dbs/qdrant/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from functools import lru_cache

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from qdrant_client import QdrantClient
from sentence_transformers import SentenceTransformer

Expand All @@ -26,7 +27,7 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
app.model = SentenceTransformer(model_checkpoint)
app.model_type = "sbert"
# Define Qdrant client
app.client = QdrantClient(host=settings.qdrant_service, port=settings.qdrant_port)
app.client = QdrantClient(host=settings.qdrant_service, port=settings.qdrant_port, timeout=None)
print("Successfully connected to Qdrant")
yield
print("Successfully closed Qdrant connection and released resources")
Expand All @@ -49,5 +50,14 @@ async def root():
}


app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["*"],
)

# Attach routes
app.include_router(rest.router, prefix="/wine", tags=["wine"])
8 changes: 4 additions & 4 deletions dbs/qdrant/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
qdrant-client~=1.3.0
transformers~=4.28.0
qdrant-client~=1.6.0
transformers~=4.33.0
sentence-transformers~=2.2.0
pydantic~=2.0.0
pydantic~=2.4.0
pydantic-settings>=2.0.0
python-dotenv>=1.0.0
fastapi~=0.100.0
fastapi~=0.104.0
httpx>=0.24.0
aiohttp>=3.8.4
uvloop>=0.17.0
Expand Down