Skip to content

Commit

Permalink
feat(client): use model settings for midjourney instead of env
Browse files Browse the repository at this point in the history
  • Loading branch information
OrenZhang committed Dec 18, 2024
1 parent 6f14dcf commit 40b1060
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ LOG_LEVEL=INFO
APP_CODE=chatgpt-api
APP_SECRET=<Unmodifiable after generation>
BACKEND_HOST=api.example.com
BACKEND_URL=https://api.example.com
FRONTEND_URL=https://example.com
DB_NAME=chatgpt_api
DB_USER=chatgpt_api
Expand All @@ -37,22 +38,15 @@ DB_PORT=3306
REDIS_HOST=<Redis Host>
REDIS_PORT=6379
REDIS_PASSWORD=<Redis Password>
REDIS_DB=<Redis DB>
REDIS_DB=0
SESSION_COOKIE_DOMAIN=.example.com
OVINC_WEB_URL=<Contact us to obtain>
OVINC_API_DOMAIN=<Contact us to obtain>
OPENAI_HTTP_PROXY_URL=http://proxy.example.com:3814
QCLOUD_SECRET_ID=<Tencent Cloud SecretID>
QCLOUD_SECRET_KEY=<Tencent Cloud SecretKey>
QCLOUD_COS_URL=https://cos.example.com
QCLOUD_COS_BUCKET=example-cos-12345678
GEMINI_API_KEY=<Gemini API Key>
OPENAI_MAX_ALLOWED_TOKENS=<Max Token Count for Conversations>
QIANFAN_ACCESS_KEY=<Baidu Qianfan Access Key>
QIANFAN_SECRET_KEY=<Baidu Qianfan Secret Key>
DASHSCOPE_API_KEY=<Alibaba Dashscope API Key>
DJANGO_SETTINGS_MODULE=entry.settings
KIMI_API_KEY=<Moonshot API Key>
KIMI_API_BASE_URL=https://api.moonshot.cn/v1
WXPAY_ENABLED=0
WXPAY_PRIVATE_KEY_SERIAL_NO=<WeChat Pay Certificate Serial Number>
WXPAY_PRIVATE_KEY_PATH=<WeChat Pay Private Key Path>
Expand Down
7 changes: 0 additions & 7 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,12 @@ REDIS_PASSWORD=<Redis 密码>
REDIS_DB=<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=<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=<Moonshot API Key>
KIMI_API_BASE_URL=https://api.moonshot.cn/v1
WXPAY_ENABLED=0"
WXPAY_PRIVATE_KEY_SERIAL_NO=<微信支付证书序列号>
WXPAY_PRIVATE_KEY_PATH=<微信支付密钥路径>
Expand Down
17 changes: 8 additions & 9 deletions apps/chat/client/midjourney.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions apps/cos/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 0 additions & 9 deletions entry/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
7 changes: 0 additions & 7 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,11 @@ REDIS_DB=0
SESSION_COOKIE_DOMAIN=.example.com
OVINC_WEB_URL=<Contact us to obtain>
OVINC_API_DOMAIN=<Contact us to obtain>
OPENAI_HTTP_PROXY_URL=http://proxy.example.com:3814
QCLOUD_SECRET_ID=<Tencent Cloud SecretID>
QCLOUD_SECRET_KEY=<Tencent Cloud SecretKey>
QCLOUD_COS_URL=https://cos.example.com
QCLOUD_COS_BUCKET=example-cos-12345678
GEMINI_API_KEY=<Gemini API Key>
QIANFAN_ACCESS_KEY=<Baidu Qianfan Access Key>
QIANFAN_SECRET_KEY=<Baidu Qianfan Secret Key>
DASHSCOPE_API_KEY=<Alibaba Dashscope API Key>
DJANGO_SETTINGS_MODULE=entry.settings
KIMI_API_KEY=<Moonshot API Key>
KIMI_API_BASE_URL=https://api.moonshot.cn/v1
WXPAY_ENABLED=0
WXPAY_PRIVATE_KEY_SERIAL_NO=<WeChat Pay Certificate Serial Number>
WXPAY_PRIVATE_KEY_PATH=<WeChat Pay Private Key Path>
Expand Down

0 comments on commit 40b1060

Please sign in to comment.