Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add sponsorship #66

Merged
merged 10 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: refactor azure bot
  • Loading branch information
iuiaoin committed Jul 20, 2023
commit d12763448c1b3154687700896f8dbe3dc1837db5
21 changes: 1 addition & 20 deletions bot/azure_chatgpt.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
import openai
import requests
from bot.chatgpt import ChatGPTBot
from config import conf
from utils.log import logger
from common.reply import Reply, ReplyType


class AzureChatGPTBot(ChatGPTBot):
def __init__(self):
super().__init__()
openai.api_type = "azure"
openai.api_version = "2023-03-15-preview"
openai.api_version = "2023-06-01-preview"
self.args["deployment_id"] = conf().get("azure_deployment_id")

def reply_img(self, query) -> Reply:
url = f"{openai.api_base}dalle/text-to-image?api-version=2022-08-03-preview"
headers = {"api-key": openai.api_key, "Content-Type": "application/json"}
create_image_size = conf().get("create_image_size", "256x256")
try:
body = {"caption": query, "resolution": create_image_size}
submission = requests.post(url, headers=headers, json=body)
operation_location = submission.headers["Operation-Location"]
response = requests.get(operation_location, headers=headers)
image_url = response.json()["result"]["contentUrl"]
logger.info(f"[ChatGPT] Image={image_url}")
return Reply(ReplyType.IMAGE, image_url)
except Exception as e:
logger.error(f"[ChatGPT] Create image failed: {e}")
return Reply(ReplyType.TEXT, "Image created failed")
9 changes: 6 additions & 3 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

@singleton
class Bot:
def reply(self, context: Context) -> Reply:
def __init__(self):
use_azure_chatgpt = conf().get("use_azure_chatgpt", False)
if use_azure_chatgpt:
from bot.azure_chatgpt import AzureChatGPTBot

return AzureChatGPTBot().reply(context)
self.bot = AzureChatGPTBot()
else:
from bot.chatgpt import ChatGPTBot

return ChatGPTBot().reply(context)
self.bot = ChatGPTBot()

def reply(self, context: Context) -> Reply:
return self.bot.reply(context)
19 changes: 10 additions & 9 deletions bot/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@ def __init__(self):
openai.api_base = api_base
if proxy:
openai.proxy = proxy
self.name = self.__class__.__name__
self.args = {
"model": conf().get("model"),
"temperature": conf().get("temperature"),
}

def reply(self, context: Context) -> Reply:
query = context.query
logger.info(f"[ChatGPT] Query={query}")
logger.info(f"[{self.name}] Query={query}")
if context.type == ContextType.CREATE_IMAGE:
return self.reply_img(query)
else:
session_id = context.session_id
session = Session.build_session_query(context)
response = self.reply_text(session)
logger.info(f"[ChatGPT] Response={response['content']}")
logger.info(f"[{self.name}] Response={response['content']}")
if response["completion_tokens"] > 0:
Session.save_session(
response["content"], session_id, response["total_tokens"]
Expand All @@ -41,10 +42,10 @@ def reply_img(self, query) -> Reply:
try:
response = openai.Image.create(prompt=query, n=1, size=create_image_size)
image_url = response["data"][0]["url"]
logger.info(f"[ChatGPT] Image={image_url}")
logger.info(f"[{self.name}] Image={image_url}")
return Reply(ReplyType.IMAGE, image_url)
except Exception as e:
logger.error(f"[ChatGPT] Create image failed: {e}")
logger.error(f"[{self.name}] Create image failed: {e}")
return Reply(ReplyType.TEXT, "Image created failed")

def reply_text(self, session):
Expand All @@ -64,18 +65,18 @@ def reply_text(self, session):
except Exception as e:
result = {"completion_tokens": 0, "content": "Please ask me again"}
if isinstance(e, openai.error.RateLimitError):
logger.warn(f"[ChatGPT] RateLimitError: {e}")
logger.warn(f"[{self.name}] RateLimitError: {e}")
result["content"] = "Ask too frequently, please try again in 20s"
elif isinstance(e, openai.error.APIConnectionError):
logger.warn(f"[ChatGPT] APIConnectionError: {e}")
logger.warn(f"[{self.name}] APIConnectionError: {e}")
result[
"content"
] = "I cannot connect the server, please check the network and try again"
elif isinstance(e, openai.error.Timeout):
logger.warn(f"[ChatGPT] Timeout: {e}")
logger.warn(f"[{self.name}] Timeout: {e}")
result["content"] = "I didn't receive your message, please try again"
elif isinstance(e, openai.error.APIError):
logger.warn(f"[ChatGPT] APIError: {e}")
logger.warn(f"[{self.name}] APIError: {e}")
else:
logger.exception(f"[ChatGPT] Exception: {e}")
logger.exception(f"[{self.name}] Exception: {e}")
return result