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

for ChatVectorDBChain, add top_k_docs_for_context to allow control how many chunks of context will be retrieved #1155

Merged
merged 5 commits into from
Feb 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions langchain/chains/chat_vector_db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ChatVectorDBChain(Chain, BaseModel):
question_generator: LLMChain
output_key: str = "answer"
return_source_documents: bool = False
top_k_docs_for_context: int = 4
"""Return the source documents."""

@property
Expand Down Expand Up @@ -88,7 +89,9 @@ def _call(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
)
else:
new_question = question
docs = self.vectorstore.similarity_search(new_question, k=4, **vectordbkwargs)
docs = self.vectorstore.similarity_search(
new_question, k=self.top_k_docs_for_context, **vectordbkwargs
)
new_inputs = inputs.copy()
new_inputs["question"] = new_question
new_inputs["chat_history"] = chat_history_str
Expand All @@ -109,7 +112,9 @@ async def _acall(self, inputs: Dict[str, Any]) -> Dict[str, str]:
else:
new_question = question
# TODO: This blocks the event loop, but it's not clear how to avoid it.
docs = self.vectorstore.similarity_search(new_question, k=4, **vectordbkwargs)
docs = self.vectorstore.similarity_search(
new_question, k=self.top_k_docs_for_context, **vectordbkwargs
)
new_inputs = inputs.copy()
new_inputs["question"] = new_question
new_inputs["chat_history"] = chat_history_str
Expand Down