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

Use sqlite-vec extension instead of chromadb for embeddings #14163

Merged
merged 56 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
affa7bd
swap sqlite_vec for chroma in requirements
hawkeye217 Oct 4, 2024
139c865
load sqlite_vec in embeddings manager
hawkeye217 Oct 4, 2024
654efe6
remove chroma and revamp Embeddings class for sqlite_vec
hawkeye217 Oct 4, 2024
4444d82
manual minilm onnx inference
hawkeye217 Oct 4, 2024
3c33417
remove chroma in clip model
hawkeye217 Oct 4, 2024
3e5420c
migrate api from chroma to sqlite_vec
hawkeye217 Oct 4, 2024
f5aceec
migrate event cleanup from chroma to sqlite_vec
hawkeye217 Oct 4, 2024
cb5b982
migrate embedding maintainer from chroma to sqlite_vec
hawkeye217 Oct 4, 2024
b31216d
genai description for sqlite_vec
hawkeye217 Oct 4, 2024
5181ea7
load sqlite_vec in main thread db
hawkeye217 Oct 4, 2024
1b7f469
extend the SqliteQueueDatabase class and use peewee db.execute_sql
hawkeye217 Oct 4, 2024
fc242e2
search with Event type for similarity
hawkeye217 Oct 4, 2024
afb36a5
fix similarity search
hawkeye217 Oct 4, 2024
980a889
install and add comment about transformers
hawkeye217 Oct 4, 2024
df94a94
fix normalization
hawkeye217 Oct 4, 2024
cdafb31
add id filter
hawkeye217 Oct 4, 2024
e74d35a
clean up
hawkeye217 Oct 4, 2024
ac9f904
clean up
hawkeye217 Oct 4, 2024
6607ad2
fully remove chroma and add transformers env var
hawkeye217 Oct 4, 2024
ddfabbb
readd uvicorn for fastapi
hawkeye217 Oct 4, 2024
e28767b
readd tokenizer parallelism env var
hawkeye217 Oct 5, 2024
a4d43a9
remove chroma from docs
hawkeye217 Oct 5, 2024
3b78908
remove chroma from UI
hawkeye217 Oct 5, 2024
d189893
try removing custom pysqlite3 build
hawkeye217 Oct 5, 2024
92298e6
hard code limit
hawkeye217 Oct 5, 2024
fa6fda4
optimize queries
hawkeye217 Oct 5, 2024
6bfee99
revert explore query
hawkeye217 Oct 5, 2024
18e1eca
fix query
hawkeye217 Oct 5, 2024
57d0aab
keep building pysqlite3
hawkeye217 Oct 5, 2024
3e58a6d
single pass fetch and process
hawkeye217 Oct 5, 2024
bc52812
remove unnecessary re-embed
hawkeye217 Oct 5, 2024
72e4883
update deps
hawkeye217 Oct 5, 2024
87cc007
move SqliteVecQueueDatabase to db directory
hawkeye217 Oct 5, 2024
cf7b278
make search thumbnail take up full size of results box
hawkeye217 Oct 6, 2024
0b736c8
improve typing
hawkeye217 Oct 6, 2024
52a2bbc
improve model downloading and add status screen
hawkeye217 Oct 7, 2024
9679520
daemon downloading thread
hawkeye217 Oct 7, 2024
d7df5c9
catch case when semantic search is disabled
hawkeye217 Oct 7, 2024
41d1f04
fix typing
hawkeye217 Oct 7, 2024
d5f6a14
build sqlite_vec from source
hawkeye217 Oct 7, 2024
4391e9c
resolve conflict
hawkeye217 Oct 7, 2024
8f9f25f
file permissions
hawkeye217 Oct 7, 2024
afac978
try build deps
hawkeye217 Oct 7, 2024
3f289d5
remove sources
hawkeye217 Oct 7, 2024
ce80aeb
sources
hawkeye217 Oct 7, 2024
e34f948
fix thread start
hawkeye217 Oct 7, 2024
28d48dc
include git in build
hawkeye217 Oct 7, 2024
2eaf160
reorder embeddings after detectors are started
hawkeye217 Oct 7, 2024
f089366
build with sqlite amalgamation
hawkeye217 Oct 7, 2024
f5f2820
non-platform specific
hawkeye217 Oct 7, 2024
8b6bd30
use wget instead of curl
hawkeye217 Oct 7, 2024
2e72f6f
remove unzip -d
hawkeye217 Oct 7, 2024
dbdbbc0
remove sqlite_vec from requirements and load the compiled version
hawkeye217 Oct 7, 2024
78a5075
fix build
hawkeye217 Oct 7, 2024
5cda95f
avoid race in db connection
hawkeye217 Oct 7, 2024
e3a81db
add scale_factor and bias to description zscore normalization
hawkeye217 Oct 7, 2024
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
20 changes: 15 additions & 5 deletions docker/main/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ RUN --mount=type=tmpfs,target=/tmp --mount=type=tmpfs,target=/var/cache/apt \
--mount=type=cache,target=/root/.ccache \
/deps/build_nginx.sh

