format codes

This commit is contained in:
hibobmaster 2023-05-30 10:26:39 +08:00
parent ac3bd2a557
commit 95891a7fe7
No known key found for this signature in database
7 changed files with 102 additions and 62 deletions

View File

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

View File

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

39
bing.py
View File

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

View File

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

View File

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

View File

@ -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'<mx-reply><blockquote><a href="https://matrix.to/#/' + room_id + r'/' + reply_to_event_id \
+ r'">In reply to</a> <a href="https://matrix.to/#/' + sender_id + r'">' + sender_id \
+ r'</a><br>' + user_message + r'</blockquote></mx-reply>' + reply_message
body = r"> <" + sender_id + r"> " + user_message + r"\n\n" + reply_message
format = r"org.matrix.custom.html"
formatted_body = (
r'<mx-reply><blockquote><a href="https://matrix.to/#/'
+ room_id
+ r"/"
+ reply_to_event_id
+ r'">In reply to</a> <a href="https://matrix.to/#/'
+ sender_id
+ r'">'
+ sender_id
+ r"</a><br>"
+ user_message
+ r"</blockquote></mx-reply>"
+ 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",

21
v3.py
View File

@ -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,