优化 prompt
This commit is contained in:
@@ -101,59 +101,143 @@ async def generate_furniture(runtime: ToolRuntime, prompts: List[str] = None, nu
|
||||
|
||||
|
||||
@tool
|
||||
async def edit_furniture(runtime: ToolRuntime, config: RunnableConfig, input_image_paths: list[str] = None, prompts: list[str] = None, ):
|
||||
async def edit_furniture(
|
||||
runtime: ToolRuntime,
|
||||
config: RunnableConfig,
|
||||
input_image_paths: list[str] = None,
|
||||
prompts: list[str] = None,
|
||||
mode: str = "auto",
|
||||
):
|
||||
"""
|
||||
使用先进的图像编辑模型对家具设计草图进行精准修改。
|
||||
|
||||
功能说明:
|
||||
- 支持批量处理多张家具图片,根据对应的提示词生成修改后的新图片。
|
||||
- input_image_paths 和 prompts 必须一一对应,数量完全一致。
|
||||
- 最多支持同时处理 4 对图片和提示词(即最多 4 张图片)。
|
||||
支持三种灵活模式(与 edit_quote_upload_furniture 保持一致):
|
||||
- one_to_one(默认,最常用):多张图片 + 多个提示词,一一对应编辑
|
||||
- one_to_many:1 张图片 + 多个提示词(同一张图片生成多个不同变体,例如不同风格/颜色)
|
||||
- many_to_one:多张图片 + 1 个提示词(所有图片应用相同的修改)
|
||||
|
||||
参数说明:
|
||||
- input_image_paths (list[str]): 输入图片的 MinIO 路径列表,长度建议 1~4
|
||||
- prompts (list[str]): 修改提示词列表(必须是英文提示词)
|
||||
- mode (str): "one_to_one", "one_to_many", "many_to_one", "auto"(默认自动判断)
|
||||
|
||||
- input_image_paths (list[str]):
|
||||
输入图片在 MinIO 中的存储路径列表。
|
||||
示例:["furniture/designs/sofa_concept_v1.png", "projects/room_2026/chair_v2.jpg"]
|
||||
注意:路径必须是有效的 MinIO 对象路径,工具会自动下载对应图片。
|
||||
|
||||
- prompts (list[str]):
|
||||
必须是列表,即使只有一个提示词也要用 ["你的提示词"] 格式。
|
||||
与图片一一对应的详细英文提示词列表。
|
||||
每个提示词描述对对应图片的具体修改要求(风格、颜色、材质、形状、添加/删除元素等)。
|
||||
示例:["Change the sofa to a modern minimalist style with dark gray fabric and metal legs, add a matching coffee table.",
|
||||
"Convert the chair to Scandinavian Nordic style with light wood and soft beige upholstery."]
|
||||
|
||||
使用要求(重要):
|
||||
- input_image_paths 和 prompts 的长度必须完全相同。
|
||||
- 列表长度必须在 1 到 4 之间(最多 4 对)。
|
||||
- input_image_paths[0] 对应 prompts[0],以此类推,一一对应进行编辑。
|
||||
|
||||
使用场景:
|
||||
- 家具设计方案迭代
|
||||
- 室内设计多方案对比修改
|
||||
- 批量风格转换(现代/北欧/工业/奢华风等)
|
||||
- 材质、颜色、细节批量调整
|
||||
使用要求(必须严格遵守):
|
||||
- input_image_paths 和 prompts 不能为空,长度必须在 1~4 之间。
|
||||
- mode="auto" 时会根据列表长度智能判断模式:
|
||||
- 1 张图片 + 多个 prompt → one_to_many
|
||||
- 多个图片 + 1 个 prompt → many_to_one
|
||||
- 图片数量 == prompt 数量 → one_to_one
|
||||
- 编辑对象默认使用最近生成的图片(由 Supervisor 传入最新路径)。
|
||||
|
||||
示例调用:
|
||||
input_image_paths = ["designs/sofa1.png", "designs/chair1.png"]
|
||||
|
||||
1. one_to_one(一一对应,最常用)
|
||||
input_image_paths = ["furniture/sketches/sofa_v1.png", "furniture/sketches/chair_v1.png"]
|
||||
prompts = [
|
||||
"Make the sofa more luxurious with velvet fabric and gold accents.",
|
||||
"Change the chair to a sleek modern design with black leather and chrome legs."
|
||||
"Change the sofa to modern minimalist style with dark gray fabric.",
|
||||
"Make the chair more Scandinavian with light wood and beige upholstery."
|
||||
]
|
||||
mode = "one_to_one"
|
||||
|
||||
2. one_to_many(同一张图片多个版本)
|
||||
input_image_paths = ["furniture/sketches/sofa_latest.png"]
|
||||
prompts = [
|
||||
"Change to luxurious velvet with gold accents.",
|
||||
"Change to industrial style with metal frame.",
|
||||
"Change to soft pastel Nordic style."
|
||||
]
|
||||
mode = "one_to_many"
|
||||
|
||||
3. many_to_one(多张图片统一修改)
|
||||
input_image_paths = ["furniture/sketches/sofa1.png", "furniture/sketches/chair1.png", "furniture/sketches/table1.png"]
|
||||
prompts = ["Make all furniture more luxurious with velvet fabric and gold accents."]
|
||||
mode = "many_to_one"
|
||||
"""
|
||||
try:
|
||||
# ====================== 参数校验 ======================
|
||||
if not input_image_paths or len(input_image_paths) < 1 or len(input_image_paths) > 4:
|
||||
return f"参数错误:input_image_paths 必须提供,且长度需在 1 到 4 张之间。目前收到 {len(input_image_paths) if input_image_paths else 0} 张。"
|
||||
|
||||
if not prompts:
|
||||
return "参数错误:prompts 不能为空,请至少提供一个修改提示词。"
|
||||
|
||||
if mode not in ["one_to_one", "one_to_many", "many_to_one", "auto"]:
|
||||
return f"参数错误:mode 参数无效。可用值:one_to_one, one_to_many, many_to_one, auto。当前收到:{mode}"
|
||||
|
||||
# Auto 模式智能判断
|
||||
if mode == "auto":
|
||||
if len(input_image_paths) == 1 and len(prompts) > 1:
|
||||
mode = "one_to_many"
|
||||
elif len(prompts) == 1:
|
||||
mode = "many_to_one"
|
||||
elif len(input_image_paths) == len(prompts):
|
||||
mode = "one_to_one"
|
||||
else:
|
||||
mode = "one_to_one" # 兜底
|
||||
|
||||
# 各模式严格校验
|
||||
if mode == "one_to_many":
|
||||
if len(input_image_paths) != 1:
|
||||
return f"参数错误:one_to_many 模式只能传入 1 张图片,当前传入了 {len(input_image_paths)} 张。"
|
||||
if len(prompts) < 1:
|
||||
return "参数错误:one_to_many 模式下 prompts 至少需要 1 个。"
|
||||
|
||||
elif mode == "many_to_one":
|
||||
if len(prompts) != 1:
|
||||
return f"参数错误:many_to_one 模式下 prompts 必须只有 1 个,当前有 {len(prompts)} 个。"
|
||||
|
||||
elif mode == "one_to_one":
|
||||
if len(prompts) != len(input_image_paths):
|
||||
return (f"参数错误:one_to_one 模式下 input_image_paths 和 prompts 数量必须完全一致。\n"
|
||||
f"当前图片 {len(input_image_paths)} 张,prompts {len(prompts)} 个。")
|
||||
|
||||
# ====================== 执行编辑 ======================
|
||||
result = []
|
||||
if len(input_image_paths):
|
||||
for i in range(len(input_image_paths)):
|
||||
bucket_name = "fida-public-bucket"
|
||||
object_name = f"furniture/sketches/{uuid.uuid4()}"
|
||||
image_url = await generate_or_edit_image(input_path=[input_image_paths[i]], prompt=prompts[i], bucket_name=bucket_name, object_name=f"{object_name}-{i}.png")
|
||||
bucket_name = "fida-public-bucket"
|
||||
|
||||
if mode == "one_to_many":
|
||||
# 同一张图片 + 多个 prompt
|
||||
base_image = input_image_paths[0]
|
||||
for i, prompt in enumerate(prompts):
|
||||
object_name = f"furniture/sketches/{uuid.uuid4()}.png"
|
||||
image_url = await generate_or_edit_image(
|
||||
input_path=[base_image],
|
||||
prompt=prompt,
|
||||
bucket_name=bucket_name,
|
||||
object_name=f"{object_name}-var{i}.png"
|
||||
)
|
||||
result.append(image_url)
|
||||
|
||||
elif mode == "many_to_one":
|
||||
# 多张图片 + 1 个 prompt
|
||||
current_prompt = prompts[0]
|
||||
for i, image_path in enumerate(input_image_paths):
|
||||
object_name = f"furniture/sketches/{uuid.uuid4()}.png"
|
||||
image_url = await generate_or_edit_image(
|
||||
input_path=[image_path],
|
||||
prompt=current_prompt,
|
||||
bucket_name=bucket_name,
|
||||
object_name=f"{object_name}-{i}.png"
|
||||
)
|
||||
result.append(image_url)
|
||||
|
||||
else:
|
||||
# one_to_one:一一对应
|
||||
for i in range(len(input_image_paths)):
|
||||
object_name = f"furniture/sketches/{uuid.uuid4()}.png"
|
||||
image_url = await generate_or_edit_image(
|
||||
input_path=[input_image_paths[i]],
|
||||
prompt=prompts[i],
|
||||
bucket_name=bucket_name,
|
||||
object_name=f"{object_name}-{i}.png"
|
||||
)
|
||||
result.append(image_url)
|
||||
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"edit_furniture error :{e}")
|
||||
return "edit_furniture error"
|
||||
logger.error(f"edit_furniture 执行异常: {e}", exc_info=True)
|
||||
return f"工具执行失败:{str(e)},请检查参数后重试。"
|
||||
|
||||
|
||||
@tool
|
||||
|
||||
Reference in New Issue
Block a user