From 2dfbc840b362ae80076082725f65edf0943d4316 Mon Sep 17 00:00:00 2001 From: lanvent Date: Thu, 13 Apr 2023 20:44:08 +0800 Subject: [PATCH] chore: save model args as a dict --- bot/chatgpt/chat_gpt_bot.py | 32 ++++++++++++-------------------- bot/openai/open_ai_bot.py | 20 ++++++++++++-------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/bot/chatgpt/chat_gpt_bot.py b/bot/chatgpt/chat_gpt_bot.py index 9e99809..feb428f 100644 --- a/bot/chatgpt/chat_gpt_bot.py +++ b/bot/chatgpt/chat_gpt_bot.py @@ -28,6 +28,16 @@ class ChatGPTBot(Bot,OpenAIImage): self.tb4chatgpt = TokenBucket(conf().get('rate_limit_chatgpt', 20)) self.sessions = SessionManager(ChatGPTSession, model= conf().get("model") or "gpt-3.5-turbo") + self.args ={ + "model": conf().get("model") or "gpt-3.5-turbo", # 对话模型的名称 + "temperature":conf().get('temperature', 0.9), # 值在[0,1]之间,越大表示回复越具有不确定性 + # "max_tokens":4096, # 回复最大的字符数 + "top_p":1, + "frequency_penalty":conf().get('frequency_penalty', 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 + "presence_penalty":conf().get('presence_penalty', 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 + "request_timeout": conf().get('request_timeout', None), # 请求超时时间,openai接口默认设置为600,对于难问题一般需要较长时间 + "timeout": conf().get('request_timeout', None), #重试超时时间,在这个时间内,将会自动重试 + } def reply(self, query, context=None): # acquire reply content @@ -82,18 +92,6 @@ class ChatGPTBot(Bot,OpenAIImage): reply = Reply(ReplyType.ERROR, 'Bot不支持处理{}类型的消息'.format(context.type)) return reply - def compose_args(self): - return { - "model": conf().get("model") or "gpt-3.5-turbo", # 对话模型的名称 - "temperature":conf().get('temperature', 0.9), # 值在[0,1]之间,越大表示回复越具有不确定性 - # "max_tokens":4096, # 回复最大的字符数 - "top_p":1, - "frequency_penalty":conf().get('frequency_penalty', 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 - "presence_penalty":conf().get('presence_penalty', 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 - "request_timeout": conf().get('request_timeout', None), # 请求超时时间,openai接口默认设置为600,对于难问题一般需要较长时间 - "timeout": conf().get('request_timeout', None), #重试超时时间,在这个时间内,将会自动重试 - } - def reply_text(self, session:ChatGPTSession, api_key=None, retry_count=0) -> dict: ''' call openai's ChatCompletion to get the answer @@ -107,7 +105,7 @@ class ChatGPTBot(Bot,OpenAIImage): raise openai.error.RateLimitError("RateLimitError: rate limit exceeded") # if api_key == None, the default openai.api_key will be used response = openai.ChatCompletion.create( - api_key=api_key, messages=session.messages, **self.compose_args() + api_key=api_key, messages=session.messages, **self.args ) # logger.info("[ChatGPT] reply={}, total_tokens={}".format(response.choices[0]['message']['content'], response["usage"]["total_tokens"])) return {"total_tokens": response["usage"]["total_tokens"], @@ -147,10 +145,4 @@ class AzureChatGPTBot(ChatGPTBot): super().__init__() openai.api_type = "azure" openai.api_version = "2023-03-15-preview" - - def compose_args(self): - args = super().compose_args() - args["deployment_id"] = conf().get("azure_deployment_id") - #args["engine"] = args["model"] - #del(args["model"]) - return args + self.args["deployment_id"] = conf().get("azure_deployment_id") \ No newline at end of file diff --git a/bot/openai/open_ai_bot.py b/bot/openai/open_ai_bot.py index 4d88b99..757f36a 100644 --- a/bot/openai/open_ai_bot.py +++ b/bot/openai/open_ai_bot.py @@ -26,6 +26,17 @@ class OpenAIBot(Bot, OpenAIImage): openai.proxy = proxy self.sessions = SessionManager(OpenAISession, model= conf().get("model") or "text-davinci-003") + self.args = { + "model": conf().get("model") or "text-davinci-003", # 对话模型的名称 + "temperature":conf().get('temperature', 0.9), # 值在[0,1]之间,越大表示回复越具有不确定性 + "max_tokens":1200, # 回复最大的字符数 + "top_p":1, + "frequency_penalty":conf().get('frequency_penalty', 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 + "presence_penalty":conf().get('presence_penalty', 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 + "request_timeout": conf().get('request_timeout', None), # 请求超时时间,openai接口默认设置为600,对于难问题一般需要较长时间 + "timeout": conf().get('request_timeout', None), #重试超时时间,在这个时间内,将会自动重试 + "stop":["\n\n\n"] + } def reply(self, query, context=None): # acquire reply content @@ -64,14 +75,7 @@ class OpenAIBot(Bot, OpenAIImage): def reply_text(self, session:OpenAISession, retry_count=0): try: response = openai.Completion.create( - model= conf().get("model") or "text-davinci-003", # 对话模型的名称 - prompt=str(session), - temperature=0.9, # 值在[0,1]之间,越大表示回复越具有不确定性 - max_tokens=1200, # 回复最大的字符数 - top_p=1, - frequency_penalty=0.0, # [-2,2]之间,该值越大则更倾向于产生不同的内容 - presence_penalty=0.0, # [-2,2]之间,该值越大则更倾向于产生不同的内容 - stop=["\n\n\n"] + prompt=str(session), **self.args ) res_content = response.choices[0]['text'].strip().replace('<|endoftext|>', '') total_tokens = response["usage"]["total_tokens"]