Skip to content
AI & Vectors

Python 客户端

Manage unstructured vector stores in Postgres.

Supabase 提供了一个名为 vecs 的 Python 客户端,用于管理非结构化向量存储。这个客户端提供了一套实用工具,可以使用 pgvector 扩展在 Postgres 中创建和查询集合。

🌐 Supabase provides a Python client called vecs for managing unstructured vector stores. This client provides a set of useful tools for creating and querying collections in Postgres using the pgvector extension.

快速开始 #

🌐 Quick start

要了解 Vecs 是如何工作的,请使用本地数据库。确保你的电脑上已经安装了 Supabase CLI 安装指南

🌐 To see how Vecs works, use a local database. Make sure you have the Supabase CLI installed on your machine.

初始化你的项目 #

🌐 Initialize your project

使用 initstart 命令在任意文件夹中启动本地 Postgres 实例。确保 Docker 正在运行!

🌐 Start a local Postgres instance in any folder using the init and start commands. Make sure you have Docker running!

1
# Initialize your project
2
supabase init
3
4
# Start Postgres
5
supabase start

创建一个收藏 #

🌐 Create a collection

在 Python 解释器里,运行以下命令来创建一个名为“docs”的新集合,具有 3 个维度。

🌐 Inside a Python shell, run the following commands to create a new collection called "docs", with 3 dimensions.

1
import vecs
2
3
# create vector store client
4
vx = vecs.create_client("postgresql://postgres:postgres@localhost:54322/postgres")
5
6
# create a collection of vectors with 3 dimensions
7
docs = vx.get_or_create_collection(name="docs", dimension=3)

添加嵌入 #

🌐 Add embeddings

现在我们可以使用 upsert() 命令向我们的“docs”集合中插入一些嵌入:

🌐 Now we can insert some embeddings into our "docs" collection using the upsert() command:

1
import vecs
2
3
# create vector store client
4
docs = vecs.get_or_create_collection(name="docs", dimension=3)
5
6
# a collection of vectors with 3 dimensions
7
vectors=[
8
("vec0", [0.1, 0.2, 0.3], {"year": 1973}),
9
("vec1", [0.7, 0.8, 0.9], {"year": 2012})
10
]
11
12
# insert our vectors
13
docs.upsert(vectors=vectors)

查询集合 #

🌐 Query the collection

你现在可以查询这个集合来获取相关匹配项:

🌐 You can now query the collection to retrieve a relevant match:

1
import vecs
2
3
docs = vecs.get_or_create_collection(name="docs", dimension=3)
4
5
# query the collection filtering metadata for "year" = 2012
6
docs.query(
7
data=[0.4,0.5,0.6], # required
8
limit=1, # number of records to return
9
filters={"year": {"$eq": 2012}}, # metadata filters
10
)

深入探讨 #

🌐 Deep dive

想要了解更多关于 vecs 收藏的详细指南,请查看 API

🌐 For a more in-depth guide on vecs collections, see API.

资源 #

🌐 Resources