From 9d394adf2465f9574f625bb397ed208051d06449 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 12 Mar 2024 08:32:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1.=E4=BF=AE=E5=A4=8DAzure=20Openai=20Dalle?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=202.=E5=A2=9E=E5=8A=A0Azure=20Openai=20Dalle?= =?UTF-8?q?3=20=E8=AF=B7=E6=B1=82=E5=8F=82=E6=95=B0=203.=E5=B0=86=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E5=9B=9E=E5=A4=8D=E6=96=87=E5=AD=97=E5=92=8C=E5=9B=9E?= =?UTF-8?q?=E5=A4=8DDalle3=E7=9A=84Azure=20Openai=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E5=88=86=E7=A6=BB=E5=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/chatgpt/chat_gpt_bot.py | 63 ++++++++++++++++++++++++------------- config.py | 7 +++++ 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/bot/chatgpt/chat_gpt_bot.py b/bot/chatgpt/chat_gpt_bot.py index 979ce4c4e..0ae02d3a1 100644 --- a/bot/chatgpt/chat_gpt_bot.py +++ b/bot/chatgpt/chat_gpt_bot.py @@ -171,24 +171,45 @@ def __init__(self): self.args["deployment_id"] = conf().get("azure_deployment_id") def create_img(self, query, retry_count=0, api_key=None): - api_version = "2022-08-03-preview" - url = "{}dalle/text-to-image?api-version={}".format(openai.api_base, api_version) - api_key = api_key or openai.api_key - headers = {"api-key": api_key, "Content-Type": "application/json"} - try: - body = {"caption": query, "resolution": conf().get("image_create_size", "256x256")} - submission = requests.post(url, headers=headers, json=body) - operation_location = submission.headers["Operation-Location"] - retry_after = submission.headers["Retry-after"] - status = "" - image_url = "" - while status != "Succeeded": - logger.info("waiting for image create..., " + status + ",retry after " + retry_after + " seconds") - time.sleep(int(retry_after)) - response = requests.get(operation_location, headers=headers) - status = response.json()["status"] - image_url = response.json()["result"]["contentUrl"] - return True, image_url - except Exception as e: - logger.error("create image error: {}".format(e)) - return False, "图片生成失败" + text_to_image_model = conf().get("text_to_image") + if text_to_image_model == "dall-e-2": + api_version = "2023-06-01-preview" + endpoint = conf().get("azure_openai_dalle_api_base","open_ai_api_base") + # 检查endpoint是否以/结尾 + if not endpoint.endswith("/"): + endpoint = endpoint + "/" + url = "{}openai/images/generations:submit?api-version={}".format(endpoint, api_version) + api_key = conf().get("azure_openai_dalle_api_key","open_ai_api_key") + headers = {"api-key": api_key, "Content-Type": "application/json"} + try: + body = {"prompt": query, "size": conf().get("image_create_size", "256x256"),"n": 1} + submission = requests.post(url, headers=headers, json=body) + operation_location = submission.headers['operation-location'] + status = "" + while (status != "succeeded"): + response = requests.get(operation_location, headers=headers) + status = response.json()['status'] + image_url = response.json()['result']['data'][0]['url'] + return True, image_url + except Exception as e: + logger.error("create image error: {}".format(e)) + return False, "图片生成失败" + elif text_to_image_model == "dall-e-3": + api_version = conf().get("azure_api_version", "2024-02-15-preview") + endpoint = conf().get("azure_openai_dalle_api_base","open_ai_api_base") + # 检查endpoint是否以/结尾 + if not endpoint.endswith("/"): + endpoint = endpoint + "/" + url = "{}openai/deployments/{}/images/generations?api-version={}".format(endpoint, conf().get("azure_openai_dalle_deployment_id","text_to_image"),api_version) + api_key = conf().get("azure_openai_dalle_api_key","open_ai_api_key") + headers = {"api-key": api_key, "Content-Type": "application/json"} + try: + body = {"prompt": query, "size": conf().get("image_create_size", "1024x1024"), "quality": conf().get("dalle3_image_quality", "standard")} + submission = requests.post(url, headers=headers, json=body) + image_url = submission.json()['data'][0]['url'] + return True, image_url + except Exception as e: + logger.error("create image error: {}".format(e)) + return False, "图片生成失败" + else: + return False, "图片生成失败,未配置text_to_image参数" diff --git a/config.py b/config.py index 154c633fb..2d99c6433 100644 --- a/config.py +++ b/config.py @@ -36,6 +36,13 @@ "group_welcome_msg": "", # 配置新人进群固定欢迎语,不配置则使用随机风格欢迎 "trigger_by_self": False, # 是否允许机器人触发 "text_to_image": "dall-e-2", # 图片生成模型,可选 dall-e-2, dall-e-3 + # Azure OpenAI dall-e-3 配置 + "dalle3_image_style": "vivid", # 图片生成dalle3的风格,可选有 vivid, natural + "dalle3_image_quality": "hd", # 图片生成dalle3的质量,可选有 standard, hd + # Azure OpenAI DALL-E API 配置, 当use_azure_chatgpt为true时,用于将文字回复的资源和Dall-E的资源分开. + "azure_openai_dalle_api_base": "", # [可选] azure openai 用于回复图片的资源 endpoint,默认使用 open_ai_api_base + "azure_openai_dalle_api_key": "", # [可选] azure openai 用于回复图片的资源 key,默认使用 open_ai_api_key + "azure_openai_dalle_deployment_id":"", # [可选] azure openai 用于回复图片的资源 deployment id,默认使用 text_to_image "image_proxy": True, # 是否需要图片代理,国内访问LinkAI时需要 "image_create_prefix": ["画", "看", "找"], # 开启图片回复的前缀 "concurrency_in_session": 1, # 同一会话最多有多少条消息在处理中,大于1可能乱序 From 22c7f8fe9e09fb8794476a748427be774a010251 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 30 Mar 2024 01:07:52 +0800 Subject: [PATCH 2/2] add dall-e-2 retry_count limit --- bot/chatgpt/chat_gpt_bot.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bot/chatgpt/chat_gpt_bot.py b/bot/chatgpt/chat_gpt_bot.py index 0ae02d3a1..8f7110738 100644 --- a/bot/chatgpt/chat_gpt_bot.py +++ b/bot/chatgpt/chat_gpt_bot.py @@ -187,8 +187,11 @@ def create_img(self, query, retry_count=0, api_key=None): operation_location = submission.headers['operation-location'] status = "" while (status != "succeeded"): + if retry_count > 3: + return False, "图片生成失败" response = requests.get(operation_location, headers=headers) status = response.json()['status'] + retry_count += 1 image_url = response.json()['result']['data'][0]['url'] return True, image_url except Exception as e: