From 5d05df4bce9f7d4254afbc02a009a4dcd3795874 Mon Sep 17 00:00:00 2001 From: Kai <1974961+Yuffie@users.noreply.github.com> Date: Mon, 8 Jan 2024 00:30:18 +0800 Subject: [PATCH] community: Fixed bug of "system message check" in chat_models/tongyi. (#15631) - **Description:** This PR is to fix a bug of "system message check" in langchain_community/ chat_models/tongyi.py - **Issue:** In term of current logic, if there's no system message in the chat messages, an error of "System message can only be the first message." will be wrongly raised. - **Dependencies:** No. - **Twitter handle:** I don't have a Twitter account. --- libs/community/langchain_community/chat_models/tongyi.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libs/community/langchain_community/chat_models/tongyi.py b/libs/community/langchain_community/chat_models/tongyi.py index ab2958d5c7..be4373a505 100644 --- a/libs/community/langchain_community/chat_models/tongyi.py +++ b/libs/community/langchain_community/chat_models/tongyi.py @@ -383,8 +383,10 @@ class ChatTongyi(BaseChatModel): system_message_indices = [ i for i, m in enumerate(message_dicts) if m["role"] == "system" ] - if len(system_message_indices) != 1 or system_message_indices[0] != 0: + if len(system_message_indices) == 1 and system_message_indices[0] != 0: raise ValueError("System message can only be the first message.") + elif len(system_message_indices) > 1: + raise ValueError("There can be only one system message at most.") params["messages"] = message_dicts