Skip to content

Commit

Permalink
Add PrismaVectorStore filter IN operator (#3304)
Browse files Browse the repository at this point in the history
* add Prisma filter IN operator

* better naming

* Update langchain/src/vectorstores/prisma.ts

Co-authored-by: Brace Sproul <[email protected]>

* use typeguard

* add operators doc

* add in operator in doc

* typo

* Update langchain/src/vectorstores/prisma.ts

Co-authored-by: Brace Sproul <[email protected]>

* Update examples/src/indexes/vector_stores/prisma_vectorstore/prisma.ts

Co-authored-by: Brace Sproul <[email protected]>

* prettier

* Update prisma.mdx

* Update prisma.mdx

---------

Co-authored-by: Brace Sproul <[email protected]>
Co-authored-by: Jacob Lee <[email protected]>
  • Loading branch information
3 people authored Nov 17, 2023
1 parent 6d91724 commit da38143
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/core_docs/docs/integrations/vectorstores/prisma.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ import Schema from "@examples/indexes/vector_stores/prisma_vectorstore/prisma/sc

<CodeBlock language="typescript">{Example}</CodeBlock>

The following SQL operators are available as filters: `equals`, `in`, `lt`, `lte`, `gt`, `gte`, `not`.

The samples above uses the following schema:

<CodeBlock language="prisma">{Schema}</CodeBlock>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ export const run = async () => {
const resultThree = await vectorStore.similaritySearchWithScore(
"Hello world",
1,
{ content: { equals: "different_content" } }
{
content: { equals: "different_content" },
customForeignKey: { in: ["<id1>", "<id2>", "<id3>"] },
}
);
console.log(resultThree);
};
26 changes: 24 additions & 2 deletions langchain/src/vectorstores/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type ModelColumns<TModel extends Record<string, unknown>> = {
export type PrismaSqlFilter<TModel extends Record<string, unknown>> = {
[K in keyof TModel]?: {
equals?: TModel[K];
in?: TModel[K][];
lt?: TModel[K];
lte?: TModel[K];
gt?: TModel[K];
Expand All @@ -71,6 +72,7 @@ export type PrismaSqlFilter<TModel extends Record<string, unknown>> = {

const OpMap = {
equals: "=",
in: "IN",
lt: "<",
lte: "<=",
gt: ">",
Expand Down Expand Up @@ -410,9 +412,29 @@ export class PrismaVectorStore<
Object.entries(ops).map(([opName, value]) => {
// column name, operators cannot be parametrised
// these fields are thus not escaped by Prisma and can be dangerous if user input is used
const opNameKey = opName as keyof typeof OpMap;
const colRaw = this.Prisma.raw(`"${key}"`);
const opRaw = this.Prisma.raw(OpMap[opName as keyof typeof OpMap]);
return this.Prisma.sql`${colRaw} ${opRaw} ${value}`;
const opRaw = this.Prisma.raw(OpMap[opNameKey]);

switch (OpMap[opNameKey]) {
case OpMap.in: {
if (
!Array.isArray(value) ||
!value.every((v) => typeof v === "string")
) {
throw new Error(
`Invalid filter: IN operator requires an array of strings. Received: ${JSON.stringify(
value,
null,
2
)}`
);
}
return this.Prisma.sql`${colRaw} ${opRaw} (${value.join(",")})`;
}
default:
return this.Prisma.sql`${colRaw} ${opRaw} ${value}`;
}
})
),
" AND ",
Expand Down

1 comment on commit da38143

@vercel
Copy link

@vercel vercel bot commented on da38143 Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

langchainjs-docs – ./docs/core_docs/

langchainjs-docs-langchain.vercel.app
langchainjs-docs-ruddy.vercel.app
langchainjs-docs-git-main-langchain.vercel.app
js.langchain.com

Please sign in to comment.