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

add single execution option for ConversationalRetrievalChain #5066

Conversation

jpzhangvincent
Copy link
Contributor

@jpzhangvincent jpzhangvincent commented May 21, 2023

add single execution option for ConversationalRetrievalChain

  • The current default workflow for ConversationalRetrievalChain is to rephrase the question based on the chat history and then use the rephrased question to perform retrieval and question answering.
  • This current two-step process has pros and cons. It helps address the context size limit but it could increase unnecessary latency and incur potential information loss.
  • We can make the question_generator (chain) argument optional to allow it to bypass the rephrasing step and use a custom prompt to combine the chat_history, context and question together for single execution. This could give users more flexibility based on their use cases and requirements.

Fixes #5123

Before submitting

I updated the notebook to include the ## ConversationalRetrievalChain with only custom combine_docs_chain section

Who can review?

Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested:
@hwchase17 @dev2049

@jpzhangvincent
Copy link
Contributor Author

I believe this feature provides practical values. Can I get a review on this one? @hwchase17 @dev2049

@franco-roura
Copy link

Hello guys, we're having a similar issue with the ConversationalRetrievalQAChain class.
When adding a Pinecone vector store for memory, it can even reach 45 seconds of total response time, which is far from ideal. This PR does seem to have the potential to mitigate the issue, is it being actively looked at?

Thanks in advance.

Copy link
Contributor

@dev2049 dev2049 left a comment

Choose a reason for hiding this comment

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

making question_generator optional makes sense to me! but don't think we should be messing with prompts at runtime, you should be able to set those when instantiating the chain in first place

and "context"
not in self.combine_docs_chain.llm_chain.prompt.input_variables
):
self.combine_docs_chain.llm_chain.prompt = CHAT_RETRIEVAL_QA_PROMPT
Copy link
Contributor

Choose a reason for hiding this comment

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

why do this at runtime, why not set the prompt of combine_docs_chain when instantiating the ConversationalRetrievalChain?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense! Just made the change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dev2049 can you review again?

@jpzhangvincent jpzhangvincent requested a review from dev2049 May 30, 2023 07:00
@jpzhangvincent jpzhangvincent force-pushed the add-convQARetrchain-single-run branch from db0f390 to fd0153e Compare May 30, 2023 16:17
@hwchase17
Copy link
Contributor

i think we need just better documentation around conversational retrieval chains in general

@jpzhangvincent
Copy link
Contributor Author

i think we need just better documentation around conversational retrieval chains in general

I have updated the notebook to show how to set question_generator as None and use a custom prompt. But feel free to let me know anything else needed to get this PR merged. Thanks!

@jpzhangvincent
Copy link
Contributor Author

Hoping this can get merged soon since I need this patch for a work project! @hwchase17 @dev2049 any feedback?

@italojs
Copy link

italojs commented Jun 13, 2023

👀👀👀

@jpzhangvincent jpzhangvincent force-pushed the add-convQARetrchain-single-run branch from b92120d to 98794d1 Compare June 27, 2023 16:15
@vercel
Copy link

vercel bot commented Jun 27, 2023

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 Mar 6, 2024 0:39am

@jpzhangvincent
Copy link
Contributor Author

@dev2049 @hwchase17 can you take a look at this PR? Sorry for chasing..

langchain/chains/conversational_retrieval/prompts.py Outdated Show resolved Hide resolved
langchain/chains/conversational_retrieval/base.py Outdated Show resolved Hide resolved
@@ -36,6 +37,7 @@ def format_document(doc: Document, prompt: BasePromptTemplate) -> str:
class BaseCombineDocumentsChain(Chain, ABC):
"""Base interface for chains combining documents."""

llm_chain: LLMChain

Choose a reason for hiding this comment

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

is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes because we set the prompt variable like
combine_docs_chain.llm_chain.prompt = CHAT_RETRIEVAL_QA_PROMPT in the validate_combine_docs_chain function for the error handling and I found I have to put the type there to pass the linting check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@leferradkw let me know if any other questions! Hope to get the PR merged soon! :)

