From 5dbe456aae755e3190c46316102e772dfcb6e148 Mon Sep 17 00:00:00 2001 From: verlocks <45618243+verlocks@users.noreply.github.com> Date: Sat, 21 Oct 2023 06:46:41 +0800 Subject: [PATCH] Bug fix tongyi.py to be compatible with DashScope API (#11956) Current ChatTongyi is not compatible with DashScope API, which will cause error when passing api key to chat model directly. - **Description:** Update tongyi.py to be compatible with DashScope API. Specifically, update parameter name "dashscope_api_key" to "api_key". - **Issue:** None. - **Dependencies:** Nothing new, Tongyi would require DashScope as before. --- libs/langchain/langchain/chat_models/tongyi.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libs/langchain/langchain/chat_models/tongyi.py b/libs/langchain/langchain/chat_models/tongyi.py index 42326b32df0..6d657483ccc 100644 --- a/libs/langchain/langchain/chat_models/tongyi.py +++ b/libs/langchain/langchain/chat_models/tongyi.py @@ -318,7 +318,14 @@ class ChatTongyi(BaseChatModel): ) return _generate_from_stream(stream_iter) + if not messages: + raise ValueError("No messages provided.") + message_dicts, params = self._create_message_dicts(messages, stop) + + if message_dicts[-1]["role"] != "user": + raise ValueError("Last message should be user message.") + params = {**params, **kwargs} response = self.completion_with_retry( messages=message_dicts, run_manager=run_manager, **params @@ -374,7 +381,7 @@ class ChatTongyi(BaseChatModel): def _client_params(self) -> Dict[str, Any]: """Get the parameters used for the openai client.""" creds: Dict[str, Any] = { - "dashscope_api_key": self.dashscope_api_key, + "api_key": self.dashscope_api_key, } return {**self._default_params, **creds}