caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

95 lines
3.5 KiB
Python

import os
from typing import List, Optional
from .config import Config
try:
from .vlm_dashscope import QwenVLClient
except ImportError:
from vlm_dashscope import QwenVLClient
class VLM:
def __init__(self,
dashscope_api_key: Optional[str] = None,
dashscope_base_url: Optional[str] = None):
"""
Unified VLM (Vision Language Model) Client
Routes asset-analysis VLM requests to DashScope (Qwen/Qwen-Omni).
"""
dashscope_key = dashscope_api_key or Config.DASHSCOPE_API_KEY
self.dashscope_client = (
QwenVLClient(
api_key=dashscope_key,
base_url=dashscope_base_url or Config.DASHSCOPE_BASE_URL
)
if dashscope_key else None
)
def query(self,
prompt: str,
image_paths: Optional[List[str]] = None,
model: Optional[str] = None,
session_id: Optional[str] = None,
video_paths: Optional[List[str]] = None) -> str:
selected_model = (model or "").strip()
if not selected_model:
raise RuntimeError("DashScope VLM model must be explicitly selected.")
if Config.PRINT_MODEL_INPUT:
print("---- VLM REQUEST ----")
print(f"Prompt: {prompt}")
if image_paths:
print(f"Images: {len(image_paths)}")
for p in image_paths:
if p.startswith("data:"):
print(f" - [Base64图片]")
else:
print(f" - {p}")
if video_paths:
print(f"Videos: {len(video_paths)}")
for p in video_paths:
print(f" - {p}")
print(f"Model: {selected_model}")
if session_id:
print(f"Session ID: {session_id}")
print("-" * 30)
if self.dashscope_client is None:
raise RuntimeError("DashScope VLM API key is not configured.")
image_urls = [self._to_dashscope_file_url(path, allow_data_url=True) for path in image_paths or []]
video_urls = [self._to_dashscope_file_url(path, allow_data_url=False) for path in video_paths or []]
return self.dashscope_client.chat(
text=prompt,
images=image_urls,
videos=video_urls,
model=selected_model,
stream=False,
)
def _to_dashscope_file_url(self, path: str, allow_data_url: bool) -> str:
if path.startswith("data:"):
if not allow_data_url:
raise ValueError("DashScope video input does not support data URLs in this adapter.")
import base64 as b64
import tempfile
try:
header, b64_data = path.split(",", 1)
mime_type = header.split(";")[0].replace("data:", "")
image_data = b64.b64decode(b64_data)
suffix = f".{mime_type.split('/')[-1]}" if "/" in mime_type else ".png"
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
tmp.write(image_data)
temp_path = tmp.name
return f"file://{os.path.abspath(temp_path)}"
except Exception as e:
print(f"Error processing base64 image: {e}")
raise ValueError(f"无法解析 base64 图片: {e}")
if path.startswith("http") or path.startswith("file://"):
return path
return f"file://{os.path.abspath(path)}"