FROM wget AS sqlite-vec
ARG DEBIAN_FRONTEND

# Build sqlite_vec from source
COPY docker/main/build_sqlite_vec.sh /deps/build_sqlite_vec.sh
RUN --mount=type=tmpfs,target=/tmp --mount=type=tmpfs,target=/var/cache/apt \
--mount=type=bind,source=docker/main/build_sqlite_vec.sh,target=/deps/build_sqlite_vec.sh \
--mount=type=cache,target=/root/.ccache \
/deps/build_sqlite_vec.sh

FROM scratch AS go2rtc
ARG TARGETARCH
WORKDIR /rootfs/usr/local/go2rtc/bin
Expand Down Expand Up @@ -163,7 +173,7 @@ RUN wget -q https://bootstrap.pypa.io/get-pip.py -O get-pip.py \
COPY docker/main/requirements.txt /requirements.txt
RUN pip3 install -r /requirements.txt

# Build pysqlite3 from source to support ChromaDB
# Build pysqlite3 from source
COPY docker/main/build_pysqlite3.sh /build_pysqlite3.sh
RUN /build_pysqlite3.sh

Expand All @@ -177,6 +187,7 @@ RUN pip3 wheel --no-deps --wheel-dir=/wheels-post -r /requirements-wheels-post.t
# Collect deps in a single layer
FROM scratch AS deps-rootfs
COPY --from=nginx /usr/local/nginx/ /usr/local/nginx/
COPY --from=sqlite-vec /usr/local/lib/ /usr/local/lib/
COPY --from=go2rtc /rootfs/ /
COPY --from=libusb-build /usr/local/lib /usr/local/lib
COPY --from=tempio /rootfs/ /
Expand All @@ -197,12 +208,11 @@ ARG APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn
ENV NVIDIA_VISIBLE_DEVICES=all
ENV NVIDIA_DRIVER_CAPABILITIES="compute,video,utility"

# Turn off Chroma Telemetry: https://docs.trychroma.com/telemetry#opting-out
ENV ANONYMIZED_TELEMETRY=False
# Allow resetting the chroma database
ENV ALLOW_RESET=True
# Disable tokenizer parallelism warning
# https://stackoverflow.com/questions/62691279/how-to-disable-tokenizers-parallelism-true-false-warning/72926996#72926996
ENV TOKENIZERS_PARALLELISM=true
# https://github.com/huggingface/transformers/issues/27214
ENV TRANSFORMERS_NO_ADVISORY_WARNINGS=1

ENV PATH="/usr/local/go2rtc/bin:/usr/local/tempio/bin:/usr/local/nginx/sbin:${PATH}"
ENV LIBAVFORMAT_VERSION_MAJOR=60
Expand Down
31 changes: 31 additions & 0 deletions docker/main/build_sqlite_vec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

set -euxo pipefail

SQLITE_VEC_VERSION="0.1.3"

cp /etc/apt/sources.list /etc/apt/sources.list.d/sources-src.list
sed -i 's|deb http|deb-src http|g' /etc/apt/sources.list.d/sources-src.list
apt-get update
apt-get -yqq build-dep sqlite3 gettext git

mkdir /tmp/sqlite_vec
# Grab the sqlite_vec source code.
wget -nv https://github.com/asg017/sqlite-vec/archive/refs/tags/v${SQLITE_VEC_VERSION}.tar.gz
tar -zxf v${SQLITE_VEC_VERSION}.tar.gz -C /tmp/sqlite_vec

cd /tmp/sqlite_vec/sqlite-vec-${SQLITE_VEC_VERSION}

