From df357f82ca715f78cf0ea6402a9d158bc794d893 Mon Sep 17 00:00:00 2001 From: MarkYQJ Date: Tue, 23 Jul 2024 04:11:17 +0800 Subject: [PATCH] ignore the first turn to apply "history" mechanism (#14118) This will generate a meaningless string "system: " for generating condense question; this increases the probability to make an improper condense question and misunderstand user's question. Below is a case - Original Question: Can you explain the arguments of Meilisearch? - Condense Question - What are the benefits of using Meilisearch? (by CodeLlama) - What are the reasons for using Meilisearch? (by GPT-4) The condense questions (not matter from CodeLlam or GPT-4) are different from the original one. By checking the content of each dialogue turn, generating history string only when the dialog content is not empty. Since there is nothing before first turn, the "history" mechanism will be ignored at the very first turn. Doing so, the condense question will be "What are the arguments for using Meilisearch?". --------- Co-authored-by: Bagatur Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: ccurme --- .../langchain/chains/conversational_retrieval/base.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libs/langchain/langchain/chains/conversational_retrieval/base.py b/libs/langchain/langchain/chains/conversational_retrieval/base.py index ec6c80502e..b35f93167f 100644 --- a/libs/langchain/langchain/chains/conversational_retrieval/base.py +++ b/libs/langchain/langchain/chains/conversational_retrieval/base.py @@ -42,8 +42,11 @@ def _get_chat_history(chat_history: List[CHAT_TURN_TYPE]) -> str: buffer = "" for dialogue_turn in chat_history: if isinstance(dialogue_turn, BaseMessage): - role_prefix = _ROLE_MAP.get(dialogue_turn.type, f"{dialogue_turn.type}: ") - buffer += f"\n{role_prefix}{dialogue_turn.content}" + if len(dialogue_turn.content) > 0: + role_prefix = _ROLE_MAP.get( + dialogue_turn.type, f"{dialogue_turn.type}: " + ) + buffer += f"\n{role_prefix}{dialogue_turn.content}" elif isinstance(dialogue_turn, tuple): human = "Human: " + dialogue_turn[0] ai = "Assistant: " + dialogue_turn[1]