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: Add support for ChromaDB (run-llama#310)
Co-authored-by: Aarav Navani <38411399+oofmeister27@users.noreply.github.com>
- Loading branch information
1 parent
bb46afe
commit 648482b
Showing
10 changed files
with
353 additions
and
104 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 | ||
--- | ||
|
||
Feat: Add support for Chroma DB as a vector store |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Chroma Vector Store Example | ||
|
||
How to run `examples/chromadb/test.ts`: | ||
|
||
Export your OpenAI API Key using `export OPEN_API_KEY=insert your api key here` | ||
|
||
If you haven't installed chromadb, run `pip install chromadb`. Start the server using `chroma run`. | ||
|
||
Now, open a new terminal window and inside `examples`, run `pnpx ts-node chromadb/test.ts`. | ||
|
||
Here's the output for the input query `Tell me about Godfrey Cheshire's rating of La Sapienza.`: | ||
|
||
`Godfrey Cheshire gave La Sapienza a rating of 4 out of 4, describing it as fresh and the most astonishing and important movie to emerge from France in quite some time.` |
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,40 @@ | ||
import { | ||
ChromaVectorStore, | ||
PapaCSVReader, | ||
storageContextFromDefaults, | ||
VectorStoreIndex, | ||
} from "llamaindex"; | ||
|
||
const collectionName = "movie_reviews"; | ||
|
||
async function main() { | ||
const sourceFile: string = "./data/movie_reviews.csv"; | ||
|
||
try { | ||
console.log(`Loading data from ${sourceFile}`); | ||
const reader = new PapaCSVReader(false, ", ", "\n", { | ||
header: true, | ||
}); | ||
const docs = await reader.loadData(sourceFile); | ||
|
||
console.log("Creating ChromaDB vector store"); | ||
const chromaVS = new ChromaVectorStore({ collectionName }); | ||
const ctx = await storageContextFromDefaults({ vectorStore: chromaVS }); | ||
|
||
console.log("Embedding documents and adding to index"); | ||
const index = await VectorStoreIndex.fromDocuments(docs, { | ||
storageContext: ctx, | ||
}); | ||
|
||
console.log("Querying index"); | ||
const queryEngine = index.asQueryEngine(); | ||
const response = await queryEngine.query( | ||
"Tell me about Godfrey Cheshire's rating of La Sapienza.", | ||
); | ||
console.log(response.toString()); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
main(); |
200 changes: 100 additions & 100 deletions
200
examples/astradb/data/movie_reviews.csv → examples/data/movie_reviews.csv
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
148 changes: 148 additions & 0 deletions
148
packages/core/src/storage/vectorStore/ChromaVectorStore.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,148 @@ | ||
import { | ||
AddParams, | ||
ChromaClient, | ||
ChromaClientParams, | ||
Collection, | ||
IncludeEnum, | ||
QueryResponse, | ||
Where, | ||
WhereDocument, | ||
} from "chromadb"; | ||
import { BaseNode, MetadataMode } from "../../Node"; | ||
import { | ||
VectorStore, | ||
VectorStoreQuery, | ||
VectorStoreQueryMode, | ||
VectorStoreQueryResult, | ||
} from "./types"; | ||
import { metadataDictToNode, nodeToMetadata } from "./utils"; | ||
|
||
type ChromaDeleteOptions = { | ||
where?: Where; | ||
whereDocument?: WhereDocument; | ||
}; | ||
|
||
type ChromaQueryOptions = { | ||
whereDocument?: WhereDocument; | ||
}; | ||
|
||
const DEFAULT_TEXT_KEY = "text"; | ||
|
||
export class ChromaVectorStore implements VectorStore { | ||
storesText: boolean = true; | ||
flatMetadata: boolean = true; | ||
textKey: string; | ||
private chromaClient: ChromaClient; | ||
private collection: Collection | null = null; | ||
private collectionName: string; | ||
|
||
constructor(init: { | ||
collectionName: string; | ||
textKey?: string; | ||
chromaClientParams?: ChromaClientParams; | ||
}) { | ||
this.collectionName = init.collectionName; | ||
this.chromaClient = new ChromaClient(init.chromaClientParams); | ||
this.textKey = init.textKey ?? DEFAULT_TEXT_KEY; | ||
} | ||
|
||
client(): ChromaClient { | ||
return this.chromaClient; | ||
} | ||
|
||
async getCollection(): Promise<Collection> { | ||
if (!this.collection) { | ||
const coll = await this.chromaClient.createCollection({ | ||
name: this.collectionName, | ||
}); | ||
this.collection = coll; | ||
} | ||
return this.collection; | ||
} | ||
|
||
private getDataToInsert(nodes: BaseNode[]): AddParams { | ||
const metadatas = nodes.map((node) => | ||
nodeToMetadata(node, true, this.textKey, this.flatMetadata), | ||
); | ||
return { | ||
embeddings: nodes.map((node) => node.getEmbedding()), | ||
ids: nodes.map((node) => node.id_), | ||
metadatas, | ||
documents: nodes.map((node) => node.getContent(MetadataMode.NONE)), | ||
}; | ||
} | ||
|
||
async add(nodes: BaseNode[]): Promise<string[]> { | ||
if (!nodes || nodes.length === 0) { | ||
return []; | ||
} | ||
|
||
const dataToInsert = this.getDataToInsert(nodes); | ||
const collection = await this.getCollection(); | ||
await collection.add(dataToInsert); | ||
return nodes.map((node) => node.id_); | ||
} | ||
|
||
async delete( | ||
refDocId: string, | ||
deleteOptions?: ChromaDeleteOptions, | ||
): Promise<void> { | ||
const collection = await this.getCollection(); | ||
await collection.delete({ | ||
ids: [refDocId], | ||
where: deleteOptions?.where, | ||
whereDocument: deleteOptions?.whereDocument, | ||
}); | ||
} | ||
|
||
async query( | ||
query: VectorStoreQuery, | ||
options?: ChromaQueryOptions, | ||
): Promise<VectorStoreQueryResult> { | ||
if (query.docIds) { | ||
throw new Error("ChromaDB does not support querying by docIDs"); | ||
} | ||
if (query.mode != VectorStoreQueryMode.DEFAULT) { | ||
throw new Error("ChromaDB does not support querying by mode"); | ||
} | ||
|
||
const chromaWhere: { [x: string]: string | number | boolean } = {}; | ||
if (query.filters) { | ||
query.filters.filters.map((filter) => { | ||
const filterKey = filter.key; | ||
const filterValue = filter.value; | ||
chromaWhere[filterKey] = filterValue; | ||
}); | ||
} | ||
|
||
const collection = await this.getCollection(); | ||
const queryResponse: QueryResponse = await collection.query({ | ||
queryEmbeddings: query.queryEmbedding ?? undefined, | ||
queryTexts: query.queryStr ?? undefined, | ||
nResults: query.similarityTopK, | ||
where: Object.keys(chromaWhere).length ? chromaWhere : undefined, | ||
whereDocument: options?.whereDocument, | ||
//ChromaDB doesn't return the result embeddings by default so we need to include them | ||
include: [ | ||
IncludeEnum.Distances, | ||
IncludeEnum.Metadatas, | ||
IncludeEnum.Documents, | ||
IncludeEnum.Embeddings, | ||
], | ||
}); | ||
const vectorStoreQueryResult: VectorStoreQueryResult = { | ||
nodes: queryResponse.ids[0].map((id, index) => { | ||
const text = (queryResponse.documents as string[][])[0][index]; | ||
const metaData = queryResponse.metadatas[0][index] ?? {}; | ||
const node = metadataDictToNode(metaData); | ||
node.setContent(text); | ||
return node; | ||
}), | ||
similarities: (queryResponse.distances as number[][])[0].map( | ||
(distance) => 1 - distance, | ||
), | ||
ids: queryResponse.ids[0], | ||
}; | ||
return vectorStoreQueryResult; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.