-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add built-in embedding engine into AnythingLLM (#411)
* Implement use of native embedder (all-Mini-L6-v2) stop showing prisma queries during dev * Add native embedder as an available embedder selection * wrap model loader in try/catch * print progress on download * Update to progress output for embedder * move embedder selection options to component * forgot import * add Data privacy alert updates for local embedder
- Loading branch information
1 parent
48764d6
commit 88cdd8c
Showing
14 changed files
with
517 additions
and
27 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
frontend/src/components/EmbeddingSelection/NativeEmbeddingOptions/index.jsx
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,10 @@ | ||
export default function NativeEmbeddingOptions() { | ||
return ( | ||
<div className="w-full h-20 items-center justify-center flex"> | ||
<p className="text-sm font-base text-white text-opacity-60"> | ||
There is no set up required when using AnythingLLM's native embedding | ||
engine. | ||
</p> | ||
</div> | ||
); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Xenova | ||
downloaded/* |
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 @@ | ||
## Native models used by AnythingLLM | ||
|
||
This folder is specifically created as a local cache and storage folder that is used for native models that can run on a CPU. | ||
|
||
Currently, AnythingLLM uses this folder for the following parts of the application. | ||
|
||
### Embedding | ||
When your embedding engine preference is `native` we will use the ONNX **all-MiniLM-L6-v2** model built by [Xenova on HuggingFace.co](https://huggingface.co/Xenova/all-MiniLM-L6-v2). This model is a quantized and WASM version of the popular [all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2) which produces a 384-dimension vector. | ||
|
||
If you are using the `native` embedding engine your vector database should be configured to accept 384-dimension models if that parameter is directly editable (Pinecone only). | ||
|
||
### Text generation (LLM selection) | ||
_in progress_ |
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,80 @@ | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
const { toChunks } = require("../../helpers"); | ||
|
||
class NativeEmbedder { | ||
constructor() { | ||
this.model = "Xenova/all-MiniLM-L6-v2"; | ||
this.cacheDir = path.resolve( | ||
process.env.STORAGE_DIR | ||
? path.resolve(process.env.STORAGE_DIR, `models`) | ||
: path.resolve(__dirname, `../../../storage/models`) | ||
); | ||
this.modelPath = path.resolve(this.cacheDir, "Xenova", "all-MiniLM-L6-v2"); | ||
|
||
// Limit the number of chunks to send per loop to not overload compute. | ||
this.embeddingChunkLimit = 16; | ||
|
||
// Make directory when it does not exist in existing installations | ||
if (!fs.existsSync(this.cacheDir)) fs.mkdirSync(this.cacheDir); | ||
} | ||
|
||
async embedderClient() { | ||
if (!fs.existsSync(this.modelPath)) { | ||
console.log( | ||
"\x1b[34m[INFO]\x1b[0m The native embedding model has never been run and will be downloaded right now. Subsequent runs will be faster. (~23MB)\n\n" | ||
); | ||
} | ||
|
||
try { | ||
// Convert ESM to CommonJS via import so we can load this library. | ||
const pipeline = (...args) => | ||
import("@xenova/transformers").then(({ pipeline }) => | ||
pipeline(...args) | ||
); | ||
return await pipeline("feature-extraction", this.model, { | ||
cache_dir: this.cacheDir, | ||
...(!fs.existsSync(this.modelPath) | ||
? { | ||
// Show download progress if we need to download any files | ||
progress_callback: (data) => { | ||
if (!data.hasOwnProperty("progress")) return; | ||
console.log( | ||
`\x1b[34m[Embedding - Downloading Model Files]\x1b[0m ${ | ||
data.file | ||
} ${~~data?.progress}%` | ||
); | ||
}, | ||
} | ||
: {}), | ||
}); | ||
} catch (error) { | ||
console.error("Failed to load the native embedding model:", error); | ||
throw error; | ||
} | ||
} | ||
|
||
async embedTextInput(textInput) { | ||
const result = await this.embedChunks(textInput); | ||
return result?.[0] || []; | ||
} | ||
|
||
async embedChunks(textChunks = []) { | ||
const Embedder = await this.embedderClient(); | ||
const embeddingResults = []; | ||
for (const chunk of toChunks(textChunks, this.embeddingChunkLimit)) { | ||
const output = await Embedder(chunk, { | ||
pooling: "mean", | ||
normalize: true, | ||
}); | ||
if (output.length === 0) continue; | ||
embeddingResults.push(output.tolist()); | ||
} | ||
|
||
return embeddingResults.length > 0 ? embeddingResults.flat() : null; | ||
} | ||
} | ||
|
||
module.exports = { | ||
NativeEmbedder, | ||
}; |
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
Oops, something went wrong.