Skip to content
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

mongodb: [performance] Increase DEFAULT_INSERT_BATCH_SIZE to 100,000 and introduce sizing constraints #19608

Merged
merged 7 commits into from
May 14, 2024

Conversation

Jibola
Copy link
Contributor

@Jibola Jibola commented Mar 26, 2024

Thank you for contributing to LangChain!

Increase DEFAULT_INSERT_BATCH_SIZE to 100,000 and introduce sizing constraints

  • [x]

    • Description: Increasing our default batch size to improve performance
    • Issue: N/A
    • Dependencies: None
    • Twitter handle: @mongodb
  • Lint and test: Run make format, make lint and make test from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/

Additional guidelines:

  • Make sure optional dependencies are imported within a function.
  • Please do not add dependencies to pyproject.toml files (even optional ones) unless they are required for unit tests.
  • Most PRs should not touch more than one package.
  • Changes should be backwards compatible.
  • If you are adding something to community, do not re-import it in langchain.

If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, hwchase17.

@efriis efriis added the partner label Mar 26, 2024
@efriis efriis self-assigned this Mar 26, 2024
Copy link

vercel bot commented Mar 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview May 14, 2024 10:10pm

if texts_batch:
result_ids.extend(self._insert_texts(texts_batch, metadatas_batch))
result_ids.extend(self._insert_texts(texts_batch, metadatas_batch)) # type: ignore
Copy link

@ShaneHarvey ShaneHarvey Mar 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one concern here is what happens when someone calls add_texts() with a generator that yields a large amount of data. With this new code we'll inflate the entire thing into memory which can degrade performance. For example:

add_texts((random_large_string() for i in range(1_000_000))

In this case it would be best to limit the batches roughly based on the string size in addition to batch_size. We could do that by tracking the sum total of the batch's text:

                size += len(text)
                texts_batch.append(text)
                metadatas_batch.append(metadata)
                if (i + 1) % batch_size == 0 or size >= 47_000_000:

47MB is the maxMessageSizeBytes that pymongo can batch in one message minus 1MB of overhead to account for other bson data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's fine with me. We'll re-introduce BATCH_SIZE as 100_000 and also have an additional bounding of 47_000_000 on the text aggregate text length. In this case, then, should we also include the length of the accompanying metadata?

@Jibola Jibola marked this pull request as ready for review April 30, 2024 17:16
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Apr 30, 2024
@dosubot dosubot bot added Ɑ: vector store Related to vector store module 🔌: mongo Primarily related to Mongo integrations 🤖:improvement Medium size change to existing code to handle new use-cases labels Apr 30, 2024
@Jibola Jibola changed the title langchain-mongodb: [performance] Remove DEFAULT_INSERT_BATCH_SIZE langchain-mongodb: [performance] Increase DEFAULT_INSERT_BATCH_SIZE to 100,000 and introduce sizing constraints Apr 30, 2024
@Jibola Jibola requested a review from ShaneHarvey May 2, 2024 15:22
Copy link

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@dosubot dosubot bot added the lgtm PR looks good. Use to confirm that a PR is ready for merging. label May 14, 2024
@efriis efriis changed the title langchain-mongodb: [performance] Increase DEFAULT_INSERT_BATCH_SIZE to 100,000 and introduce sizing constraints mongodb: [performance] Increase DEFAULT_INSERT_BATCH_SIZE to 100,000 and introduce sizing constraints May 14, 2024
@efriis efriis enabled auto-merge (squash) May 14, 2024 22:10
@efriis efriis merged commit f369495 into langchain-ai:master May 14, 2024
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:improvement Medium size change to existing code to handle new use-cases lgtm PR looks good. Use to confirm that a PR is ready for merging. 🔌: mongo Primarily related to Mongo integrations partner size:S This PR changes 10-29 lines, ignoring generated files. Ɑ: vector store Related to vector store module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants