forked from run-llama/LlamaIndexTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement Weaviate Vector Store in TS (run-llama#1109)
- Loading branch information
Showing
9 changed files
with
534 additions
and
14 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
"llamaindex": patch | ||
--- | ||
|
||
Implement Weaviate Vector Store in TS |
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,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` |
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,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(); |
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,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(); |
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
Oops, something went wrong.