From a47f1da884bb2b9741bf95c8e7a22a1937affa7a Mon Sep 17 00:00:00 2001 From: WaseemH Date: Tue, 28 Nov 2023 20:37:03 -0500 Subject: [PATCH] docs[patch]: RAG Cookbook example fix (#13914) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Description: Hey 👋🏽 this is a small docs example fix. Hoping it helps future developers who are working with Langchain. ### Problem: Take a look at the original example code. You were not able to get the `dialogue_turn[0]` while it was a tuple. Original code: ```python def _format_chat_history(chat_history: List[Tuple]) -> str: buffer = "" for dialogue_turn in chat_history: human = "Human: " + dialogue_turn[0] ai = "Assistant: " + dialogue_turn[1] buffer += "\n" + "\n".join([human, ai]) return buffer ``` In the original code you were getting this error: ```bash human = "Human: " + dialogue_turn[0].content ~~~~~~~~~~~~~^^^ TypeError: 'HumanMessage' object is not subscriptable ``` ### Solution: The fix is to just for loop over the chat history and look to see if its a human or ai message and add it to the buffer. --- docs/docs/expression_language/cookbook/retrieval.ipynb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/docs/expression_language/cookbook/retrieval.ipynb b/docs/docs/expression_language/cookbook/retrieval.ipynb index 2689ffcd96..27470a4a56 100644 --- a/docs/docs/expression_language/cookbook/retrieval.ipynb +++ b/docs/docs/expression_language/cookbook/retrieval.ipynb @@ -234,7 +234,13 @@ "from typing import List, Tuple\n", "\n", "\n", - "def _format_chat_history(chat_history: List[Tuple]) -> str:\n", + "def _format_chat_history(chat_history: List[Tuple[str, str]]) -> str:\n", + " # chat history is of format:\n", + " # [\n", + " # (human_message_str, ai_message_str),\n", + " # ...\n", + " # ]\n", + " # see below for an example of how it's invoked\n", " buffer = \"\"\n", " for dialogue_turn in chat_history:\n", " human = \"Human: \" + dialogue_turn[0]\n",