From cc0585af426f4a51a197e7beb3a528b87324c8e0 Mon Sep 17 00:00:00 2001 From: tonomura <86943880+ncomtono@users.noreply.github.com> Date: Wed, 12 Jul 2023 07:12:57 +0900 Subject: [PATCH] Improvement/add finish reason to generation info in chat open ai (#7478) Description: ChatOpenAI model does not return finish_reason in generation_info. Issue: #2702 Dependencies: None Tag maintainer: @baskaryan Thank you --------- Co-authored-by: Bagatur --- langchain/chat_models/openai.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/langchain/chat_models/openai.py b/langchain/chat_models/openai.py index 457a2f7857..66bf2761cf 100644 --- a/langchain/chat_models/openai.py +++ b/langchain/chat_models/openai.py @@ -386,7 +386,10 @@ class ChatOpenAI(BaseChatModel): generations = [] for res in response["choices"]: message = _convert_dict_to_message(res["message"]) - gen = ChatGeneration(message=message) + gen = ChatGeneration( + message=message, + generation_info=dict(finish_reason=res.get("finish_reason")), + ) generations.append(gen) llm_output = {"token_usage": response["usage"], "model_name": self.model_name} return ChatResult(generations=generations, llm_output=llm_output)