Merge branch 'main' of https://gitee.com/wg123/video-gen
This commit is contained in:
@@ -2,7 +2,6 @@ import base64
|
||||
import json
|
||||
import mimetypes
|
||||
import os
|
||||
import random
|
||||
from datetime import datetime
|
||||
|
||||
import httpx
|
||||
@@ -112,44 +111,27 @@ async def optimize_prompt(
|
||||
await db.commit()
|
||||
|
||||
if configs:
|
||||
total_weight = sum(c.weight for c in configs)
|
||||
r = random.uniform(0, total_weight)
|
||||
cumulative = 0
|
||||
selected = configs[0]
|
||||
for c in configs:
|
||||
cumulative += c.weight
|
||||
if r <= cumulative:
|
||||
selected = c
|
||||
break
|
||||
# 按 priority 从大到小依次尝试,跳过 mock,失败则用下一个
|
||||
for selected in configs:
|
||||
if selected.provider == "mock":
|
||||
continue
|
||||
if selected.provider in ("openai_compatible", "sdk"):
|
||||
try:
|
||||
return await _call_openai_compatible(
|
||||
selected, original_prompt, db, user_id, industry_key, duration,
|
||||
references=references,
|
||||
gen_type=gen_type,
|
||||
image_size=image_size,
|
||||
image_proportion=image_proportion,
|
||||
image_px=image_px,
|
||||
)
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
if selected.provider == "mock":
|
||||
# 所有真实模型都失败,降级到 mock
|
||||
mock_cfg = next((c for c in configs if c.provider == "mock"), None)
|
||||
if mock_cfg:
|
||||
return _mock_optimize(original_prompt, gen_type)
|
||||
elif selected.provider in ("openai_compatible", "sdk"):
|
||||
try:
|
||||
return await _call_openai_compatible(
|
||||
selected, original_prompt, db, user_id, industry_key, duration,
|
||||
references=references,
|
||||
gen_type=gen_type,
|
||||
image_size=image_size,
|
||||
image_proportion=image_proportion,
|
||||
image_px=image_px,
|
||||
)
|
||||
except Exception:
|
||||
for c in configs:
|
||||
if c.id == selected.id or c.provider == "mock":
|
||||
continue
|
||||
try:
|
||||
return await _call_openai_compatible(
|
||||
c, original_prompt, db, user_id, industry_key, duration,
|
||||
references=references,
|
||||
gen_type=gen_type,
|
||||
image_size=image_size,
|
||||
image_proportion=image_proportion,
|
||||
image_px=image_px,
|
||||
)
|
||||
except Exception:
|
||||
continue
|
||||
raise
|
||||
|
||||
if settings.LLM_MOCK:
|
||||
return _mock_optimize(original_prompt, gen_type)
|
||||
|
||||
Reference in New Issue
Block a user