Prisma
为了使用向量搜索增强 PostgreSQL 数据库中的现有模型,Langchain 支持将 Prisma 与 PostgreSQL 和 pgvector
Postgres 扩展一起使用。
¥For augmenting existing models in PostgreSQL database with vector search, Langchain supports using Prisma together with PostgreSQL and pgvector
Postgres extension.
设置
¥Setup
使用 Supabase 设置数据库实例
¥Setup database instance with Supabase
请参阅 Prisma 和 Supabase 集成指南 使用 Supabase 和 Prisma 设置新的数据库实例。
¥Refer to the Prisma and Supabase integration guide to setup a new database instance with Supabase and Prisma.
安装 Prisma
¥Install Prisma
- npm
- Yarn
- pnpm
npm install prisma
yarn add prisma
pnpm add prisma
使用 docker-compose
设置 pgvector
自托管实例
¥Setup pgvector
self hosted instance with docker-compose
pgvector
提供了一个预构建的 Docker 镜像,可用于快速设置自托管的 Postgres 实例。
¥pgvector
provides a prebuilt Docker image that can be used to quickly setup a self-hosted Postgres instance.
services:
db:
image: ankane/pgvector
ports:
- 5432:5432
volumes:
- db:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=
- POSTGRES_USER=
- POSTGRES_DB=
volumes:
db:
创建新模式
¥Create a new schema
假设你尚未创建模式,请创建一个包含 vector
字段和 Unsupported("vector")
类型的新模型:
¥Assuming you haven't created a schema yet, create a new model with a vector
field of type Unsupported("vector")
:
model Document {
id String @id @default(cuid())
content String
vector Unsupported("vector")?
}
之后,创建一个包含 --create-only
的新迁移,以避免直接运行迁移。
¥Afterwards, create a new migration with --create-only
to avoid running the migration directly.
- npm
- Yarn
- pnpm
npx prisma migrate dev --create-only
npx prisma migrate dev --create-only
npx prisma migrate dev --create-only
如果尚未启用 pgvector
扩展,请将以下行添加到新创建的迁移中以启用它:
¥Add the following line to the newly created migration to enable pgvector
extension if it hasn't been enabled yet:
CREATE EXTENSION IF NOT EXISTS vector;
之后运行迁移:
¥Run the migration afterwards:
- npm
- Yarn
- pnpm
npx prisma migrate dev
npx prisma migrate dev
npx prisma migrate dev
用法
¥Usage
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community @langchain/core
yarn add @langchain/openai @langchain/community @langchain/core
pnpm add @langchain/openai @langchain/community @langchain/core
表名和列名(例如 tableName
、vectorColumnName
、columns
和 filter
等字段)直接传递到 SQL 查询中,无需参数化。这些字段必须事先进行清理,以避免 SQL 注入。
¥Table names and column names (in fields such as tableName
, vectorColumnName
, columns
and filter
) are passed into SQL queries directly without parametrisation.
These fields must be sanitized beforehand to avoid SQL injection.
import { PrismaVectorStore } from "@langchain/community/vectorstores/prisma";
import { OpenAIEmbeddings } from "@langchain/openai";
import { PrismaClient, Prisma, Document } from "@prisma/client";
export const run = async () => {
const db = new PrismaClient();
// Use the `withModel` method to get proper type hints for `metadata` field:
const vectorStore = PrismaVectorStore.withModel<Document>(db).create(
new OpenAIEmbeddings(),
{
prisma: Prisma,
tableName: "Document",
vectorColumnName: "vector",
columns: {
id: PrismaVectorStore.IdColumn,
content: PrismaVectorStore.ContentColumn,
},
}
);
const texts = ["Hello world", "Bye bye", "What's this?"];
await vectorStore.addModels(
await db.$transaction(
texts.map((content) => db.document.create({ data: { content } }))
)
);
const resultOne = await vectorStore.similaritySearch("Hello world", 1);
console.log(resultOne);
// create an instance with default filter
const vectorStore2 = PrismaVectorStore.withModel<Document>(db).create(
new OpenAIEmbeddings(),
{
prisma: Prisma,
tableName: "Document",
vectorColumnName: "vector",
columns: {
id: PrismaVectorStore.IdColumn,
content: PrismaVectorStore.ContentColumn,
},
filter: {
content: {
equals: "default",
},
},
}
);
await vectorStore2.addModels(
await db.$transaction(
texts.map((content) => db.document.create({ data: { content } }))
)
);
// Use the default filter a.k.a {"content": "default"}
const resultTwo = await vectorStore.similaritySearch("Hello world", 1);
console.log(resultTwo);
};
API Reference:
- PrismaVectorStore from
@langchain/community/vectorstores/prisma
- OpenAIEmbeddings from
@langchain/openai
以下 SQL 运算符可用作过滤器:equals
, in
, isNull
, isNotNull
, like
, lt
, lte
, gt
, gte
, not
.
¥The following SQL operators are available as filters: equals
, in
, isNull
, isNotNull
, like
, lt
, lte
, gt
, gte
, not
.
以上示例使用以下架构:
¥The samples above uses the following schema:
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Document {
id String @id @default(cuid())
content String
namespace String? @default("default")
vector Unsupported("vector")?
}
API Reference:
如果你不需要 namespace
,可以将其删除。
¥You can remove namespace
if you don't need it.
相关
¥Related
向量存储 概念指南
¥Vector store conceptual guide
向量存储 操作指南
¥Vector store how-to guides