Closed nithinreddyyyyyy closed 6 months ago
🤖
Hey @nithinreddyyyyyy! 🚀 Great to see you back with another intriguing puzzle for us to solve together. 😄
Based on your request, it seems you want to modify the LangChain code to return one output from each source document instead of multiple outputs from the same source. This can be achieved by modifying the combine_docs
and acombine_docs
methods in the StuffDocumentsChain
class.
Currently, these methods combine all documents into one prompt and pass it to the language model. Instead, you should iterate over the documents, generate a prompt for each one, and pass each prompt to the language model separately.
Here's how you can modify the combine_docs
method:
def combine_docs(
self, docs: List[Document], callbacks: Callbacks = None, **kwargs: Any
) -> List[Tuple[str, dict]]:
"""Generate a separate prompt for each document and pass to LLM.
Args:
docs: List of documents to join together into one variable
callbacks: Optional callbacks to pass along
**kwargs: additional parameters to use to get inputs to LLMChain.
Returns:
A list of tuples. Each tuple contains the single string output for a document
and a dictionary of other keys to return.
"""
results = []
for doc in docs:
inputs = self._get_inputs([doc], **kwargs)
# Call predict on the LLM.
result = self.llm_chain.predict(callbacks=callbacks, **inputs), {}
results.append(result)
return results
And here's how you can modify the acombine_docs
method:
async def acombine_docs(
self, docs: List[Document], callbacks: Callbacks = None, **kwargs: Any
) -> List[Tuple[str, dict]]:
"""Async generate a separate prompt for each document and pass to LLM.
Args:
docs: List of documents to join together into one variable
callbacks: Optional callbacks to pass along
**kwargs: additional parameters to use to get inputs to LLMChain.
Returns:
A list of tuples. Each tuple contains the single string output for a document
and a dictionary of other keys to return.
"""
results = []
for doc in docs:
inputs = self._get_inputs([doc], **kwargs)
# Call predict on the LLM.
result = await self.llm_chain.apredict(callbacks=callbacks, **inputs), {}
results.append(result)
return results
These modifications will ensure that each document is processed separately and a separate output is generated for each document. You can find the original implementation of these methods in the libs/langchain/langchain/chains/combine_documents/stuff.py file in the LangChain repository.
Please note that these changes will affect the behavior of the StuffDocumentsChain
class and any other classes that use it. Make sure to thoroughly test your code after making these modifications to ensure it behaves as expected.
Let me know if you need further assistance.
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
If you want to continue the conversation, start your reply with @dosu-bot.
Issue with current documentation:
below's the complete code
the above is giving below output
it is returning multiple outputs from the same source, but i was expecting one output from each and every source document. Seems like multiple snippets from the same source should be combined and based out of that combined text it should return that one output for that source. Can you please look into this?
Idea or request for content:
No response