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

Async Support for LLMChainExtractor #3587

Closed
wants to merge 4 commits into from

Conversation

jphme
Copy link

@jphme jphme commented Apr 26, 2023

Implemented acompress_documents and changed syntax for compress_documents slightly to make sync/async functions consistent.

LLMChainExtractor as implemented in #2915 for use in the ContextualCompressionRetriever lacked an async method. As compression of retrieved documents is a highly parallelizable task, this was a major performance bottleneck in my tests.

This implementation is consistent to the implementation in the somewhat similar MapReduceDocumentsChain chain, see https://github.com/hwchase17/langchain/blob/85dae78548ed0c11db06e9154c7eb4236a1ee246/langchain/chains/combine_documents/map_reduce.py#L131 .

In my own tests (standalone as well as in a compression pipeline) inputs/outputs are unchanged and the async-speedup is significant.

@jphme
Copy link
Author

jphme commented Apr 26, 2023

fixed the formatting...

@jphme
Copy link
Author

jphme commented Apr 29, 2023

Reverted the Sync changes and just added async without the use of apply as sugested.
New PR here: #3780 .

@jphme jphme closed this Apr 29, 2023
hwchase17 pushed a commit that referenced this pull request May 2, 2023
@vowelparrot @hwchase17 Here a new implementation of
`acompress_documents` for `LLMChainExtractor ` without changes to the
sync-version, as you suggested in #3587 / [Async Support for
LLMChainExtractor](#3587) .

I created a new PR to avoid cluttering history with reverted commits,
hope that is the right way.
Happy for any improvements/suggestions.

(PS:
I also tried an alternative implementation with a nested helper function
like

``` python
  async def acompress_documents_old(
      self, documents: Sequence[Document], query: str
  ) -> Sequence[Document]:
      """Compress page content of raw documents."""
      async def _compress_concurrently(doc):
          _input = self.get_input(query, doc)
          output = await self.llm_chain.apredict_and_parse(**_input)
          return Document(page_content=output, metadata=doc.metadata)
      outputs=await asyncio.gather(*[_compress_concurrently(doc) for doc in documents])
      compressed_docs=list(filter(lambda x: len(x.page_content)>0,outputs))
      return compressed_docs
```

But in the end I found the commited version to be better readable and
more "canonical" - hope you agree.
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.

None yet

3 participants