Project Icon

lantern

PostgreSQL向量数据管理和搜索扩展

Lantern是一个PostgreSQL数据库扩展,专门用于向量数据管理和搜索。它引入了lantern_hnsw索引类型来加速向量查询,支持多种距离函数,并提供并行索引创建和外部索引生成等功能。Lantern与pgvector兼容,性能表现出色,并提供多种辅助函数以优化工作流程。

💡 Lantern

build test codecov Run on Replit

Lantern is an open-source PostgreSQL database extension to store vector data, generate embeddings, and handle vector search operations.

It provides a new index type for vector columns called lantern_hnsw which speeds up ORDER BY ... LIMIT queries.

Lantern builds and uses usearch, a single-header state-of-the-art HNSW implementation.

🔧 Quick Install

If you don’t have PostgreSQL already, use Lantern with Docker to get started quickly:

docker run --pull=always --rm -p 5432:5432 -e "POSTGRES_USER=$USER" -e "POSTGRES_PASSWORD=postgres" -v ./lantern_data:/var/lib/postgresql/data lanterndata/lantern:latest-pg15

Then, you can connect to the database via postgresql://$USER:postgres@localhost/postgres.

To install Lantern using homebrew:

brew tap lanterndata/lantern
brew install lantern && lantern_install

You can also install Lantern on top of PostgreSQL from our precompiled binaries via a single make install.

Alternatively, you can use Lantern in one click using Replit.

🔧 Build Lantern from source code on top of your existing PostgreSQL

Prerequisites:

cmake version: >=3.3
gcc && g++ version: >=11 when building portable binaries, >= 12 when building on new hardware or with CPU-specific vectorization
PostgreSQL 11, 12, 13, 14, 15 or 16
Corresponding development package for PostgreSQL (postgresql-server-dev-$version)

To build Lantern on new hardware or with CPU-specific vectorization:

git clone --recursive https://github.com/lanterndata/lantern.git
cd lantern
mkdir build
cd build
cmake -DMARCH_NATIVE=ON ..
make install

To build portable Lantern binaries:

git clone --recursive https://github.com/lanterndata/lantern.git
cd lantern
mkdir build
cd build
cmake -DMARCH_NATIVE=OFF ..
make install

📖 How to use Lantern

Lantern retains the standard PostgreSQL interface, so it is compatible with all of your favorite tools in the PostgreSQL ecosystem.

First, enable Lantern in SQL (e.g. via psql shell)

CREATE EXTENSION lantern;

Note: After running the above, lantern extension is only available on the current postgres DATABASE (single postgres instance may have multiple such DATABASES). When connecting to a different DATABASE, make sure to run the above command for the new one as well. For example:

CREATE DATABASE newdb;
\c newdb
CREATE EXTENSION lantern;

Create a table with a vector column and add your data

CREATE TABLE small_world (id integer, vector real[3]);
INSERT INTO small_world (id, vector) VALUES (0, '{0,0,0}'), (1, '{0,0,1}');

Create an hnsw index on the table via lantern_hnsw:

CREATE INDEX ON small_world USING lantern_hnsw (vector);

Customize lantern_hnsw index parameters depending on your vector data, such as the distance function (e.g., dist_l2sq_ops), index construction parameters, and index search parameters.

CREATE INDEX ON small_world USING lantern_hnsw (vector dist_l2sq_ops)
WITH (M=2, ef_construction=10, ef=4, dim=3);

Start querying data

SET enable_seqscan = false;
SELECT id, l2sq_dist(vector, ARRAY[0,0,0]) AS dist
FROM small_world ORDER BY vector <-> ARRAY[0,0,0] LIMIT 1;

A note on operators and operator classes

Lantern supports several distance functions in the index and it has 2 modes for operators:

  1. lantern.pgvector_compat=TRUE (default) In this mode there are 3 operators available <-> (l2sq), <=> (cosine), <+> (hamming).

    Note that in this mode, you need to use right operator in order to trigger an index scan.

  2. lantern.pgvector_compat=FALSE In this mode you only need to specify the distance function used for a column at index creation time. Lantern will automatically infer the distance function to use for search so you always use <?> operator in search queries.

    Note that in this mode, the operator <?> is intended exclusively for use with index lookups. If you expect to not use the index in a query, use the distance function directly (e.g. l2sq_dist(v1, v2))

To switch between modes set lantern.pgvector_compat variable to TRUE or FALSE.

There are four defined operator classes that can be employed during index creation:

  • dist_l2sq_ops: Default for the type real[]
  • dist_vec_l2sq_ops: Default for the type vector
  • dist_cos_ops: Applicable to the type real[]
  • dist_vec_cos_ops: Applicable to the type vector
  • dist_hamming_ops: Applicable to the type integer[]

Index Construction Parameters

The M, ef, and ef_construction parameters control the performance of the HNSW algorithm for your use case.

  • In general, lower M and ef_construction speed up index creation at the cost of recall.
  • Lower M and ef improve search speed and result in fewer shared buffer hits at the cost of recall. Tuning these parameters will require experimentation for your specific use case.

Miscellaneous

  • If you have previously cloned Lantern and would like to update run git pull && git submodule update --recursive

⭐️ Features

  • Embedding generation for popular use cases (CLIP model, Hugging Face models, custom model)
  • Interoperability with pgvector's data type, so anyone using pgvector can switch to Lantern
  • Parallel index creation via an external indexer
  • Ability to generate the index graph outside of the database server
  • Support for creating the index outside of the database and inside another instance allows you to create an index without interrupting database workflows.
  • See all of our helper functions to better enable your workflows

🏎️ Performance

Important takeaways:

  • There's three key metrics we track. CREATE INDEX time, SELECT throughput, and SELECT latency.
  • We match or outperform pgvector and pg_embedding (Neon) on all of these metrics.
  • We plan to continue to make performance improvements to ensure we are the best performing database.

Lantern throughput Lantern latency Lantern index creation

🗺️ Roadmap

  • Cloud-hosted version of Lantern - Sign up here
  • Hardware-accelerated distance metrics, tailored for your CPU, enabling faster queries
  • Templates and guides for building applications for different industries
  • More tools for generating embeddings (support for third party model API’s, more local models)
  • Support for version control and A/B test embeddings
  • Autotuned index type that will choose appropriate creation parameters
  • Support for 1 byte and 2 byte vector elements, and up to 8000 dimensional vectors (PR #19)
  • Request a feature at support@lantern.dev

📚 Resources

  • GitHub issues: report bugs or issues with Lantern
  • Need support? Contact support@lantern.dev. We are happy to troubleshoot issues and advise on how to use Lantern for your use case
  • We welcome community contributions! Feel free to open an issue or a PR. If you contact support@lantern.dev, we can find an open issue or project that fits you
项目侧边栏1项目侧边栏2
推荐项目
Project Cover

豆包MarsCode

豆包 MarsCode 是一款革命性的编程助手,通过AI技术提供代码补全、单测生成、代码解释和智能问答等功能,支持100+编程语言,与主流编辑器无缝集成,显著提升开发效率和代码质量。

Project Cover

AI写歌

Suno AI是一个革命性的AI音乐创作平台,能在短短30秒内帮助用户创作出一首完整的歌曲。无论是寻找创作灵感还是需要快速制作音乐,Suno AI都是音乐爱好者和专业人士的理想选择。

Project Cover

有言AI

有言平台提供一站式AIGC视频创作解决方案,通过智能技术简化视频制作流程。无论是企业宣传还是个人分享,有言都能帮助用户快速、轻松地制作出专业级别的视频内容。

Project Cover

Kimi

Kimi AI助手提供多语言对话支持,能够阅读和理解用户上传的文件内容,解析网页信息,并结合搜索结果为用户提供详尽的答案。无论是日常咨询还是专业问题,Kimi都能以友好、专业的方式提供帮助。

Project Cover

阿里绘蛙

绘蛙是阿里巴巴集团推出的革命性AI电商营销平台。利用尖端人工智能技术,为商家提供一键生成商品图和营销文案的服务,显著提升内容创作效率和营销效果。适用于淘宝、天猫等电商平台,让商品第一时间被种草。

Project Cover

吐司

探索Tensor.Art平台的独特AI模型,免费访问各种图像生成与AI训练工具,从Stable Diffusion等基础模型开始,轻松实现创新图像生成。体验前沿的AI技术,推动个人和企业的创新发展。

Project Cover

SubCat字幕猫

SubCat字幕猫APP是一款创新的视频播放器,它将改变您观看视频的方式!SubCat结合了先进的人工智能技术,为您提供即时视频字幕翻译,无论是本地视频还是网络流媒体,让您轻松享受各种语言的内容。

Project Cover

美间AI

美间AI创意设计平台,利用前沿AI技术,为设计师和营销人员提供一站式设计解决方案。从智能海报到3D效果图,再到文案生成,美间让创意设计更简单、更高效。

Project Cover

AIWritePaper论文写作

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

投诉举报邮箱: service@vectorlightyear.com
@2024 懂AI·鲁ICP备2024100362号-6·鲁公网安备37021002001498号