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
This commit is contained in:
鄭棋文Steven ,Chi-Wen Cheng 2024-01-10 14:26:50 +08:00 committed by GitHub
parent 78e7e711b9
commit ecf86a2747
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -46,6 +46,7 @@ class ChatGPTAPI(Base):
super().__init__(key, language) super().__init__(key, language)
self.key_len = len(key.split(",")) self.key_len = len(key.split(","))
self.openai_client = OpenAI(api_key=key, base_url=api_base) self.openai_client = OpenAI(api_key=key, base_url=api_base)
self.api_base = api_base
self.prompt_template = ( self.prompt_template = (
prompt_template prompt_template
@ -97,7 +98,11 @@ class ChatGPTAPI(Base):
completion = self.create_chat_completion(text) completion = self.create_chat_completion(text)
# TODO work well or exception finish by length limit # 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 return t_text