Files
chongming-plugins/doubao/router/analyze.py
T
2026-07-31 16:53:38 +08:00

60 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
import logging
from chongming_worker import rpc_method
from ..models.pydantic import AnalyzePostRequest, AnalyzePostResponse
from ..global_var import get_client, get_is_aicc
logger = logging.getLogger("plugins.doubao.analyze")
@rpc_method(
subject="doubao.analyze",
method="POST",
summary="Analyze image via Doubao vision model",
response_model=AnalyzePostResponse,
)
async def analyze(request: AnalyzePostRequest) -> AnalyzePostResponse:
logger.info("收到分析请求: prompt=%s, image_len=%d", request.prompt, len(request.image_base64))
client = get_client()
if client is None:
logger.error("豆包客户端未初始化(is_aicc=%s)", get_is_aicc())
return AnalyzePostResponse(result="", error="客户端未初始化,请检查插件启动日志确认 API_KEY 和 AICC 配置是否正确")
# 获取图片数据
image_base64 = request.image_base64
if not image_base64:
return AnalyzePostResponse(result="", error="未提供图片数据(image_base64或image_path)")
if image_base64.startswith('data:image/'):
image_base64 = image_base64.split(',', 1)[1]
mime_type = "image/png"
prompt = request.prompt or ""
try:
# AICC 模式的 AsyncOpenAI 不支持 thinking 参数,需通过 extra_body 传递
if get_is_aicc():
create_kwargs = {"extra_body": {"thinking": {"type": "disabled"}}}
else:
create_kwargs = {"thinking": {"type": "disabled"}}
response = await client.chat.completions.create(
model=os.getenv("DOUBAO_MODEL", "doubao-seed-evolving"),
messages=[
{
"role": "user",
"content": [
{"type": "image_url", "image_url": f"data:{mime_type};base64,{image_base64}"},
{"type": "text", "text": prompt}
]
} # type: ignore
],
**create_kwargs # type: ignore
)
result = response.choices[0].message.content # type: ignore
logger.info("分析完成: 结果长度=%d", len(result) if result else 0)
return AnalyzePostResponse(result=result) # type: ignore
except Exception as e:
logger.error("调用豆包API失败: %s", e, exc_info=True)
return AnalyzePostResponse(result="", error=f"调用豆包API失败: {str(e)}")