|
|
@@ -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") |