diff --git a/README.md b/README.md index 341c734..5179827 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ pip3 install azure-cognitiveservices-speech { "open_ai_api_key": "YOUR API KEY", # 填入上面创建的 OpenAI API KEY "model": "gpt-3.5-turbo", # 模型名称。当use_azure_chatgpt为true时,其名称为Azure上model deployment名称 - "proxy": "127.0.0.1:7890", # 代理客户端的ip和端口 + "proxy": "", # 代理客户端的ip和端口,国内网络环境需要填该项,如 "127.0.0.1:7890" "single_chat_prefix": ["bot", "@bot"], # 私聊时文本需要包含该前缀才能触发机器人回复 "single_chat_reply_prefix": "[bot] ", # 私聊时自动回复的前缀,用于区分真人 "group_chat_prefix": ["@bot"], # 群聊时包含该前缀则会触发机器人回复 diff --git a/bot/linkai/link_ai_bot.py b/bot/linkai/link_ai_bot.py index 8b8ca8b..c95113a 100644 --- a/bot/linkai/link_ai_bot.py +++ b/bot/linkai/link_ai_bot.py @@ -29,18 +29,24 @@ class LinkAIBot(Bot, OpenAIImage): if context.type == ContextType.TEXT: return self._chat(query, context) elif context.type == ContextType.IMAGE_CREATE: - ok, retstring = self.create_img(query, 0) - reply = None + ok, res = self.create_img(query, 0) if ok: - reply = Reply(ReplyType.IMAGE_URL, retstring) + reply = Reply(ReplyType.IMAGE_URL, res) else: - reply = Reply(ReplyType.ERROR, retstring) + reply = Reply(ReplyType.ERROR, res) return reply else: reply = Reply(ReplyType.ERROR, "Bot不支持处理{}类型的消息".format(context.type)) return reply - def _chat(self, query, context, retry_count=0): + def _chat(self, query, context, retry_count=0) -> Reply: + """ + 发起对话请求 + :param query: 请求提示词 + :param context: 对话上下文 + :param retry_count: 当前递归重试次数 + :return: 回复 + """ if retry_count >= 2: # exit from retry 2 times logger.warn("[LINKAI] failed after maximum number of retry times") @@ -63,10 +69,8 @@ class LinkAIBot(Bot, OpenAIImage): if app_code and session.messages[0].get("role") == "system": session.messages.pop(0) - logger.info(f"[LINKAI] query={query}, app_code={app_code}") - body = { - "appCode": app_code, + "app_code": app_code, "messages": session.messages, "model": conf().get("model") or "gpt-3.5-turbo", # 对话模型的名称 "temperature": conf().get("temperature"), @@ -74,31 +78,34 @@ class LinkAIBot(Bot, OpenAIImage): "frequency_penalty": conf().get("frequency_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 "presence_penalty": conf().get("presence_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 } + logger.info(f"[LINKAI] query={query}, app_code={app_code}, mode={body.get('model')}") headers = {"Authorization": "Bearer " + linkai_api_key} # do http request - res = requests.post(url=self.base_url + "/chat/completion", json=body, headers=headers).json() - - if not res or not res["success"]: - if res.get("code") == self.AUTH_FAILED_CODE: - logger.exception(f"[LINKAI] please check your linkai_api_key, res={res}") - return Reply(ReplyType.ERROR, "请再问我一次吧") + res = requests.post(url=self.base_url + "/chat/completions", json=body, headers=headers, + timeout=conf().get("request_timeout", 180)) + if res.status_code == 200: + # execute success + response = res.json() + reply_content = response["choices"][0]["message"]["content"] + total_tokens = response["usage"]["total_tokens"] + logger.info(f"[LINKAI] reply={reply_content}, total_tokens={total_tokens}") + self.sessions.session_reply(reply_content, session_id, total_tokens) + return Reply(ReplyType.TEXT, reply_content) - elif res.get("code") == self.NO_QUOTA_CODE: - logger.exception(f"[LINKAI] please check your account quota, https://chat.link-ai.tech/console/account") - return Reply(ReplyType.ERROR, "提问太快啦,请休息一下再问我吧") + else: + response = res.json() + error = response.get("error") + logger.error(f"[LINKAI] chat failed, status_code={res.status_code}, " + f"msg={error.get('message')}, type={error.get('type')}") - else: - # retry + if res.status_code >= 500: + # server error, need retry time.sleep(2) logger.warn(f"[LINKAI] do retry, times={retry_count}") return self._chat(query, context, retry_count + 1) - # execute success - reply_content = res["data"]["content"] - logger.info(f"[LINKAI] reply={reply_content}") - self.sessions.session_reply(reply_content, session_id) - return Reply(ReplyType.TEXT, reply_content) + return Reply(ReplyType.ERROR, "提问太快啦,请休息一下再问我吧") except Exception as e: logger.exception(e)