Skip to content

Commit

Permalink
Improve Cassandra vector store docs (#2903)
Browse files Browse the repository at this point in the history
* Improve cassandra docs

* Update index name;

* improve code readability

* Fix formatting
  • Loading branch information
alexleventer authored Oct 13, 2023
1 parent 85586fa commit 29610d3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 41 deletions.
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

1 comment on commit 29610d3

@vercel
Copy link

@vercel vercel bot commented on 29610d3 Oct 13, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.