From af69a8987d4b3fe55c63e181eb4fec6028ba52c8 Mon Sep 17 00:00:00 2001 From: hibobmaster <32976627+hibobmaster@users.noreply.github.com> Date: Tue, 11 Apr 2023 14:21:36 +0800 Subject: [PATCH 1/3] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4e25a82..c3d6e4a 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,14 @@ ## Introduction This is a simple Matrix bot that uses OpenAI's GPT API and Bing AI to generate responses to user inputs. The bot responds to four types of prompts: `!gpt`, `!chat` and `!bing` and `!pic` depending on the first word of the prompt. -![demo](https://i.imgur.com/kK4rnPf.jpeg "demo") +![2023-04-11_14-18](https://user-images.githubusercontent.com/32976627/231073146-3e380217-a6a2-413d-9203-ab36965b909d.png) ## Feature 1. Support openai and Bing AI 2. Support Bing Image Creator 3. Support E2E Encrypted Room +4. Colorful code blocks ## Installation and Setup -- 2.45.2 From 4951cbb302a1ed2c5e257351932fc77424ac35b0 Mon Sep 17 00:00:00 2001 From: BobMaster <32976627+hibobmaster@users.noreply.github.com> Date: Wed, 12 Apr 2023 11:50:52 +0800 Subject: [PATCH 2/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c3d6e4a..23d04a6 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This is a simple Matrix bot that uses OpenAI's GPT API and Bing AI to generate responses to user inputs. The bot responds to four types of prompts: `!gpt`, `!chat` and `!bing` and `!pic` depending on the first word of the prompt. ![2023-04-11_14-18](https://user-images.githubusercontent.com/32976627/231073146-3e380217-a6a2-413d-9203-ab36965b909d.png) - +![demo](https://i.imgur.com/kK4rnPf.jpeg) ## Feature 1. Support openai and Bing AI -- 2.45.2 From bce867acd2b4313736b8300a8df6c2935d90d313 Mon Sep 17 00:00:00 2001 From: hibobmaster Date: Thu, 13 Apr 2023 13:41:47 +0800 Subject: [PATCH 3/3] improve error handle --- askgpt.py | 2 +- bing.py | 4 ++-- bot.py | 27 +++++++++++++++++---------- send_image.py | 1 + 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/askgpt.py b/askgpt.py index c840284..1b1c083 100644 --- a/askgpt.py +++ b/askgpt.py @@ -36,4 +36,4 @@ class askGPT: resp = await response.read() return json.loads(resp)['choices'][0]['message']['content'] except Exception as e: - logger.error("Error Exception", exc_info=True) + raise Exception(e) diff --git a/bing.py b/bing.py index 15350cc..d14b3a7 100644 --- a/bing.py +++ b/bing.py @@ -47,6 +47,6 @@ class BingBot: return json_body['response'] except Exception as e: logger.error("Error Exception", exc_info=True) - print(f"Error: {e}") - pass + + return "Error, please retry" diff --git a/bot.py b/bot.py index 0390169..9764e68 100644 --- a/bot.py +++ b/bot.py @@ -495,14 +495,14 @@ class Bot: try: text = await asyncio.wait_for(self.chatbot.ask_async(prompt), timeout=180) except TimeoutError as e: - logger.error("timeoutException", exc_info=True) - text = "Timeout error" + logger.error(f"TimeoutException: {e}", exc_info=True) + raise Exception("Timeout error") except Exception as e: - logger.error("Error", exc_info=True) - print(f"Error: {e}") + raise Exception(e) - text = text.strip() + try: + text = text.strip() await send_room_message(self.client, room_id, reply_message=text, reply_to_event_id="", sender_id=sender_id, user_message=raw_user_message, markdown_formatted=self.markdown_formatted) except Exception as e: @@ -516,11 +516,14 @@ class Bot: # timeout 120s text = await asyncio.wait_for(self.askgpt.oneTimeAsk(prompt, self.chatgpt_api_endpoint, self.headers), timeout=180) except TimeoutError: - logger.error("timeoutException", exc_info=True) - text = "Timeout error" + logger.error("TimeoutException", exc_info=True) + raise Exception("Timeout error") + except Exception as e: + raise Exception(e) - text = text.strip() + try: + text = text.strip() await send_room_message(self.client, room_id, reply_message=text, reply_to_event_id="", sender_id=sender_id, user_message=raw_user_message, markdown_formatted=self.markdown_formatted) except Exception as e: @@ -535,9 +538,13 @@ class Bot: text = await asyncio.wait_for(self.bingbot.ask_bing(prompt), timeout=180) except TimeoutError: logger.error("timeoutException", exc_info=True) - text = "Timeout error" - text = text.strip() + raise Exception("Timeout error") + except Exception as e: + raise Exception(e) + + try: + text = text.strip() await send_room_message(self.client, room_id, reply_message=text, reply_to_event_id="", sender_id=sender_id, user_message=raw_user_message, markdown_formatted=self.markdown_formatted) except Exception as e: diff --git a/send_image.py b/send_image.py index 3378d44..3e363d5 100644 --- a/send_image.py +++ b/send_image.py @@ -59,3 +59,4 @@ async def send_room_image(client: AsyncClient, except Exception as e: logger.error( f"Image send of file {image} failed.\n Error: {e}", exc_info=True) + raise Exception(e) -- 2.45.2