mkdir -p vendor
wget -O sqlite-amalgamation.zip https://www.sqlite.org/2024/sqlite-amalgamation-3450300.zip
unzip sqlite-amalgamation.zip
mv sqlite-amalgamation-3450300/* vendor/
rmdir sqlite-amalgamation-3450300
rm sqlite-amalgamation.zip

# build loadable module
make loadable

# install it
cp dist/vec0.* /usr/local/lib

9 changes: 5 additions & 4 deletions docker/main/requirements-wheels.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ click == 8.1.*
# FastAPI
starlette-context == 0.3.6
fastapi == 0.115.0
uvicorn == 0.30.*
slowapi == 0.1.9
imutils == 0.5.*
joserfc == 1.0.*
Expand Down Expand Up @@ -32,12 +33,12 @@ unidecode == 1.3.*
# OpenVino (ONNX installed in wheels-post)
openvino == 2024.3.*
# Embeddings
chromadb == 0.5.7
transformers == 4.45.*
onnx_clip == 4.0.*
# Generative AI
google-generativeai == 0.6.*
ollama == 0.2.*
openai == 1.30.*
google-generativeai == 0.8.*
ollama == 0.3.*
openai == 1.51.*
# push notifications
py-vapid == 1.9.*
pywebpush == 2.0.*

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions docker/main/rootfs/etc/s6-overlay/s6-rc.d/chroma-log/run

This file was deleted.

1 change: 0 additions & 1 deletion docker/main/rootfs/etc/s6-overlay/s6-rc.d/chroma-log/type

This file was deleted.

Empty file.
28 changes: 0 additions & 28 deletions docker/main/rootfs/etc/s6-overlay/s6-rc.d/chroma/finish

This file was deleted.

This file was deleted.

27 changes: 0 additions & 27 deletions docker/main/rootfs/etc/s6-overlay/s6-rc.d/chroma/run

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion docker/main/rootfs/etc/s6-overlay/s6-rc.d/chroma/type

This file was deleted.

Empty file.
2 changes: 1 addition & 1 deletion docker/main/rootfs/etc/s6-overlay/s6-rc.d/log-prepare/run
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

set -o errexit -o nounset -o pipefail

dirs=(/dev/shm/logs/frigate /dev/shm/logs/go2rtc /dev/shm/logs/nginx /dev/shm/logs/certsync /dev/shm/logs/chroma)
dirs=(/dev/shm/logs/frigate /dev/shm/logs/go2rtc /dev/shm/logs/nginx /dev/shm/logs/certsync)

mkdir -p "${dirs[@]}"
chown nobody:nogroup "${dirs[@]}"
Expand Down
14 changes: 0 additions & 14 deletions docker/main/rootfs/usr/local/chroma

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions docs/docs/configuration/semantic_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Using Semantic Search

Semantic Search in Frigate allows you to find tracked objects within your review items using either the image itself, a user-defined text description, or an automatically generated one. This feature works by creating _embeddings_ — numerical vector representations — for both the images and text descriptions of your tracked objects. By comparing these embeddings, Frigate assesses their similarities to deliver relevant search results.

Frigate has support for two models to create embeddings, both of which run locally: [OpenAI CLIP](https://openai.com/research/clip) and [all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2). Embeddings are then saved to a local instance of [ChromaDB](https://trychroma.com).
Frigate has support for two models to create embeddings, both of which run locally: [OpenAI CLIP](https://openai.com/research/clip) and [all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2). Embeddings are then saved to Frigate's database.

Semantic Search is accessed via the _Explore_ view in the Frigate UI.

Expand All @@ -29,7 +29,7 @@ If you are enabling the Search feature for the first time, be advised that Friga

### OpenAI CLIP

This model is able to embed both images and text into the same vector space, which allows `image -> image` and `text -> image` similarity searches. Frigate uses this model on tracked objects to encode the thumbnail image and store it in Chroma. When searching for tracked objects via text in the search box, Frigate will perform a `text -> image` similarity search against this embedding. When clicking "Find Similar" in the tracked object detail pane, Frigate will perform an `image -> image` similarity search to retrieve the closest matching thumbnails.
This model is able to embed both images and text into the same vector space, which allows `image -> image` and `text -> image` similarity searches. Frigate uses this model on tracked objects to encode the thumbnail image and store it in the database. When searching for tracked objects via text in the search box, Frigate will perform a `text -> image` similarity search against this embedding. When clicking "Find Similar" in the tracked object detail pane, Frigate will perform an `image -> image` similarity search to retrieve the closest matching thumbnails.

### all-MiniLM-L6-v2

Expand Down
5 changes: 2 additions & 3 deletions frigate/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,12 @@ def vainfo():

@router.get("/logs/{service}", tags=[Tags.logs])
def logs(
service: str = Path(enum=["frigate", "nginx", "go2rtc", "chroma"]),
service: str = Path(enum=["frigate", "nginx", "go2rtc"]),
download: Optional[str] = None,
start: Optional[int] = 0,
end: Optional[int] = None,
):
"""Get logs for the requested service (frigate/nginx/go2rtc/chroma)"""
"""Get logs for the requested service (frigate/nginx/go2rtc)"""

def download_logs(service_location: str):
try:
Expand All @@ -408,7 +408,6 @@ def download_logs(service_location: str):
"frigate": "/dev/shm/logs/frigate/current",
"go2rtc": "/dev/shm/logs/go2rtc/current",
"nginx": "/dev/shm/logs/nginx/current",
"chroma": "/dev/shm/logs/chroma/current",
}
service_location = log_locations.get(service)

Expand Down
Loading