Choose a reason for hiding this comment

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

Then add a unit test to prove the constructor is working well and the interface is the same as before

@anthonycoded
Copy link

How can I implement this solution in my project?

@jpzhangvincent jpzhangvincent force-pushed the add-convQARetrchain-single-run branch from 98794d1 to c496d5b Compare July 8, 2023 16:46
@jpzhangvincent jpzhangvincent requested a review from leferradkw July 8, 2023 16:57
langchain/chains/combine_documents/base.py Outdated Show resolved Hide resolved
@@ -36,6 +37,7 @@ def format_document(doc: Document, prompt: BasePromptTemplate) -> str:
class BaseCombineDocumentsChain(Chain, ABC):
"""Base interface for chains combining documents."""

llm_chain: LLMChain

Choose a reason for hiding this comment

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

Then add a unit test to prove the constructor is working well and the interface is the same as before

@dosubot dosubot bot added the 🤖:improvement Medium size change to existing code to handle new use-cases label Jul 14, 2023
@jpzhangvincent jpzhangvincent force-pushed the add-convQARetrchain-single-run branch from c496d5b to 2f42514 Compare August 9, 2023 23:48
@kowsikgelli
Copy link

A must needed feature. I am looking for this.

@leo-gan
Copy link
Collaborator

leo-gan commented Sep 13, 2023

@jpzhangvincent Hi , could you, please, resolve the merging issues and address the last comments (if needed)? After that, ping me and I push this PR for the review. Thanks!

@leo-gan
Copy link
Collaborator

leo-gan commented Sep 15, 2023

@jpzhangvincent It is again :( Could you, please, resolve the merging issues? After that ping me and I push this PR for the review. Thanks!

@jpzhangvincent
Copy link
Contributor Author

@jpzhangvincent It is again :( Could you, please, resolve the merging issues? After that ping me and I push this PR for the review. Thanks!

@leo-gan I believe I have fixed the merge conflicts. I tried to add a test or an example in the notebook(if needed) but not sure where/which file to add specifically. Open to suggestion!

@leo-gan
Copy link
Collaborator

leo-gan commented Sep 15, 2023

@jpzhangvincent There are failed unit tests and pydantic compatibility tests. When you fix them we will be ready.

@jpzhangvincent jpzhangvincent force-pushed the add-convQARetrchain-single-run branch from b33137d to 4418657 Compare September 18, 2023 21:59
@jpzhangvincent
Copy link
Contributor Author

@leo-gan I believe I had resolved the test issues.. please review again and feel free to make code changes directly!

@leo-gan leo-gan requested a review from baskaryan September 19, 2023 15:17
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:XXL This PR changes 1000+ lines, ignoring generated files. labels Dec 22, 2023
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Dec 23, 2023
@jpzhangvincent
Copy link
Contributor Author

@baskaryan @leo-gan Okay I have addressed the feedback and added a unit test. Hopeful we can get this merged soon since it could be low-hanging fruit but useful feature.

@jpzhangvincent
Copy link
Contributor Author

@baskaryan @leo-gan Okay I have addressed the feedback and added a unit test. Hopeful we can get this merged soon since it could be low-hanging fruit but useful feature.

@baskaryan @leo-gan @hwchase17 thoughts on merging this PR?

@hwchase17 hwchase17 closed this Jan 30, 2024
@baskaryan baskaryan reopened this Jan 30, 2024
@jpzhangvincent
Copy link
Contributor Author

Can you please review again? Thanks! @baskaryan @leo-gan

@ccurme ccurme added the langchain Related to the langchain package label Jun 21, 2024
@ccurme
Copy link
Collaborator

ccurme commented Jul 8, 2024

Thank you for this. Closing because this is readily achievable with create_retrieval_chain. See the example snippet in the API reference. You can also reference this guide on migrating from ConversationalRetrievalChain to LCEL implementations. Please let me know if you have any other concerns.

@ccurme ccurme closed this Jul 8, 2024
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 langchain Related to the langchain package size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Slow response time with ConversationalRetrievalQAChain