Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Cassandra vector store docs #2903

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```
```
13 changes: 9 additions & 4 deletions langchain/src/vectorstores/cassandra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ export class CassandraStore extends VectorStore {
vector VECTOR<FLOAT, ${this.dimensions}>
);`);

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;
}
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions langchain/src/vectorstores/tests/cassandra.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down