-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Add built-in embedding engine into AnythingLLM #411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6c63918
Implement use of native embedder (all-Mini-L6-v2)
timothycarambat f038a65
Add native embedder as an available embedder selection
timothycarambat ab451f8
wrap model loader in try/catch
timothycarambat 48680a6
Merge branch 'master' of github.com:Mintplex-Labs/anything-llm into b…
timothycarambat 0618196
print progress on download
timothycarambat f7bc1fc
Update to progress output for embedder
timothycarambat e3bfaf9
move embedder selection options to component
timothycarambat 5b62ff9
forgot import
timothycarambat af22665
add Data privacy alert updates for local embedder
timothycarambat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
frontend/src/components/EmbeddingSelection/NativeEmbeddingOptions/index.jsx
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.