θΏ™ζ˜―indexlocζδΎ›ηš„ζœεŠ‘οΌŒδΈθ¦θΎ“ε…₯任何密码
Skip to content

Conversation

@timothycarambat
Copy link
Member

@timothycarambat timothycarambat commented Jan 13, 2024

Pull Request Type

  • ✨ feat
  • πŸ› fix
  • ♻️ refactor
  • πŸ’„ style
  • πŸ”¨ chore
  • πŸ“ docs

What is in this change?

Improve native embedder to handle large files (>100K words) without memory accrual resulting in OOM on some devices that are resource-constrained.

Additional Information

There could be improvement in other models as well as the native model relies on a local tmp write log for storing the response and other models still keep the embedding array in memory as the iterate.

Note: this doesn't improve the speed at which native embeddings are done, it just keeps very large file embeds from crashing the entire instance.

Developer Validations

  • I ran yarn lint from the root of the repo & committed changes
  • Relevant documentation has been updated
  • I have tested my code functionality
  • Docker build succeeds locally

@review-agent-prime
Copy link

server/utils/EmbeddingEngines/native/index.js

In the #writeToTempfile method, there is a try-catch block that catches an error but does nothing with it. It would be beneficial to log the error for debugging purposes. Also, it would be helpful to log the start and end of the embedding process for better tracking.
Create Issue
See the diff
Checkout the fix

    #writeToTempfile(filePath, data) {
      let fd = 0;
      try {
        fd = fs.openSync(filePath, "a", 0o666);
        let _ = fs.writeSync(fd, data, null, "utf8");
      } catch (e) {
        console.error(`Error writing to tempfile: ${e}`);
      } finally {
        if (fd) fs.closeSync(fd);
      }
    }

    async embedChunks(textChunks = []) {
      console.log(`\x1b[34m[INFO] Starting embedding process...\x1b[0m`);
      ...
      console.log(`\x1b[34m[INFO] Embedding process completed.\x1b[0m`);
    }
git fetch origin && git checkout -b ReviewBot/Impro-e2e95on origin/ReviewBot/Impro-e2e95on

The #writeToTempfile method uses synchronous file operations which can block the event loop and degrade performance. Consider using asynchronous file operations instead.
Create Issue
See the diff
Checkout the fix

    async #writeToTempfile(filePath, data) {
      try {
        await fs.promises.appendFile(filePath, data, { encoding: 'utf8' });
      } catch (e) {
        console.error(`Error writing to tempfile: ${e}`);
      }
    }
git fetch origin && git checkout -b ReviewBot/Impro-e2sno5w origin/ReviewBot/Impro-e2sno5w

@timothycarambat timothycarambat deleted the embedder-improvements branch January 13, 2024 07:15
@timothycarambat timothycarambat restored the embedder-improvements branch January 13, 2024 07:28
@timothycarambat timothycarambat merged commit 4f6d931 into master Jan 13, 2024
@timothycarambat timothycarambat deleted the embedder-improvements branch January 13, 2024 08:32
cabwds pushed a commit to cabwds/anything-llm that referenced this pull request Jul 3, 2025
* improve native embedder handling of large files

* perf changes

* ignore storage tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants