Skip to content

Commit

Permalink
feat: implement Weaviate Vector Store in TS (run-llama#1109)
Browse files Browse the repository at this point in the history
  • Loading branch information
thucpn authored Aug 12, 2024
1 parent 0664a99 commit c654398
Show file tree
Hide file tree
Showing 9 changed files with 534 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-avocados-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"llamaindex": patch
---

Implement Weaviate Vector Store in TS
31 changes: 31 additions & 0 deletions examples/weaviate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Weaviate Vector Store

Here are two sample scripts which work with loading and querying data from a Weaviate Vector Store.

## Prerequisites

- An Weaviate Vector Database
- Hosted https://weaviate.io/
- Self Hosted https://weaviate.io/developers/weaviate/installation/docker-compose#starter-docker-compose-file
- An OpenAI API Key

## Setup

1. Set your env variables:

- `WEAVIATE_CLUSTER_URL`: Address of your Weaviate Vector Store (like localhost:8080)
- `WEAVIATE_API_KEY`: Your Weaviate API key
- `OPENAI_API_KEY`: Your OpenAI key

2. `cd` Into the `examples` directory
3. run `npm i`

## Load the data

This sample loads the same dataset of movie reviews as sample dataset

run `npx tsx weaviate/load`

## Use RAG to Query the data

run `npx tsx weaviate/query`
23 changes: 23 additions & 0 deletions examples/weaviate/load.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
PapaCSVReader,
storageContextFromDefaults,
VectorStoreIndex,
WeaviateVectorStore,
} from "llamaindex";

const indexName = "MovieReviews";

async function main() {
try {
const reader = new PapaCSVReader(false);
const docs = await reader.loadData("./data/movie_reviews.csv");
const vectorStore = new WeaviateVectorStore({ indexName });
const storageContext = await storageContextFromDefaults({ vectorStore });
await VectorStoreIndex.fromDocuments(docs, { storageContext });
console.log("Successfully loaded data into Weaviate");
} catch (e) {
console.error(e);
}
}

void main();
46 changes: 46 additions & 0 deletions examples/weaviate/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { VectorStoreIndex, WeaviateVectorStore } from "llamaindex";

const indexName = "MovieReviews";

async function main() {
try {
const query = "Get all movie titles.";
const vectorStore = new WeaviateVectorStore({ indexName });
const index = await VectorStoreIndex.fromVectorStore(vectorStore);
const retriever = index.asRetriever({ similarityTopK: 20 });

const queryEngine = index.asQueryEngine({ retriever });
const results = await queryEngine.query({ query });
console.log(`Query from ${results.sourceNodes?.length} nodes`);
console.log(results.response);

console.log("\n=====\nQuerying the index with filters");
const queryEngineWithFilters = index.asQueryEngine({
retriever,
preFilters: {
filters: [
{
key: "document_id",
value: "./data/movie_reviews.csv_37",
operator: "==",
},
{
key: "document_id",
value: "./data/movie_reviews.csv_21",
operator: "==",
},
],
condition: "or",
},
});
const resultAfterFilter = await queryEngineWithFilters.query({
query: "Get all movie titles.",
});
console.log(`Query from ${resultAfterFilter.sourceNodes?.length} nodes`);
console.log(resultAfterFilter.response);
} catch (e) {
console.error(e);
}
}

void main();
1 change: 1 addition & 0 deletions packages/llamaindex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"string-strip-html": "^13.4.8",
"tiktoken": "^1.0.15",
"unpdf": "^0.11.0",
"weaviate-client": "^3.1.4",
"wikipedia": "^2.1.2",
"wink-nlp": "^2.3.0",
"zod": "^3.23.8"
Expand Down
1 change: 1 addition & 0 deletions packages/llamaindex/src/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export { PineconeVectorStore } from "./vectorStore/PineconeVectorStore.js";
export { QdrantVectorStore } from "./vectorStore/QdrantVectorStore.js";
export { SimpleVectorStore } from "./vectorStore/SimpleVectorStore.js";
export * from "./vectorStore/types.js";
export { WeaviateVectorStore } from "./vectorStore/WeaviateVectorStore.js";
Loading

0 comments on commit c654398

Please sign in to comment.