From ecf86a274793fe2f999ee82f821cb461b2c3ddfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=84=AD=E6=A3=8B=E6=96=87Steven=20=2CChi-Wen=20Cheng?= Date: Wed, 10 Jan 2024 14:26:50 +0800 Subject: [PATCH] Fixed Azure openai bug (#368) fix(chatgptapi_translator): when use azure openai something wrong 1. when I use azure openai will get "api_base" not have this arribute error. So I add self.api_base = api_base 2. In line 84 when the message content type is None will get typeError. So I add a if else to avoid this error. * style: format code style * style: format code style * fix: lint --- book_maker/translator/chatgptapi_translator.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/book_maker/translator/chatgptapi_translator.py b/book_maker/translator/chatgptapi_translator.py index e880bfb..5c686c5 100644 --- a/book_maker/translator/chatgptapi_translator.py +++ b/book_maker/translator/chatgptapi_translator.py @@ -46,6 +46,7 @@ class ChatGPTAPI(Base): super().__init__(key, language) self.key_len = len(key.split(",")) self.openai_client = OpenAI(api_key=key, base_url=api_base) + self.api_base = api_base self.prompt_template = ( prompt_template @@ -97,7 +98,11 @@ class ChatGPTAPI(Base): completion = self.create_chat_completion(text) # TODO work well or exception finish by length limit - t_text = completion.choices[0].message.content.encode("utf8").decode() or "" + # Check if content is not None before encoding + if completion.choices[0].message.content is not None: + t_text = completion.choices[0].message.content.encode("utf8").decode() or "" + else: + t_text = "" return t_text