A demonstration on creating a semantic search with Python thanks to the Django web framework and pgvector, the extension for vector storage on PostgreSQL and similarity search.
start in Lawrence Journal-World (2003) Public release and WSGI (PEP 333) support (2005) Django Software Foundation (2008) Python 3 support (2013) ASGI support (2019) Psycopg 3 support (2022)
SELECT "items_item"."content" FROM "items_item" WHERE ( "items_item"."content" LIKE '%rock%' ESCAPE '\' AND "items_item"."in_stock" AND "items_item"."price" = 10 ) ORDER BY "items_item"."content" ASC;
$ $ python -m manage createsuperuser $ python -m manage runserver Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). April 05, 2024 - 19:30:00 Django version 5.0.4, using settings 'semanticsearch.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. 24
4 2023) [GCC 13.2.0] on linux Type "help", "copyright", "credits" or "license" for more info. >>> from items.models import Item >>> Item.objects.filter(content__icontains="rocks") <QuerySet []> >>> Item.objects.filter( ... content__search="rocks" ... ).values_list("content", flat=True) <QuerySet ['rock']>
SELECT "items_item"."id", "items_item"."content", "items_item"."embedding" FROM "items_item" WHERE ("items_item"."embedding" <=> '[...]') < 0.5 ORDER BY ("items_item"."embedding" <=> '[...]') ASC;