format codes

This commit is contained in:
hibobmaster 2023-05-30 10:26:39 +08:00
parent ac3bd2a557
commit 95891a7fe7
Signed by: bobmaster
SSH key fingerprint: SHA256:5ZYgd8fg+PcNZNy4SzcSKu5JtqZyBF8kUhY7/k2viDk
7 changed files with 102 additions and 62 deletions

View file

@ -136,8 +136,9 @@ class ImageGenAsync:
raise Exception("No images") raise Exception("No images")
return normal_image_links return normal_image_links
async def save_images(self, links: list, output_dir: str, async def save_images(
output_four_images: bool) -> list: self, links: list, output_dir: str, output_four_images: bool
) -> list:
""" """
Saves images to output directory Saves images to output directory
""" """
@ -151,25 +152,33 @@ class ImageGenAsync:
image_name = str(uuid4()) image_name = str(uuid4())
image_path = os.path.join(output_dir, f"{image_name}.jpeg") image_path = os.path.join(output_dir, f"{image_name}.jpeg")
try: 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: with open(image_path, "wb") as output_file:
async for chunk in response.content.iter_chunked(8192): async for chunk in response.content.iter_chunked(8192):
output_file.write(chunk) output_file.write(chunk)
image_path_list.append(image_path) image_path_list.append(image_path)
except aiohttp.client_exceptions.InvalidURL as url_exception: 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: else:
image_name = str(uuid4()) image_name = str(uuid4())
if links: if links:
link = links.pop() link = links.pop()
try: 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") image_path = os.path.join(output_dir, f"{image_name}.jpeg")
with open(image_path, "wb") as output_file: with open(image_path, "wb") as output_file:
async for chunk in response.content.iter_chunked(8192): async for chunk in response.content.iter_chunked(8192):
output_file.write(chunk) output_file.write(chunk)
image_path_list.append(image_path) image_path_list.append(image_path)
except aiohttp.client_exceptions.InvalidURL as url_exception: 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 return image_path_list

View file

@ -2,6 +2,7 @@ import aiohttp
import asyncio import asyncio
import json import json
from log import getlogger from log import getlogger
logger = getlogger() logger = getlogger()
@ -22,8 +23,9 @@ class askGPT:
max_try = 2 max_try = 2
while max_try > 0: while max_try > 0:
try: try:
async with self.session.post(url=api_endpoint, async with self.session.post(
json=jsons, headers=headers, timeout=120) as response: url=api_endpoint, json=jsons, headers=headers, timeout=120
) as response:
status_code = response.status status_code = response.status
if not status_code == 200: if not status_code == 200:
# print failed reason # print failed reason
@ -34,6 +36,6 @@ class askGPT:
continue continue
resp = await response.read() resp = await response.read()
return json.loads(resp)['choices'][0]['message']['content'] return json.loads(resp)["choices"][0]["message"]["content"]
except Exception as e: except Exception as e:
raise Exception(e) raise Exception(e)

39
bing.py
View file

