From 2b83c7c3ab16b6d086b8d740091bed5655994ff0 Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 24 Sep 2024 15:47:36 +0200 Subject: [PATCH] community[patch]: Fix `tool_calls` parsing when streaming from DeepInfra (#26813) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - **Description:** This PR fixes the response parsing logic for `ChatDeepInfra`, more specifially `_convert_delta_to_message_chunk()`, which is invoked when streaming via `ChatDeepInfra`. - **Issue:** Streaming from DeepInfra via `ChatDeepInfra` is currently broken because the response parsing logic doesn't handle that `tool_calls` can be `None`. (There is no GitHub issue for this problem yet.) - **Dependencies:** – - **Twitter handle:** – Keeping this here as a reminder: > If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17. --- libs/community/langchain_community/chat_models/deepinfra.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libs/community/langchain_community/chat_models/deepinfra.py b/libs/community/langchain_community/chat_models/deepinfra.py index 061bed9e8b0..71389cc2c90 100644 --- a/libs/community/langchain_community/chat_models/deepinfra.py +++ b/libs/community/langchain_community/chat_models/deepinfra.py @@ -144,13 +144,12 @@ def _convert_delta_to_message_chunk( ) -> BaseMessageChunk: role = _dict.get("role") content = _dict.get("content") or "" + tool_calls = _dict.get("tool_calls") or [] if role == "user" or default_class == HumanMessageChunk: return HumanMessageChunk(content=content) elif role == "assistant" or default_class == AIMessageChunk: - tool_calls = [ - _parse_tool_calling(tool_call) for tool_call in _dict.get("tool_calls", []) - ] + tool_calls = [_parse_tool_calling(tool_call) for tool_call in tool_calls] return AIMessageChunk(content=content, tool_calls=tool_calls) elif role == "system" or default_class == SystemMessageChunk: return SystemMessageChunk(content=content)