-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Tigris Vector Store * review feedback * Skip integration test, small docs update * Remove unused example * Revert build artifact change * Remove build artifact --------- Co-authored-by: Ovais Tariq <ot@tigrisdata.com> Co-authored-by: Ovais Tariq <ovaistariq@gmail.com>
- Loading branch information
1 parent
da6320a
commit dc948fc
Showing
14 changed files
with
459 additions
and
11 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
docs/docs/modules/indexes/vector_stores/integrations/tigris.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
sidebar_class_name: node-only | ||
--- | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
|
||
# Tigris | ||
|
||
Tigris makes it easy to build AI applications with vector embeddings. | ||
It is a fully managed cloud-native database that allows you store and | ||
index documents and vector embeddings for fast and scalable vector search. | ||
|
||
:::tip Compatibility | ||
Only available on Node.js. | ||
::: | ||
|
||
## Setup | ||
|
||
### 1. Install the Tigris SDK | ||
|
||
Install the SDK as follows | ||
|
||
```bash npm2yarn | ||
npm install -S @tigrisdata/vector | ||
``` | ||
|
||
### 2. Fetch Tigris API credentials | ||
|
||
You can sign up for a free Tigris account [here](https://console.preview.tigrisdata.cloud/signup). | ||
|
||
Once you have signed up for the Tigris account, create a new project called `vectordemo`. | ||
Next, make a note of the `clientId` and `clientSecret`, which you can get from the | ||
Application Keys section of the project. | ||
|
||
## Index docs | ||
|
||
import FromDocs from "@examples/indexes/vector_stores/tigris/fromDocs.ts"; | ||
|
||
<CodeBlock language="typescript">{FromDocs}</CodeBlock> | ||
|
||
## Query docs | ||
|
||
import Search from "@examples/indexes/vector_stores/tigris/search.ts"; | ||
|
||
<CodeBlock language="typescript">{Search}</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { VectorDocumentStore } from "@tigrisdata/vector"; | ||
import { Document } from "langchain/document"; | ||
import { OpenAIEmbeddings } from "langchain/embeddings/openai"; | ||
import { TigrisVectorStore } from "langchain/vectorstores/tigris"; | ||
|
||
const index = new VectorDocumentStore({ | ||
connection: { | ||
serverUrl: "api.preview.tigrisdata.cloud", | ||
projectName: process.env.TIGRIS_PROJECT, | ||
clientId: process.env.TIGRIS_CLIENT_ID, | ||
clientSecret: process.env.TIGRIS_CLIENT_SECRET, | ||
}, | ||
indexName: "examples_index", | ||
numDimensions: 1536, // match the OpenAI embedding size | ||
}); | ||
|
||
const docs = [ | ||
new Document({ | ||
metadata: { foo: "bar" }, | ||
pageContent: "tigris is a cloud-native vector db", | ||
}), | ||
new Document({ | ||
metadata: { foo: "bar" }, | ||
pageContent: "the quick brown fox jumped over the lazy dog", | ||
}), | ||
new Document({ | ||
metadata: { baz: "qux" }, | ||
pageContent: "lorem ipsum dolor sit amet", | ||
}), | ||
new Document({ | ||
metadata: { baz: "qux" }, | ||
pageContent: "tigris is a river", | ||
}), | ||
]; | ||
|
||
await TigrisVectorStore.fromDocuments(docs, new OpenAIEmbeddings(), { index }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { VectorDocumentStore } from "@tigrisdata/vector"; | ||
import { OpenAIEmbeddings } from "langchain/embeddings/openai"; | ||
import { TigrisVectorStore } from "langchain/vectorstores/tigris"; | ||
|
||
const index = new VectorDocumentStore({ | ||
connection: { | ||
serverUrl: "api.preview.tigrisdata.cloud", | ||
projectName: process.env.TIGRIS_PROJECT, | ||
clientId: process.env.TIGRIS_CLIENT_ID, | ||
clientSecret: process.env.TIGRIS_CLIENT_SECRET, | ||
}, | ||
indexName: "examples_index", | ||
numDimensions: 1536, // match the OpenAI embedding size | ||
}); | ||
|
||
const vectorStore = await TigrisVectorStore.fromExistingIndex( | ||
new OpenAIEmbeddings(), | ||
{ index } | ||
); | ||
|
||
/* Search the vector DB independently with metadata filters */ | ||
const results = await vectorStore.similaritySearch("tigris", 1, { | ||
"metadata.foo": "bar", | ||
}); | ||
console.log(JSON.stringify(results, null, 2)); | ||
/* | ||
[ | ||
Document { | ||
pageContent: 'tigris is a cloud-native vector db', | ||
metadata: { foo: 'bar' } | ||
} | ||
] | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* eslint-disable no-process-env */ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { beforeEach, describe, expect, test } from "@jest/globals"; | ||
import { faker } from "@faker-js/faker"; | ||
import { VectorDocumentStore } from "@tigrisdata/vector"; | ||
import * as uuid from "uuid"; | ||
import { Document } from "../../document.js"; | ||
import { OpenAIEmbeddings } from "../../embeddings/openai.js"; | ||
import { TigrisVectorStore } from "../tigris.js"; | ||
|
||
describe.skip("TigrisVectorStore", () => { | ||
let tigrisStore: TigrisVectorStore; | ||
|
||
beforeEach(async () => { | ||
const client = new VectorDocumentStore({ | ||
connection: { | ||
serverUrl: process.env.TIGRIS_URI, | ||
projectName: process.env.TIGRIS_PROJECT, | ||
clientId: process.env.TIGRIS_CLIENT_ID, | ||
clientSecret: process.env.TIGRIS_CLIENT_SECRET, | ||
}, | ||
indexName: "integration_test_index", | ||
numDimensions: 1536, | ||
}); | ||
|
||
const embeddings = new OpenAIEmbeddings(); | ||
tigrisStore = new TigrisVectorStore(embeddings, { index: client }); | ||
}); | ||
|
||
test("user-provided ids", async () => { | ||
const documentId = uuid.v4(); | ||
const pageContent = faker.lorem.sentence(5); | ||
|
||
await tigrisStore.addDocuments( | ||
[{ pageContent, metadata: {} }], | ||
[documentId] | ||
); | ||
|
||
const results = await tigrisStore.similaritySearch(pageContent, 1); | ||
|
||
expect(results).toEqual([new Document({ metadata: {}, pageContent })]); | ||
}); | ||
|
||
test("auto-generated ids", async () => { | ||
const pageContent = faker.lorem.sentence(5); | ||
|
||
await tigrisStore.addDocuments([{ pageContent, metadata: { foo: "bar" } }]); | ||
|
||
const results = await tigrisStore.similaritySearch(pageContent, 1); | ||
|
||
expect(results).toEqual([ | ||
new Document({ metadata: { foo: "bar" }, pageContent }), | ||
]); | ||
}); | ||
|
||
test("metadata filtering", async () => { | ||
const pageContent = faker.lorem.sentence(5); | ||
const id = uuid.v4(); | ||
|
||
await tigrisStore.addDocuments([ | ||
{ pageContent, metadata: { foo: "bar" } }, | ||
{ pageContent, metadata: { foo: id } }, | ||
{ pageContent, metadata: { foo: "qux" } }, | ||
]); | ||
|
||
// If the filter wasn't working, we'd get all 3 documents back | ||
const results = await tigrisStore.similaritySearch(pageContent, 3, { | ||
"metadata.foo": id, | ||
}); | ||
|
||
expect(results).toEqual([ | ||
new Document({ metadata: { foo: id }, pageContent }), | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { jest, test, expect } from "@jest/globals"; | ||
import { FakeEmbeddings } from "../../embeddings/fake.js"; | ||
|
||
import { TigrisVectorStore } from "../tigris.js"; | ||
|
||
test("TigrisVectorStore with external ids", async () => { | ||
const client = { | ||
addDocumentsWithVectors: jest.fn(), | ||
similaritySearchVectorWithScore: jest | ||
.fn() | ||
.mockImplementation(async () => []), | ||
}; | ||
const embeddings = new FakeEmbeddings(); | ||
|
||
const store = new TigrisVectorStore(embeddings, { | ||
index: client as any, | ||
}); | ||
|
||
expect(store).toBeDefined(); | ||
|
||
await store.addDocuments( | ||
[ | ||
{ | ||
pageContent: "hello", | ||
metadata: { | ||
a: 1, | ||
b: { nested: [1, { a: 4 }] }, | ||
}, | ||
}, | ||
], | ||
["id1"] | ||
); | ||
|
||
expect(client.addDocumentsWithVectors).toHaveBeenCalledTimes(1); | ||
|
||
expect(client.addDocumentsWithVectors).toHaveBeenCalledWith({ | ||
ids: ["id1"], | ||
embeddings: [[0.1, 0.2, 0.3, 0.4]], | ||
documents: [ | ||
{ | ||
content: "hello", | ||
metadata: { | ||
a: 1, | ||
b: { nested: [1, { a: 4 }] }, | ||
}, | ||
}, | ||
], | ||
}); | ||
|
||
const results = await store.similaritySearch("hello", 1); | ||
|
||
expect(results).toHaveLength(0); | ||
}); | ||
|
||
test("TigrisVectorStore with generated ids", async () => { | ||
const client = { | ||
addDocumentsWithVectors: jest.fn(), | ||
similaritySearchVectorWithScore: jest | ||
.fn() | ||
.mockImplementation(async () => []), | ||
}; | ||
const embeddings = new FakeEmbeddings(); | ||
|
||
const store = new TigrisVectorStore(embeddings, { index: client as any }); | ||
|
||
expect(store).toBeDefined(); | ||
|
||
await store.addDocuments([{ pageContent: "hello", metadata: { a: 1 } }]); | ||
|
||
expect(client.addDocumentsWithVectors).toHaveBeenCalledTimes(1); | ||
|
||
const results = await store.similaritySearch("hello", 1); | ||
|
||
expect(results).toHaveLength(0); | ||
}); |
Oops, something went wrong.
dc948fc
There was a problem hiding this comment.
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 – ./
langchainjs-docs-ruddy.vercel.app
langchainjs-docs-git-main-langchain.vercel.app
js.langchain.com
langchainjs-docs-langchain.vercel.app