Skip to content

Commit

Permalink
for ChatVectorDBChain, add top_k_docs_for_context to allow control ho…
Browse files Browse the repository at this point in the history
…w many chunks of context will be retrieved (#1155)

given that we allow user define chunk size, think it would be useful for
user to define how many chunks of context will be retrieved.
  • Loading branch information
AlexZhangji committed Feb 20, 2023
1 parent 955c89f commit ed37fba
Showing 1 changed file with 7 additions and 2 deletions.
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

0 comments on commit ed37fba

Please sign in to comment.