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
Next Next commit
feat: support azure openai service
  • Loading branch information
iuiaoin committed Jul 19, 2023
commit c2be7edc38bb8acc2da695e7918aa0b7f74c34d0
43 changes: 43 additions & 0 deletions bot/azure_chatgpt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import openai
import requests
from bot.chatgpt import ChatGPTBot
from common.singleton import singleton
from config import conf
from utils.log import logger
from common.reply import Reply, ReplyType


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

def reply_img(self, query):
create_image_size = conf().get("create_image_size", "256x256")
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}")
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")

def create_img(self, query):
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: 5 additions & 4 deletions bot/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def __init__(self):
openai.api_base = api_base
if proxy:
openai.proxy = proxy
self.args = {
"model": conf().get("model"),
"temperature": conf().get("temperature"),
}

def reply(self, context: Context):
query = context.query
Expand Down Expand Up @@ -46,16 +50,13 @@ def reply_img(self, query):
return Reply(ReplyType.TEXT, "Image created failed")

def reply_text(self, session):
model = conf().get("model")
temperature = conf().get("temperature")
try:
response = openai.ChatCompletion.create(
model=model,
messages=session,
temperature=temperature,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
**self.args,
)
return {
"total_tokens": response["usage"]["total_tokens"],
Expand Down
12 changes: 7 additions & 5 deletions common/singleton.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
def singleton(cls):
instances = {}

def get_instance(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
class Singleton(cls):
def __new__(cls, *args, **kwargs):
if cls not in instances:
instances[cls] = super(Singleton, cls).__new__(cls, *args, **kwargs)
return instances[cls]

return get_instance
Singleton.__name__ = cls.__name__
return Singleton
1 change: 1 addition & 0 deletions config.template.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"openai_api_key": "YOUR API SECRET KEY",
"model": "gpt-3.5-turbo",
"azure_deployment_id": "",
"role_desc": "You are a helpful assistant.",
"session_expired_duration": 3600,
"max_tokens": 1000,
Expand Down