From 750fa0ad52ec6f3f7bb26d01dbdd20e3b5a7f9a5 Mon Sep 17 00:00:00 2001 From: Sypherd Date: Fri, 15 Dec 2023 07:29:49 -0700 Subject: [PATCH 1/3] Add safe lookup to OpenAI response adapter --- .../langchain_community/adapters/openai.py | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/libs/community/langchain_community/adapters/openai.py b/libs/community/langchain_community/adapters/openai.py index 0af759ebf5b08..f828bc99f4954 100644 --- a/libs/community/langchain_community/adapters/openai.py +++ b/libs/community/langchain_community/adapters/openai.py @@ -71,27 +71,30 @@ def convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage: Returns: The LangChain message. """ - role = _dict["role"] + role = _dict.get("role") if role == "user": - return HumanMessage(content=_dict["content"]) + return HumanMessage(content=_dict.get("content")) elif role == "assistant": # Fix for azure # Also OpenAI returns None for tool invocations - content = _dict.get("content", "") or "" + content = _dict.get("content", "") additional_kwargs: Dict = {} - if _dict.get("function_call"): - additional_kwargs["function_call"] = dict(_dict["function_call"]) - if _dict.get("tool_calls"): - additional_kwargs["tool_calls"] = _dict["tool_calls"] + if function_call := _dict.get("function_call"): + additional_kwargs["function_call"] = dict(function_call) + if tool_calls := _dict.get("tool_calls"): + additional_kwargs["tool_calls"] = tool_calls return AIMessage(content=content, additional_kwargs=additional_kwargs) elif role == "system": - return SystemMessage(content=_dict["content"]) + return SystemMessage(content=_dict.get("content", "")) elif role == "function": - return FunctionMessage(content=_dict["content"], name=_dict["name"]) + return FunctionMessage(content=_dict.get("content", ""), name=_dict.get("name")) elif role == "tool": - return ToolMessage(content=_dict["content"], tool_call_id=_dict["tool_call_id"]) + return ToolMessage( + content=_dict.get("content", ""), + tool_call_id=_dict.get("tool_call_id"), + ) else: - return ChatMessage(content=_dict["content"], role=role) + return ChatMessage(content=_dict.get("content", ""), role=role) def convert_message_to_dict(message: BaseMessage) -> dict: From 0295cbc7cac30ebd5d3ee2ab7467b676a95180f8 Mon Sep 17 00:00:00 2001 From: Sypherd <50557586+Sypherd@users.noreply.github.com> Date: Fri, 15 Dec 2023 08:08:31 -0700 Subject: [PATCH 2/3] Remove redundant lookup --- libs/community/langchain_community/adapters/openai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/community/langchain_community/adapters/openai.py b/libs/community/langchain_community/adapters/openai.py index c99623170d13f..12fdb212f2c94 100644 --- a/libs/community/langchain_community/adapters/openai.py +++ b/libs/community/langchain_community/adapters/openai.py @@ -91,7 +91,7 @@ def convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage: elif role == "tool": additional_kwargs = {} if "name" in _dict: - additional_kwargs["name"] = _dict.get("name") + additional_kwargs["name"] = _dict["name"] return ToolMessage( content=_dict.get("content", ""), tool_call_id=_dict.get("tool_call_id"), From fd0f8a8cbcfb96cd43b12dd3538c74a698021b10 Mon Sep 17 00:00:00 2001 From: Bagatur Date: Wed, 20 Dec 2023 01:10:12 -0500 Subject: [PATCH 3/3] cr --- libs/community/langchain_community/adapters/openai.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/community/langchain_community/adapters/openai.py b/libs/community/langchain_community/adapters/openai.py index 12fdb212f2c94..4f03c3a9c452f 100644 --- a/libs/community/langchain_community/adapters/openai.py +++ b/libs/community/langchain_community/adapters/openai.py @@ -73,11 +73,11 @@ def convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage: """ role = _dict.get("role") if role == "user": - return HumanMessage(content=_dict.get("content")) + return HumanMessage(content=_dict.get("content", "")) elif role == "assistant": # Fix for azure # Also OpenAI returns None for tool invocations - content = _dict.get("content", "") + content = _dict.get("content", "") or "" additional_kwargs: Dict = {} if function_call := _dict.get("function_call"): additional_kwargs["function_call"] = dict(function_call)