From 40b10605e94b43baea04b0fa5f0b5b79d5610af9 Mon Sep 17 00:00:00 2001 From: orenzhang Date: Wed, 18 Dec 2024 21:03:27 +0800 Subject: [PATCH] feat(client): use model settings for midjourney instead of env --- .pre-commit-config.yaml | 2 +- README.md | 12 +++--------- README_ZH.md | 7 ------- apps/chat/client/midjourney.py | 17 ++++++++--------- apps/cos/client.py | 6 +++--- entry/settings.py | 9 --------- env.example | 7 ------- 7 files changed, 15 insertions(+), 45 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b0de07b..a273ca8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,7 +9,7 @@ repos: hooks: - id: check-merge-conflict - repo: https://github.com/psf/black - rev: 22.8.0 + rev: 24.10.0 hooks: - id: black language_version: python3.12 diff --git a/README.md b/README.md index 9a2d4b0..d91c072 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ LOG_LEVEL=INFO APP_CODE=chatgpt-api APP_SECRET= BACKEND_HOST=api.example.com +BACKEND_URL=https://api.example.com FRONTEND_URL=https://example.com DB_NAME=chatgpt_api DB_USER=chatgpt_api @@ -37,22 +38,15 @@ DB_PORT=3306 REDIS_HOST= REDIS_PORT=6379 REDIS_PASSWORD= -REDIS_DB= +REDIS_DB=0 SESSION_COOKIE_DOMAIN=.example.com +OVINC_WEB_URL= OVINC_API_DOMAIN= -OPENAI_HTTP_PROXY_URL=http://proxy.example.com:3814 QCLOUD_SECRET_ID= QCLOUD_SECRET_KEY= QCLOUD_COS_URL=https://cos.example.com QCLOUD_COS_BUCKET=example-cos-12345678 -GEMINI_API_KEY= -OPENAI_MAX_ALLOWED_TOKENS= -QIANFAN_ACCESS_KEY= -QIANFAN_SECRET_KEY= -DASHSCOPE_API_KEY= DJANGO_SETTINGS_MODULE=entry.settings -KIMI_API_KEY= -KIMI_API_BASE_URL=https://api.moonshot.cn/v1 WXPAY_ENABLED=0 WXPAY_PRIVATE_KEY_SERIAL_NO= WXPAY_PRIVATE_KEY_PATH= diff --git a/README_ZH.md b/README_ZH.md index 50e8b20..9ca2399 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -40,19 +40,12 @@ REDIS_PASSWORD= REDIS_DB= SESSION_COOKIE_DOMAIN=.example.com OVINC_API_DOMAIN=<请联系我们获取> -OPENAI_HTTP_PROXY_URL=http://proxy.example.com:3814 QCLOUD_SECRET_ID=<腾讯云 SecretID> QCLOUD_SECRET_KEY=<腾讯云 SecretKey> QCLOUD_COS_URL=https://cos.example.com QCLOUD_COS_BUCKET=example-cos-12345678 -GEMINI_API_KEY= OPENAI_MAX_ALLOWED_TOKENS=<对话最大 Token 数> -QIANFAN_ACCESS_KEY=<百度千帆 Access Key> -QIANFAN_SECRET_KEY=<百度千帆 Secret Key> -DASHSCOPE_API_KEY=<阿里百炼平台 API Key> DJANGO_SETTINGS_MODULE=entry.settings -KIMI_API_KEY= -KIMI_API_BASE_URL=https://api.moonshot.cn/v1 WXPAY_ENABLED=0" WXPAY_PRIVATE_KEY_SERIAL_NO=<微信支付证书序列号> WXPAY_PRIVATE_KEY_PATH=<微信支付密钥路径> diff --git a/apps/chat/client/midjourney.py b/apps/chat/client/midjourney.py index ed633f1..957313a 100644 --- a/apps/chat/client/midjourney.py +++ b/apps/chat/client/midjourney.py @@ -2,7 +2,6 @@ import time import uuid -from django.conf import settings from httpx import AsyncClient from opentelemetry.trace import SpanKind from ovinc_client.core.logger import logger @@ -23,28 +22,28 @@ class MidjourneyClient(BaseClient): async def _chat(self, *args, **kwargs) -> any: client = AsyncClient( http2=True, - headers={"Authorization": f"Bearer {settings.MIDJOURNEY_API_KEY}"}, - base_url=settings.MIDJOURNEY_API_BASE_URL, - proxy=settings.OPENAI_HTTP_PROXY_URL or None, - timeout=settings.MIDJOURNEY_API_TIMEOUT, + headers={"Authorization": f"Bearer {self.model_settings.get("api_key")}"}, + base_url=self.model_settings.get("base_url"), + proxy=self.model_settings.get("proxy"), + timeout=self.model_settings.get("timeout"), ) # call midjourney api try: with self.start_span(SpanType.API, SpanKind.CLIENT): # submit job response = await client.post( - url=settings.MIDJOURNEY_IMAGINE_API_PATH, json={"prompt": self.messages[-1].content} + url=self.model_settings.get("imaging_path"), json={"prompt": self.messages[-1].content} ) result_id = response.json()["result"] # wait for result start_time = time.time() - while time.time() - start_time < settings.MIDJOURNEY_IMAGE_JOB_TIMEOUT: - result = await client.get(url=settings.MIDJOURNEY_TASK_RESULT_API_PATH.format(id=result_id)) + while time.time() - start_time < self.model_settings.get("wait_timeout", 600): + result = await client.get(url=self.model_settings.get("result_path").format(id=result_id)) result_data = result.json() # if not finished, continue loop if result_data["status"] not in [MidjourneyResult.FAILURE, MidjourneyResult.SUCCESS]: yield "" - await asyncio.sleep(settings.MIDJOURNEY_IMAGE_JOB_INTERVAL) + await asyncio.sleep(self.model_settings.get("no_result_sleep", 5)) continue # if failed if result_data["status"] == MidjourneyResult.FAILURE: diff --git a/apps/cos/client.py b/apps/cos/client.py index 00dd6f6..d4722e6 100644 --- a/apps/cos/client.py +++ b/apps/cos/client.py @@ -119,9 +119,9 @@ async def generate_cos_upload_credential(self, filename: str) -> COSCredential: start_time=response["startTime"], expired_time=response["expiredTime"], use_accelerate=settings.QCLOUD_COS_USE_ACCELERATE, - image_format=settings.QCLOUD_COS_IMAGE_STYLE - if key.split(".")[-1] in settings.QCLOUD_COS_IMAGE_SUFFIX - else "", + image_format=( + settings.QCLOUD_COS_IMAGE_STYLE if key.split(".")[-1] in settings.QCLOUD_COS_IMAGE_SUFFIX else "" + ), cdn_sign=TCloudUrlParser.sign("/" + quote(key)), ) except Exception as err: diff --git a/entry/settings.py b/entry/settings.py index d2215d2..1e52ce0 100644 --- a/entry/settings.py +++ b/entry/settings.py @@ -284,12 +284,3 @@ WXPAY_UNIT = os.getenv("WXPAY_UNIT", "") WXPAY_ORDER_TIMEOUT = int(os.getenv("WXPAY_ORDER_TIMEOUT", str(60 * 10))) WXPAY_SUPPORT_FAPIAO = strtobool(os.getenv("WXPAY_SUPPORT_FAPIAO", "False")) - -# Midjourney -MIDJOURNEY_API_BASE_URL = os.getenv("MIDJOURNEY_API_BASE_URL", "") -MIDJOURNEY_API_KEY = os.getenv("MIDJOURNEY_API_KEY", "") -MIDJOURNEY_IMAGINE_API_PATH = os.getenv("MIDJOURNEY_IMAGINE_API_PATH", "/fast/mj/submit/imagine") -MIDJOURNEY_TASK_RESULT_API_PATH = os.getenv("MIDJOURNEY_TASK_RESULT_API_PATH", "/mj/task/{id}/fetch") -MIDJOURNEY_IMAGE_JOB_INTERVAL = int(os.getenv("MIDJOURNEY_IMAGE_JOB_INTERVAL", "5")) -MIDJOURNEY_IMAGE_JOB_TIMEOUT = int(os.getenv("MIDJOURNEY_IMAGE_JOB_TIMEOUT", "600")) -MIDJOURNEY_API_TIMEOUT = int(os.getenv("MIDJOURNEY_API_TIMEOUT", "60")) diff --git a/env.example b/env.example index 2c482c3..5f31828 100644 --- a/env.example +++ b/env.example @@ -20,18 +20,11 @@ REDIS_DB=0 SESSION_COOKIE_DOMAIN=.example.com OVINC_WEB_URL= OVINC_API_DOMAIN= -OPENAI_HTTP_PROXY_URL=http://proxy.example.com:3814 QCLOUD_SECRET_ID= QCLOUD_SECRET_KEY= QCLOUD_COS_URL=https://cos.example.com QCLOUD_COS_BUCKET=example-cos-12345678 -GEMINI_API_KEY= -QIANFAN_ACCESS_KEY= -QIANFAN_SECRET_KEY= -DASHSCOPE_API_KEY= DJANGO_SETTINGS_MODULE=entry.settings -KIMI_API_KEY= -KIMI_API_BASE_URL=https://api.moonshot.cn/v1 WXPAY_ENABLED=0 WXPAY_PRIVATE_KEY_SERIAL_NO= WXPAY_PRIVATE_KEY_PATH=