@ -2,14 +2,20 @@ import aiohttp
import json import json
import asyncio import asyncio
from log import getlogger from log import getlogger
# api_endpoint = "http://localhost:3000/conversation" # api_endpoint = "http://localhost:3000/conversation"
logger = getlogger() logger = getlogger()
class BingBot: 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 = { self.data = {
'clientOptions.clientToUse': 'bing', "clientOptions.clientToUse": "bing",
} }
self.bing_api_endpoint = bing_api_endpoint self.bing_api_endpoint = bing_api_endpoint
@ -18,14 +24,16 @@ class BingBot:
self.jailbreakEnabled = jailbreakEnabled self.jailbreakEnabled = jailbreakEnabled
if self.jailbreakEnabled: if self.jailbreakEnabled:
self.data['jailbreakConversationId'] = True self.data["jailbreakConversationId"] = True
async def ask_bing(self, prompt) -> str: async def ask_bing(self, prompt) -> str:
self.data['message'] = prompt self.data["message"] = prompt
max_try = 2 max_try = 2
while max_try > 0: while max_try > 0:
try: 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 status_code = resp.status
body = await resp.read() body = await resp.read()
if not status_code == 200: if not status_code == 200:
@ -37,16 +45,19 @@ class BingBot:
continue continue
json_body = json.loads(body) json_body = json.loads(body)
if self.jailbreakEnabled: if self.jailbreakEnabled:
self.data['jailbreakConversationId'] = json_body['jailbreakConversationId'] self.data["jailbreakConversationId"] = json_body[
self.data['parentMessageId'] = json_body['messageId'] "jailbreakConversationId"
]
self.data["parentMessageId"] = json_body["messageId"]
else: else:
self.data['conversationSignature'] = json_body['conversationSignature'] self.data["conversationSignature"] = json_body[
self.data['conversationId'] = json_body['conversationId'] "conversationSignature"
self.data['clientId'] = json_body['clientId'] ]
self.data['invocationId'] = json_body['invocationId'] self.data["conversationId"] = json_body["conversationId"]
return json_body['details']['adaptiveCards'][0]['body'][0]['text'] 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: except Exception as e:
logger.error("Error Exception", exc_info=True) logger.error("Error Exception", exc_info=True)
return "Error, please retry" return "Error, please retry"

View file

@ -1,5 +1,6 @@
import requests import requests
def flowise_query(api_url: str, prompt: str, headers: dict = None) -> str: def flowise_query(api_url: str, prompt: str, headers: dict = None) -> str:
""" """
Sends a query to the Flowise API and returns the response. 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. str: The response from the API.
""" """
if headers: if headers:
response = requests.post(api_url, json={"question": prompt}, response = requests.post(
headers=headers, timeout=120) api_url, json={"question": prompt}, headers=headers, timeout=120
)
else: else:
response = requests.post(api_url, json={"question": prompt}, timeout=120) response = requests.post(api_url, json={"question": prompt}, timeout=120)
return response.text return response.text

View file

