From 29610d3ce1471671273d21399f95fac7ab6ec052 Mon Sep 17 00:00:00 2001 From: Alex Leventer <3254549+alexleventer@users.noreply.github.com> Date: Fri, 13 Oct 2023 12:04:30 -0700 Subject: [PATCH] Improve Cassandra vector store docs (#2903) * Improve cassandra docs * Update index name; * improve code readability * Fix formatting --- .../vectorstores/integrations/cassandra.mdx | 70 +++++++++---------- langchain/src/vectorstores/cassandra.ts | 13 ++-- .../vectorstores/tests/cassandra.int.test.ts | 1 + 3 files changed, 43 insertions(+), 41 deletions(-) diff --git a/docs/extras/modules/data_connection/vectorstores/integrations/cassandra.mdx b/docs/extras/modules/data_connection/vectorstores/integrations/cassandra.mdx index 693d40e0d56b..1ea61f8af664 100644 --- a/docs/extras/modules/data_connection/vectorstores/integrations/cassandra.mdx +++ b/docs/extras/modules/data_connection/vectorstores/integrations/cassandra.mdx @@ -37,47 +37,43 @@ npm install cassandra-driver import { CassandraStore } from "langchain/vectorstores/cassandra"; import { OpenAIEmbeddings } from "langchain/embeddings/openai"; -// text sample from Godel, Escher, Bach + +const config = { + cloud: { + secureConnectBundle: process.env.CASSANDRA_SCB as string, + }, + credentials: { + username: "token", + password: process.env.CASSANDRA_TOKEN as string, + }, + keyspace: "test", + dimensions: 1536, + table: "test", + primaryKey: { + name: "id", + type: "int", + }, + metadataColumns: [ + { + name: "name", + type: "text", + }, + ], + }; + const vectorStore = await CassandraStore.fromTexts( + ["I am blue", "Green yellow purple", "Hello there hello"], [ - "Tortoise: Labyrinth? Labyrinth? Could it Are we in the notorious Little\ - Harmonic Labyrinth of the dreaded Majotaur?", - "Achilles: Yiikes! What is that?", - "Tortoise: They say-although I person never believed it myself-that an I\ - Majotaur has created a tiny labyrinth sits in a pit in the middle of\ - it, waiting innocent victims to get lost in its fears complexity.\ - Then, when they wander and dazed into the center, he laughs and\ - laughs at them-so hard, that he laughs them to death!", - "Achilles: Oh, no!", - "Tortoise: But it's only a myth. Courage, Achilles.", + { id: 2, name: "2" }, + { id: 1, name: "1" }, + { id: 3, name: "3" }, ], - [{ id: 2 }, { id: 1 }, { id: 3 }, { id: 4 }, { id: 5 }], new OpenAIEmbeddings(), - { - collectionName: "goldel_escher_bach", - } + cassandraConfig ); -// or alternatively from docs -const vectorStore = await CassandraStore.fromDocuments(docs, new OpenAIEmbeddings(), { - collectionName: "goldel_escher_bach", -}); - -const response = await vectorStore.similaritySearch("scared", 2); -``` - -## Query docs from existing collection - -```typescript -import { CassandraStore } from "langchain/vectorstores/cassandra"; -import { OpenAIEmbeddings } from "langchain/embeddings/openai"; - -const vectorStore = await CassandraStore.fromExistingCollection( - new OpenAIEmbeddings(), - { - collectionName: "goldel_escher_bach", - } +const results = await vectorStore.similaritySearch( + "Green yellow purple", + 1 ); - -const response = await vectorStore.similaritySearch("scared", 2); -``` +``` \ No newline at end of file diff --git a/langchain/src/vectorstores/cassandra.ts b/langchain/src/vectorstores/cassandra.ts index e191514d70f8..9161fddfd351 100644 --- a/langchain/src/vectorstores/cassandra.ts +++ b/langchain/src/vectorstores/cassandra.ts @@ -195,7 +195,8 @@ export class CassandraStore extends VectorStore { vector VECTOR );`); - await this.client.execute(`CREATE CUSTOM INDEX IF NOT EXISTS ann_index + await this.client + .execute(`CREATE CUSTOM INDEX IF NOT EXISTS idx_vector_${this.table} ON ${this.keyspace}.${this.table}(vector) USING 'StorageAttachedIndex';`); this.isInitialized = true; } @@ -218,9 +219,13 @@ export class CassandraStore extends VectorStore { const metadataColNames = Object.keys(document.metadata); const metadataVals = Object.values(document.metadata); - const query = `INSERT INTO ${this.keyspace}.${this.table} (vector, text${ - metadataColNames.length > 0 ? ", " + metadataColNames.join(", ") : "" - }) VALUES ([${vector}], '${document.pageContent}'${ + const metadataInsert = + metadataColNames.length > 0 ? ", " + metadataColNames.join(", ") : ""; + const query = `INSERT INTO ${this.keyspace}.${ + this.table + } (vector, text${metadataInsert}) VALUES ([${vector}], '${ + document.pageContent + }'${ metadataVals.length > 0 ? ", " + metadataVals diff --git a/langchain/src/vectorstores/tests/cassandra.int.test.ts b/langchain/src/vectorstores/tests/cassandra.int.test.ts index 902b8fa06342..38775d49948d 100644 --- a/langchain/src/vectorstores/tests/cassandra.int.test.ts +++ b/langchain/src/vectorstores/tests/cassandra.int.test.ts @@ -5,6 +5,7 @@ import { CassandraStore } from "../cassandra.js"; import { OpenAIEmbeddings } from "../../embeddings/openai.js"; import { Document } from "../../document.js"; +// yarn test:single /langchain/src/vectorstores/tests/cassandra.int.test.ts describe.skip("CassandraStore", () => { const cassandraConfig = { cloud: {