add first commit
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import os
|
||||
from chongming_worker import rpc_method
|
||||
from ..models.pydantic import AnalyzePostRequest, AnalyzePostResponse
|
||||
from ..global_var import get_ark_client
|
||||
|
||||
@rpc_method(
|
||||
subject="doubao.analyze",
|
||||
method="POST",
|
||||
summary="Analyze image via Doubao vision model",
|
||||
response_model=AnalyzePostResponse,
|
||||
)
|
||||
async def analyze(request: AnalyzePostRequest) -> AnalyzePostResponse:
|
||||
print(f"[DEBUG] analyze called with prompt={request.prompt}, image_base64 length={len(request.image_base64)}")
|
||||
client = get_ark_client()
|
||||
if client is None:
|
||||
return AnalyzePostResponse(result="", error="客户端未初始化")
|
||||
|
||||
# 获取图片数据
|
||||
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:
|
||||
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
|
||||
],
|
||||
thinking={"type": "disabled"}
|
||||
)
|
||||
result = response.choices[0].message.content # type: ignore
|
||||
return AnalyzePostResponse(result=result) # type: ignore
|
||||
except Exception as e:
|
||||
return AnalyzePostResponse(result="", error=f"调用豆包API失败: {str(e)}")
|
||||
Reference in New Issue
Block a user