@ -12,8 +12,7 @@ from log import getlogger
logger = getlogger() logger = getlogger()
async def send_room_image(client: AsyncClient, async def send_room_image(client: AsyncClient, room_id: str, image: str):
room_id: str, image: str):
""" """
image: image path image: image path
""" """
@ -36,8 +35,10 @@ async def send_room_image(client: AsyncClient,
await client.room_send( await client.room_send(
room_id, room_id,
message_type="m.room.message", message_type="m.room.message",
content={"msgtype": "m.text", content={
"body": f"Failed to generate image. Failure response: {resp}", }, "msgtype": "m.text",
"body": f"Failed to generate image. Failure response: {resp}",
},
ignore_unverified_devices=True, ignore_unverified_devices=True,
) )
return return
@ -57,6 +58,5 @@ async def send_room_image(client: AsyncClient,
try: try:
await client.room_send(room_id, message_type="m.room.message", content=content) await client.room_send(room_id, message_type="m.room.message", content=content)
except Exception as e: except Exception as e:
logger.error( logger.error(f"Image send of file {image} failed.\n Error: {e}", exc_info=True)
f"Image send of file {image} failed.\n Error: {e}", exc_info=True)
raise Exception(e) raise Exception(e)

View file

@ -3,15 +3,20 @@ import re
import markdown import markdown
async def send_room_message(client: AsyncClient, async def send_room_message(
room_id: str, client: AsyncClient,
reply_message: str, room_id: str,
sender_id: str = '', reply_message: str,
user_message: str = '', sender_id: str = "",
reply_to_event_id: str = '', user_message: str = "",
markdown_formatted: bool = False) -> None: reply_to_event_id: str = "",
NORMAL_BODY = content = {"msgtype": "m.text", "body": reply_message, } markdown_formatted: bool = False,
if reply_to_event_id == '': ) -> None:
NORMAL_BODY = content = {
"msgtype": "m.text",
"body": reply_message,
}
if reply_to_event_id == "":
if markdown_formatted: if markdown_formatted:
# only format message contains multiline codes, *, | # only format message contains multiline codes, *, |
if re.search(r"```|\*|\|", reply_message) is not None: if re.search(r"```|\*|\|", reply_message) is not None:
@ -19,7 +24,9 @@ async def send_room_message(client: AsyncClient,
"msgtype": "m.text", "msgtype": "m.text",
"body": reply_message, "body": reply_message,
"format": "org.matrix.custom.html", "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: else:
content = NORMAL_BODY content = NORMAL_BODY
@ -27,14 +34,30 @@ async def send_room_message(client: AsyncClient,
else: else:
content = NORMAL_BODY content = NORMAL_BODY
else: else:
body = r'> <' + sender_id + r'> ' + user_message + r'\n\n' + reply_message body = r"> <" + sender_id + r"> " + user_message + r"\n\n" + reply_message
format = r'org.matrix.custom.html' format = r"org.matrix.custom.html"
formatted_body = r'<mx-reply><blockquote><a href="https://matrix.to/#/' + room_id + r'/' + reply_to_event_id \ formatted_body = (
+ r'">In reply to</a> <a href="https://matrix.to/#/' + sender_id + r'">' + sender_id \ r'<mx-reply><blockquote><a href="https://matrix.to/#/'
+ r'</a><br>' + user_message + r'</blockquote></mx-reply>' + reply_message + 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, content = {
"m.relates_to": {"m.in_reply_to": {"event_id": reply_to_event_id}}, } "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( await client.room_send(
room_id, room_id,
message_type="m.room.message", message_type="m.room.message",

21
v3.py
View file

@ -56,8 +56,7 @@ class Chatbot:
}, },
) )
proxy = ( proxy = (
proxy or os.environ.get( proxy or os.environ.get("all_proxy") or os.environ.get("ALL_PROXY") or None
"all_proxy") or os.environ.get("ALL_PROXY") or None
) )
if proxy: if proxy:
@ -160,10 +159,8 @@ class Chatbot:
self.__truncate_conversation(convo_id=convo_id) self.__truncate_conversation(convo_id=convo_id)
# Get response # Get response
response = self.session.post( response = self.session.post(
os.environ.get( os.environ.get("API_URL") or "https://api.openai.com/v1/chat/completions",
"API_URL") or "https://api.openai.com/v1/chat/completions", headers={"Authorization": f"Bearer {kwargs.get('api_key', self.api_key)}"},
headers={
"Authorization": f"Bearer {kwargs.get('api_key', self.api_key)}"},
json={ json={
"model": self.engine, "model": self.engine,
"messages": self.conversation[convo_id], "messages": self.conversation[convo_id],
@ -209,8 +206,7 @@ class Chatbot:
content = delta["content"] content = delta["content"]
full_response += content full_response += content
yield content yield content
self.add_to_conversation( self.add_to_conversation(full_response, response_role, convo_id=convo_id)
full_response, response_role, convo_id=convo_id)
async def ask_stream_async( async def ask_stream_async(
self, self,
@ -230,10 +226,8 @@ class Chatbot:
# Get response # Get response
async with self.aclient.stream( async with self.aclient.stream(
"post", "post",
os.environ.get( os.environ.get("API_URL") or "https://api.openai.com/v1/chat/completions",
"API_URL") or "https://api.openai.com/v1/chat/completions", headers={"Authorization": f"Bearer {kwargs.get('api_key', self.api_key)}"},
headers={
"Authorization": f"Bearer {kwargs.get('api_key', self.api_key)}"},
json={ json={
"model": self.engine, "model": self.engine,
"messages": self.conversation[convo_id], "messages": self.conversation[convo_id],
@ -281,8 +275,7 @@ class Chatbot:
content: str = delta["content"] content: str = delta["content"]
full_response += content full_response += content
yield content yield content
self.add_to_conversation( self.add_to_conversation(full_response, response_role, convo_id=convo_id)
full_response, response_role, convo_id=convo_id)
async def ask_async( async def ask_async(
self, self,