自动化研究产物 — 非上游项目。
本仓库是由自动化工具链构建的一次性实验环境,用于拉瓦尔大学(Université Laval)硕士论文中复现已公开的 GitHub Actions 工作流漏洞。它是 timescale/pgai 在提交 2a209b058c60823b57e8b7775b10244ff340eb8b(2025-05-14)时的逐字快照,依据该项目自身的许可证重新分发,该许可证文件已原样包含在本快照中。
上游项目未参与其中,也从未成为攻击目标,此处研究的漏洞已公开。本仓库中的每个密钥和变量均为随机生成的虚拟值——不包含任何真实凭据。操作引用和运行器镜像均固定为其在 2025-05-14 解析到的版本;有关对快照所做的每项更改,请参阅工具链输出中的 pinning.md。
如有疑问或异议:[email protected]
借助 PostgreSQL 为您的 RAG 与 Agentic 应用赋能
一个 Python 库,可将 PostgreSQL 转变为适用于 RAG 和 Agentic 应用的稳健、生产级检索引擎。
-
🔄 自动从 PostgreSQL 数据和 S3 文档创建并同步向量嵌入。嵌入会随数据变化自动更新。
-
🔍 借助 pgvector 和 pgvectorscale 实现强大的向量与语义搜索。
-
🛡️ 开箱即用、生产就绪:支持批量处理以高效生成嵌入,并内置对模型故障、速率限制和延迟突增的处理机制。
-
🐘 适用于任何 PostgreSQL 数据库,包括 Timescale Cloud、Amazon RDS、Supabase 等。
基本架构:
该系统由您编写的应用程序、一个 PostgreSQL 数据库以及无状态向量化工作进程组成。应用程序定义向量化配置,以从 PostgreSQL 或 S3 等来源嵌入数据。工作进程读取该配置,将数据队列处理为嵌入和分块文本,并将结果写回。随后,应用程序查询这些数据,为 RAG 和语义搜索提供支持。
该架构的关键优势在于其韧性:应用程序所做的数据修改与嵌入过程解耦,确保嵌入服务的故障不会影响核心数据操作。
安装
首先,安装 pgai 包。```
pip install pgai
然后,安装 pgai 数据库组件。你可以通过终端使用 CLI 来完成此操作,也可以在 Python 应用程序代码中使用 pgai Python 包来完成。```
# from the cli
pgai install -d <database-url>
# or from the python package, often done as part of your application setup
import pgai
pgai.install(DB_URL)
快速入门
本快速入门演示了 pgai Vectorizer 如何通过自动创建和同步嵌入(embeddings),在数据发生变化时实现对 PostgreSQL 数据的语义搜索和 RAG。
pgai Vectorizer 的关键“秘诀”在于其声明式的嵌入生成方法。只需定义你的流水线,让 Vectorizer 处理保持嵌入同步的运维复杂性,即使嵌入端点不可靠也能正常工作。你可以按如下方式定义流水线的简单版本:```sql
CREATE TABLE IF NOT EXISTS wiki (
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
url TEXT NOT NULL,
title TEXT NOT NULL,
text TEXT NOT NULL
)
SELECT ai.create_vectorizer(
'wiki'::regclass,
loading => ai.loading_column(column_name=>'text'),
destination => ai.destination_table(target_table=>'wiki_embedding_storage'),
embedding => ai.embedding_openai(model=>'text-embedding-ada-002', dimensions=>'1536')
)
向量化器会自动为`wiki`表中的所有行创建嵌入,更重要的是,它会随着底层数据的变化保持嵌入的同步更新。**可以把它想象成在`wiki`表上声明一个索引**,只不过不是由数据库替你管理索引数据结构,而是由Vectorizer来管理嵌入。
## 运行快速入门
**前提条件:**
- 一个PostgreSQL数据库([Docker安装说明](https://docs.timescale.com/self-hosted/latest/install/installation-docker/))。
- 一个OpenAI API密钥(快速入门中我们使用openai进行嵌入,但你也可以使用[多种提供商](#supported-embedding-models))。
创建一个包含以下内容的`.env`文件:```
OPENAI_API_KEY=<your-openai-api-key>
DB_URL=<your-database-url>
你可以从快速入门示例中下载完整的 python 代码 和 requirements.txt,并在与 .env 文件相同的目录下运行它。
点击此处获取用于运行快速入门的 bash 脚本
```bash
curl -O https://raw.githubusercontent.com/timescale/pgai/main/examples/quickstart/main.py
curl -O https://raw.githubusercontent.com/timescale/pgai/main/examples/quickstart/requirements.txt
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python main.py
```
示例输出:
点击展开示例输出
```
Search results 1:
[WikiSearchResult(id=7,
url='https://en.wikipedia.org/wiki/Aristotle',
title='Aristotle',
text='Aristotle (; Aristotélēs, ; 384–322\xa0BC) was an '
'Ancient Greek philosopher and polymath. His writings '
'cover a broad range of subjects spanning the natural '
'sciences, philosophy, linguistics, economics, '
'politics, psychology and the arts. As the founder of '
'the Peripatetic school of philosophy in the Lyceum in '
'Athens, he began the wider Aristotelian tradition that '
'followed, which set the groundwork for the development '
'of modern science.\n'
'\n'
"Little is known about Aristotle's life. He was born in "
'the city of Stagira in northern Greece during the '
'Classical period. His father, Nicomachus, died when '
'Aristotle was a child, and he was brought up by a '
"guardian. At 17 or 18 he joined Plato's Academy in "
'Athens and remained there till the age of 37 (). '
'Shortly after Plato died, Aristotle left Athens and, '
'at the request of Philip II of Macedon, tutored his '
'son Alexander the Great beginning in 343 BC. He '
'established a library in the Lyceum which helped him '
'to produce many of his hundreds of books on papyru',
chunk='Aristotle (; Aristotélēs, ; 384–322\xa0BC) was an '
'Ancient Greek philosopher and polymath. His writings '
'cover a broad range of subjects spanning the natural '
'sciences, philosophy, linguistics, economics, '
'politics, psychology and the arts. As the founder of '
'the Peripatetic school of philosophy in the Lyceum in '
'Athens, he began the wider Aristotelian tradition '
'that followed, which set the groundwork for the '
'development of modern science.',
distance=0.22242502364217387)]
Search results 2:
[WikiSearchResult(id=41,
url='https://en.wikipedia.org/wiki/pgai',
title='pgai',
text='pgai is a Python library that turns PostgreSQL into '
'the retrieval engine behind robust, production-ready '
'RAG and Agentic applications. It does this by '
'automatically creating vector embeddings for your data '
'based on the vectorizer you define.',
chunk='pgai is a Python library that turns PostgreSQL into '
'the retrieval engine behind robust, production-ready '
'RAG and Agentic applications. It does this by '
'automatically creating vector embeddings for your '
'data based on the vectorizer you define.',
distance=0.13639101792546204)]
RAG response:
The main thing pgai does right now is generating vector embeddings for data in PostgreSQL databases based on the vectorizer defined by the user, enabling the creation of robust RAG and Agentic applications.
```
代码走查
安装 pgai 数据库组件