From 95891a7fe700b9d95f5775bceb8ffe90d5fc00df Mon Sep 17 00:00:00 2001 From: hibobmaster Date: Tue, 30 May 2023 10:26:39 +0800 Subject: [PATCH] format codes --- BingImageGen.py | 21 ++++++++++++------ askgpt.py | 8 ++++--- bing.py | 39 +++++++++++++++++++++------------ flowise.py | 6 ++++-- send_image.py | 12 +++++------ send_message.py | 57 ++++++++++++++++++++++++++++++++++--------------- v3.py | 21 ++++++------------ 7 files changed, 102 insertions(+), 62 deletions(-) diff --git a/BingImageGen.py b/BingImageGen.py index 7fb32d6..21371fc 100644 --- a/BingImageGen.py +++ b/BingImageGen.py @@ -136,8 +136,9 @@ class ImageGenAsync: raise Exception("No images") return normal_image_links - async def save_images(self, links: list, output_dir: str, - output_four_images: bool) -> list: + async def save_images( + self, links: list, output_dir: str, output_four_images: bool + ) -> list: """ Saves images to output directory """ @@ -151,25 +152,33 @@ class ImageGenAsync: image_name = str(uuid4()) image_path = os.path.join(output_dir, f"{image_name}.jpeg") try: - async with self.session.get(link, raise_for_status=True) as response: + async with self.session.get( + link, raise_for_status=True + ) as response: with open(image_path, "wb") as output_file: async for chunk in response.content.iter_chunked(8192): output_file.write(chunk) image_path_list.append(image_path) except aiohttp.client_exceptions.InvalidURL as url_exception: - raise Exception("Inappropriate contents found in the generated images. Please try again or try another prompt.") from url_exception # noqa: E501 + raise Exception( + "Inappropriate contents found in the generated images. Please try again or try another prompt." + ) from url_exception # noqa: E501 else: image_name = str(uuid4()) if links: link = links.pop() try: - async with self.session.get(link, raise_for_status=True) as response: + async with self.session.get( + link, raise_for_status=True + ) as response: image_path = os.path.join(output_dir, f"{image_name}.jpeg") with open(image_path, "wb") as output_file: async for chunk in response.content.iter_chunked(8192): output_file.write(chunk) image_path_list.append(image_path) except aiohttp.client_exceptions.InvalidURL as url_exception: - raise Exception("Inappropriate contents found in the generated images. Please try again or try another prompt.") from url_exception # noqa: E501 + raise Exception( + "Inappropriate contents found in the generated images. Please try again or try another prompt." + ) from url_exception # noqa: E501 return image_path_list diff --git a/askgpt.py b/askgpt.py index e7b2d86..0ab9df3 100644 --- a/askgpt.py +++ b/askgpt.py @@ -2,6 +2,7 @@ import aiohttp import asyncio import json from log import getlogger + logger = getlogger() @@ -22,8 +23,9 @@ class askGPT: max_try = 2 while max_try > 0: try: - async with self.session.post(url=api_endpoint, - json=jsons, headers=headers, timeout=120) as response: + async with self.session.post( + url=api_endpoint, json=jsons, headers=headers, timeout=120 + ) as response: status_code = response.status if not status_code == 200: # print failed reason @@ -34,6 +36,6 @@ class askGPT: continue resp = await response.read() - return json.loads(resp)['choices'][0]['message']['content'] + return json.loads(resp)["choices"][0]["message"]["content"] except Exception as e: raise Exception(e) diff --git a/bing.py b/bing.py index ddedff3..4685bf6 100644 --- a/bing.py +++ b/bing.py @@ -2,14 +2,20 @@ import aiohttp import json import asyncio from log import getlogger + # api_endpoint = "http://localhost:3000/conversation" logger = getlogger() class BingBot: - def __init__(self, session: aiohttp.ClientSession, bing_api_endpoint: str, jailbreakEnabled: bool = True): + def __init__( + self, + session: aiohttp.ClientSession, + bing_api_endpoint: str, + jailbreakEnabled: bool = True, + ): self.data = { - 'clientOptions.clientToUse': 'bing', + "clientOptions.clientToUse": "bing", } self.bing_api_endpoint = bing_api_endpoint @@ -18,14 +24,16 @@ class BingBot: self.jailbreakEnabled = jailbreakEnabled if self.jailbreakEnabled: - self.data['jailbreakConversationId'] = True + self.data["jailbreakConversationId"] = True async def ask_bing(self, prompt) -> str: - self.data['message'] = prompt + self.data["message"] = prompt max_try = 2 while max_try > 0: try: - resp = await self.session.post(url=self.bing_api_endpoint, json=self.data, timeout=120) + resp = await self.session.post( + url=self.bing_api_endpoint, json=self.data, timeout=120 + ) status_code = resp.status body = await resp.read() if not status_code == 200: @@ -37,16 +45,19 @@ class BingBot: continue json_body = json.loads(body) if self.jailbreakEnabled: - self.data['jailbreakConversationId'] = json_body['jailbreakConversationId'] - self.data['parentMessageId'] = json_body['messageId'] + self.data["jailbreakConversationId"] = json_body[ + "jailbreakConversationId" + ] + self.data["parentMessageId"] = json_body["messageId"] else: - self.data['conversationSignature'] = json_body['conversationSignature'] - self.data['conversationId'] = json_body['conversationId'] - self.data['clientId'] = json_body['clientId'] - self.data['invocationId'] = json_body['invocationId'] - return json_body['details']['adaptiveCards'][0]['body'][0]['text'] + self.data["conversationSignature"] = json_body[ + "conversationSignature" + ] + self.data["conversationId"] = json_body["conversationId"] + self.data["clientId"] = json_body["clientId"] + self.data["invocationId"] = json_body["invocationId"] + return json_body["details"]["adaptiveCards"][0]["body"][0]["text"] except Exception as e: logger.error("Error Exception", exc_info=True) - - + return "Error, please retry" diff --git a/flowise.py b/flowise.py index b51f2f6..bdddd85 100644 --- a/flowise.py +++ b/flowise.py @@ -1,5 +1,6 @@ import requests + def flowise_query(api_url: str, prompt: str, headers: dict = None) -> str: """ Sends a query to the Flowise API and returns the response. @@ -12,8 +13,9 @@ def flowise_query(api_url: str, prompt: str, headers: dict = None) -> str: str: The response from the API. """ if headers: - response = requests.post(api_url, json={"question": prompt}, - headers=headers, timeout=120) + response = requests.post( + api_url, json={"question": prompt}, headers=headers, timeout=120 + ) else: response = requests.post(api_url, json={"question": prompt}, timeout=120) return response.text diff --git a/send_image.py b/send_image.py index 3e363d5..c70fd69 100644 --- a/send_image.py +++ b/send_image.py @@ -12,8 +12,7 @@ from log import getlogger logger = getlogger() -async def send_room_image(client: AsyncClient, - room_id: str, image: str): +async def send_room_image(client: AsyncClient, room_id: str, image: str): """ image: image path """ @@ -36,8 +35,10 @@ async def send_room_image(client: AsyncClient, await client.room_send( room_id, message_type="m.room.message", - content={"msgtype": "m.text", - "body": f"Failed to generate image. Failure response: {resp}", }, + content={ + "msgtype": "m.text", + "body": f"Failed to generate image. Failure response: {resp}", + }, ignore_unverified_devices=True, ) return @@ -57,6 +58,5 @@ async def send_room_image(client: AsyncClient, try: await client.room_send(room_id, message_type="m.room.message", content=content) except Exception as e: - logger.error( - f"Image send of file {image} failed.\n Error: {e}", exc_info=True) + logger.error(f"Image send of file {image} failed.\n Error: {e}", exc_info=True) raise Exception(e) diff --git a/send_message.py b/send_message.py index 3f714c3..537897e 100644 --- a/send_message.py +++ b/send_message.py @@ -3,15 +3,20 @@ import re import markdown -async def send_room_message(client: AsyncClient, - room_id: str, - reply_message: str, - sender_id: str = '', - user_message: str = '', - reply_to_event_id: str = '', - markdown_formatted: bool = False) -> None: - NORMAL_BODY = content = {"msgtype": "m.text", "body": reply_message, } - if reply_to_event_id == '': +async def send_room_message( + client: AsyncClient, + room_id: str, + reply_message: str, + sender_id: str = "", + user_message: str = "", + reply_to_event_id: str = "", + markdown_formatted: bool = False, +) -> None: + NORMAL_BODY = content = { + "msgtype": "m.text", + "body": reply_message, + } + if reply_to_event_id == "": if markdown_formatted: # only format message contains multiline codes, *, | if re.search(r"```|\*|\|", reply_message) is not None: @@ -19,7 +24,9 @@ async def send_room_message(client: AsyncClient, "msgtype": "m.text", "body": reply_message, "format": "org.matrix.custom.html", - "formatted_body": markdown.markdown(reply_message, extensions=['nl2br', 'tables', 'fenced_code']) + "formatted_body": markdown.markdown( + reply_message, extensions=["nl2br", "tables", "fenced_code"] + ), } else: content = NORMAL_BODY @@ -27,14 +34,30 @@ async def send_room_message(client: AsyncClient, else: content = NORMAL_BODY else: - body = r'> <' + sender_id + r'> ' + user_message + r'\n\n' + reply_message - format = r'org.matrix.custom.html' - formatted_body = r'
In reply to ' + sender_id \ - + r'
' + user_message + r'
' + reply_message + body = r"> <" + sender_id + r"> " + user_message + r"\n\n" + reply_message + format = r"org.matrix.custom.html" + formatted_body = ( + r'
In reply to ' + + sender_id + + r"
" + + user_message + + r"
" + + reply_message + ) - content = {"msgtype": "m.text", "body": body, "format": format, "formatted_body": formatted_body, - "m.relates_to": {"m.in_reply_to": {"event_id": reply_to_event_id}}, } + content = { + "msgtype": "m.text", + "body": body, + "format": format, + "formatted_body": formatted_body, + "m.relates_to": {"m.in_reply_to": {"event_id": reply_to_event_id}}, + } await client.room_send( room_id, message_type="m.room.message", diff --git a/v3.py b/v3.py index b708bfe..f90363b 100644 --- a/v3.py +++ b/v3.py @@ -56,8 +56,7 @@ class Chatbot: }, ) proxy = ( - proxy or os.environ.get( - "all_proxy") or os.environ.get("ALL_PROXY") or None + proxy or os.environ.get("all_proxy") or os.environ.get("ALL_PROXY") or None ) if proxy: @@ -160,10 +159,8 @@ class Chatbot: self.__truncate_conversation(convo_id=convo_id) # Get response response = self.session.post( - os.environ.get( - "API_URL") or "https://api.openai.com/v1/chat/completions", - headers={ - "Authorization": f"Bearer {kwargs.get('api_key', self.api_key)}"}, + os.environ.get("API_URL") or "https://api.openai.com/v1/chat/completions", + headers={"Authorization": f"Bearer {kwargs.get('api_key', self.api_key)}"}, json={ "model": self.engine, "messages": self.conversation[convo_id], @@ -209,8 +206,7 @@ class Chatbot: content = delta["content"] full_response += content yield content - self.add_to_conversation( - full_response, response_role, convo_id=convo_id) + self.add_to_conversation(full_response, response_role, convo_id=convo_id) async def ask_stream_async( self, @@ -230,10 +226,8 @@ class Chatbot: # Get response async with self.aclient.stream( "post", - os.environ.get( - "API_URL") or "https://api.openai.com/v1/chat/completions", - headers={ - "Authorization": f"Bearer {kwargs.get('api_key', self.api_key)}"}, + os.environ.get("API_URL") or "https://api.openai.com/v1/chat/completions", + headers={"Authorization": f"Bearer {kwargs.get('api_key', self.api_key)}"}, json={ "model": self.engine, "messages": self.conversation[convo_id], @@ -281,8 +275,7 @@ class Chatbot: content: str = delta["content"] full_response += content yield content - self.add_to_conversation( - full_response, response_role, convo_id=convo_id) + self.add_to_conversation(full_response, response_role, convo_id=convo_id) async def ask_async( self,