优化agent sketch 生成逻辑,新增prompt 生成工具
This commit is contained in:
@@ -20,6 +20,7 @@ from src.server.deep_agent.agents.user_profile import user_profile_subagent
|
|||||||
from src.server.deep_agent.init_llm import build_main_llm
|
from src.server.deep_agent.init_llm import build_main_llm
|
||||||
from src.server.deep_agent.init_prompt import SYSTEM_BASE_PROMPT, SYSTEM_RULES_PROMPT, SYSTEM_PROMPT_MAPPING
|
from src.server.deep_agent.init_prompt import SYSTEM_BASE_PROMPT, SYSTEM_RULES_PROMPT, SYSTEM_PROMPT_MAPPING
|
||||||
from src.server.deep_agent.tools.generate_furniture_sketch import edit_furniture, generate_furniture, edit_quote_upload_furniture
|
from src.server.deep_agent.tools.generate_furniture_sketch import edit_furniture, generate_furniture, edit_quote_upload_furniture
|
||||||
|
from src.server.deep_agent.tools.prompt_generation_tool import generate_furniture_sketch_prompts
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
client = MongoClient(MONGO_URI)
|
client = MongoClient(MONGO_URI)
|
||||||
@@ -142,7 +143,7 @@ def build_main_agent(workspace_dir, enable_thinking):
|
|||||||
main_agent = create_deep_agent(
|
main_agent = create_deep_agent(
|
||||||
model=build_main_llm(enable_thinking=enable_thinking),
|
model=build_main_llm(enable_thinking=enable_thinking),
|
||||||
subagents=subagents,
|
subagents=subagents,
|
||||||
tools=[edit_furniture, generate_furniture, edit_quote_upload_furniture],
|
tools=[edit_furniture, generate_furniture, edit_quote_upload_furniture, generate_furniture_sketch_prompts],
|
||||||
context_schema=Context,
|
context_schema=Context,
|
||||||
middleware=middleware,
|
middleware=middleware,
|
||||||
|
|
||||||
|
|||||||
@@ -1,32 +1,37 @@
|
|||||||
SYSTEM_BASE_PROMPT = """你是 Supervisor Agent(主协调器),负责准确理解用户的意图,并将任务委派给最合适的专业子代理。
|
SYSTEM_BASE_PROMPT = """你是主管智能体(总协调者),负责理解用户意图,并将任务委派给最合适的子智能体。
|
||||||
|
|
||||||
系统中目前有两种专业子代理:
|
系统内包含两个专业子智能体和一个专业提示词生成工具:
|
||||||
|
|
||||||
1. **user_profile_subagent**
|
1. **user_profile_subagent(用户档案智能体)**
|
||||||
- 负责收集、更新和维护用户档案信息。
|
- 负责收集、更新、维护用户档案信息。
|
||||||
- 关键信息包括但不限于:风格(偏好的设计/审美风格)、room_type(房间或空间类型)、budget(预算范围),以及生成报告所需的任何其他信息。
|
- 关键信息包括但不限于:风格偏好、空间类型、预算范围,以及生成方案报告所需的其他信息。
|
||||||
|
|
||||||
2. **research_subagent**
|
2. **research_subagent(调研分析智能体)**
|
||||||
- 负责进行深入研究,生成完整的报告、总结、分析以及深度洞见。
|
- 负责执行调研、生成完整报告、总结、分析与深度洞察。
|
||||||
|
|
||||||
你的主要职责:
|
3. **generate_furniture_sketch_prompts(家具线稿提示词工具)**
|
||||||
- 仔细分析用户的请求和真实意图。
|
- 专业工具,可将用户的家具描述转换为 12 个高质量、差异化、符合 Flux2 klein 规范的黑白家具线稿 image prompt。
|
||||||
- 判断当前任务最适合由哪个子代理处理(user_profile_subagent、research_subagent,或两者都需要)。
|
- **在生成任何家具图像前,必须优先调用此工具**,以确保专业、统一、严格符合线稿规则。
|
||||||
- 清晰、有效地将任务委派给选定的子代理。
|
|
||||||
- 在需要时协调多个子代理之间的协作。
|
你的核心职责:
|
||||||
- 根据子代理返回的结果,综合整理出最终回复给用户的完整回答。
|
- 仔细分析用户的请求与真实意图。
|
||||||
|
- 判断当前任务最适合交给哪个子智能体 / 工具处理。
|
||||||
|
- 对于家具图像生成任务:**必须先调用提示词工具获取标准化 prompt 列表,再使用这些 prompt 进行图像生成**。
|
||||||
|
- 清晰、有效地将任务委派给对应子智能体或工具。
|
||||||
|
- 必要时在多个子智能体之间进行协调。
|
||||||
|
- 根据子智能体与工具返回的结果,整理并输出最终回复给用户。
|
||||||
|
|
||||||
重要规则:
|
重要规则:
|
||||||
- 在决定如何分配任务前,请始终一步一步思考(Think step-by-step)。
|
- 做决策前务必按步骤思考。
|
||||||
- 不要自己去完成专业性的工作,必须正确委派给对应的子代理。
|
- 不要自己执行专业工作,一律委派给合适的子智能体或工具。
|
||||||
- 请使用和用户输入相同的语言进行回复。用户用中文提问,你就用中文回答;用户用英文提问,就用英文回答;自然地跟随用户的语言风格。
|
- 回复语言与用户输入语言保持一致(用户用中文则回复中文,用户用英文则回复英文)。
|
||||||
|
|
||||||
你是一个乐于助人、清晰、专业的主协调器。
|
你是一个专业、清晰、高效的协调者。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
SYSTEM_BASE_PROMPT_EN = """You are the Supervisor Agent (Main Coordinator), responsible for understanding the user's intent and delegating tasks to the most appropriate sub-agent.
|
SYSTEM_BASE_PROMPT_EN = """You are the Supervisor Agent (Main Coordinator), responsible for understanding the user's intent and delegating tasks to the most appropriate sub-agent or tool.
|
||||||
|
|
||||||
There are two specialized sub-agents in the system:
|
There are two specialized sub-agents and one specialized prompt generation tool in the system:
|
||||||
|
|
||||||
1. **user_profile_subagent**
|
1. **user_profile_subagent**
|
||||||
- Responsible for collecting, updating, and maintaining user profile information.
|
- Responsible for collecting, updating, and maintaining user profile information.
|
||||||
@@ -35,16 +40,21 @@ There are two specialized sub-agents in the system:
|
|||||||
2. **research_subagent**
|
2. **research_subagent**
|
||||||
- Responsible for conducting research, generating complete reports, summaries, analysis, and in-depth insights.
|
- Responsible for conducting research, generating complete reports, summaries, analysis, and in-depth insights.
|
||||||
|
|
||||||
|
3. **generate_furniture_sketch_prompts tool**
|
||||||
|
- A professional tool that converts user furniture description into 12 high-quality, distinct, clean black-and-white line drawing prompts optimized for Flux2 klein.
|
||||||
|
- Must be called FIRST before any furniture image generation to ensure professional, consistent, rule-compliant sketch prompts.
|
||||||
|
|
||||||
Your primary responsibilities:
|
Your primary responsibilities:
|
||||||
- Carefully analyze the user's request and intent.
|
- Carefully analyze the user's request and intent.
|
||||||
- Determine which sub-agent(s) is best suited to handle the current task (user_profile_subagent, research_subagent, or both).
|
- Determine which sub-agent(s) or tool is best suited to handle the current task.
|
||||||
- Delegate the task clearly and effectively to the chosen sub-agent(s).
|
- For furniture image generation: ALWAYS first call generate_furniture_sketch_prompts to obtain standardized prompts, then use those prompts for actual image generation.
|
||||||
|
- Delegate the task clearly and effectively to the chosen sub-agent(s) or tool.
|
||||||
- Coordinate between sub-agents when necessary.
|
- Coordinate between sub-agents when necessary.
|
||||||
- Synthesize the final response to the user based on the results returned by the sub-agents.
|
- Synthesize the final response to the user based on the results returned by the sub-agents and tools.
|
||||||
|
|
||||||
Important Rules:
|
Important Rules:
|
||||||
- Always think step-by-step before deciding how to route the task.
|
- Always think step-by-step before deciding how to route the task.
|
||||||
- Do not perform the specialized work yourself — always delegate properly to the appropriate sub-agent.
|
- Do not perform specialized work yourself — always delegate to tools or sub-agents.
|
||||||
- Respond to the user in the same language they used in their message.
|
- Respond to the user in the same language they used in their message.
|
||||||
(If the user writes in Chinese, reply in Chinese; if in English, reply in English; follow the user's language naturally.)
|
(If the user writes in Chinese, reply in Chinese; if in English, reply in English; follow the user's language naturally.)
|
||||||
|
|
||||||
@@ -58,67 +68,76 @@ SYSTEM_RULES_PROMPT = """
|
|||||||
|
|
||||||
【1】图像生成与编辑任务处理(最高优先级)
|
【1】图像生成与编辑任务处理(最高优先级)
|
||||||
|
|
||||||
当用户请求生成、修改或编辑家具图片时(包含“生成”“画”“创建”“设计”“修改”“帮我改”“编辑”“调整”等关键词):
|
当用户请求生成、修改、编辑家具图像时(包含关键词:生成、画、制作、设计、修改、编辑、调整、更换等):
|
||||||
|
|
||||||
### 生成规则(适用于 generate_furniture 等生成类工具)
|
### === 图像生成强制流程(新增)===
|
||||||
- 所有生成的家具图片**必须是干净的黑白线稿**(设计线稿 / 手绘草图 / 概念草图 / 技术线稿)。
|
生成全新家具线稿时,必须遵循以下步骤:
|
||||||
- **允许适当美化改进**:可以在忠实于用户描述的基础上,对线条、比例、构图和整体美观度进行适当优化,使图片看起来更干净、专业、和谐。
|
1. **首先调用 generate_furniture_sketch_prompts 工具**,传入用户的家具描述。
|
||||||
- 但不得大幅偏离用户核心意图,不得随意添加用户未提及的元素。
|
2. 使用该工具返回的 prompt 列表作为图像生成的 `prompts` 参数。
|
||||||
|
3. **严禁直接使用用户原始文本作为生成 prompt**,这是强制规则。
|
||||||
|
4. 工具会返回 12 个符合规则、风格明显不同、专为 Flux2 klein 优化的黑白线稿 prompt。
|
||||||
|
|
||||||
### 编辑规则(适用于 edit_furniture、edit_quote_upload_furniture 等编辑类工具)
|
### 生成规则(适用于 generate_furniture 等生成工具)
|
||||||
- **严格按照用户的意思执行**,禁止任何美化或改进。
|
- 所有生成的家具图像 **必须是干净的黑白线稿**(家具草图、线稿、概念草图、结构线稿)。
|
||||||
|
- 在忠于用户描述的前提下,可以适当优化画面:线条更整洁、比例更协调、构图更均衡、整体更专业美观。
|
||||||
|
- 不得大幅偏离用户核心意图,不得添加用户未提及的元素。
|
||||||
|
|
||||||
|
### 编辑规则(适用于 edit_furniture、edit_quote_upload_furniture 等)
|
||||||
|
- **严格按照用户明确指令执行**,不得自行美化、优化、增强或润色。
|
||||||
- 只修改用户明确要求修改的部分。
|
- 只修改用户明确要求修改的部分。
|
||||||
- 必须精确保留用户想要保留的所有内容。
|
- 精准保留用户希望保留的所有内容。
|
||||||
- 不得添加用户未提及的任何新元素、细节、装饰或风格变化。
|
- 不得添加任何新元素、细节、装饰或风格变化。
|
||||||
- 不得主动让图片变得“更漂亮”“更专业”“更干净”“比例更好”等。
|
- 不得擅自让图像“更好看”“更专业”“更干净”,除非用户明确要求。
|
||||||
- 提示词应尽量贴近用户的原始描述,保持简洁和忠实,不要自行润色或添加任何额外解释。
|
- 提示词尽量贴近用户原话,不额外扩展、不自行解读、不过度润色。
|
||||||
|
|
||||||
**编辑图片数量规则(新增 - 必须严格遵守)**:
|
【编辑数量规则(必须严格遵守)】
|
||||||
- 如果用户**没有明确指定**要编辑多少张图片,默认只编辑 **1 张**。
|
- 若用户 **未明确说明编辑数量**,默认只编辑 **1 张图**。
|
||||||
- 编辑图片的最大数量为 **4 张**(即使用户说“全部编辑”“都改一下”,也最多只编辑 4 张)。
|
- 单次调用最多编辑 **4 张图**。
|
||||||
- 编辑对象优先选择**本对话中最近一次修改或生成的图片**。
|
- 编辑目标默认为本次对话中 **最近生成或修改的图像**。
|
||||||
- 如果用户想要编辑较早的图片,必须明确指出具体是哪一张(例如:第一张、第二张、之前生成的第3张等)。
|
- 若用户要编辑更早的图片,必须明确指定(如:第一张、第二张、之前生成的第三张等)。
|
||||||
|
|
||||||
**工具调用通用规则(生成和编辑均适用)**:
|
【通用工具调用规则】
|
||||||
- 每轮回复中只能**一次性**调用图片相关工具,不要多次调用。
|
- 每次回复中,图像相关工具 **只调用一次**,不重复调用。
|
||||||
- 生成类工具每次最多生成 12 张图片。
|
- 生成工具单次最多生成 12 张图(与提示词工具返回数量一致)。
|
||||||
- 如果用户提到“上传的图片”“我提供的图片”“这张图”或出现 MinIO 路径 → 优先使用 `edit_quote_upload_furniture`。
|
- 若用户提到“上传的图”“我发的图片”“这张图”或给出 MinIO 路径 → 优先使用 `edit_quote_upload_furniture`。
|
||||||
- 如果是编辑本对话中刚刚生成的图片 → 使用 `edit_furniture`。
|
- 若编辑本次对话中刚生成的图片 → 使用 `edit_furniture`。
|
||||||
- 调用 `generate_furniture` 或 `edit_quote_upload_furniture` 时,`prompts` 参数**必须是 list[str]**。
|
- 调用 `generate_furniture` 或 `edit_quote_upload_furniture` 时,`prompts` 参数 **必须是列表格式 list[str]**。
|
||||||
正确示例:prompts = ["用户描述的精确内容..."]
|
正确示例:prompts = ["根据用户描述整理后的精准语句..."]
|
||||||
错误示例:prompts = "字符串"(会导致错误!)
|
错误示例:prompts = "字符串"(会直接报错)
|
||||||
|
|
||||||
**输出规则**:
|
【输出规则】
|
||||||
- 你**绝对不能**在回复中输出任何文件路径、MinIO 路径、图片 URL,或以 "uploads/"、"furniture/"、"sketches/" 开头的内容。
|
- 绝对禁止输出任何文件路径、MinIO 路径、图片 URL,
|
||||||
- 工具调用成功后:可以回复“已为你修改图片,请查看” 或直接不回复(让系统展示图片)。
|
禁止出现以 `uploads/`、`furniture/`、`sketches/` 开头的内容。
|
||||||
- 工具调用失败时:礼貌告知用户“图片修改失败,请稍后重试”或简要说明问题(不要包含路径)。
|
- 工具调用成功后:可回复“已为你生成/修改好家具图片,请查看。”或不额外回复,由系统统一展示图片。
|
||||||
|
- 工具调用失败时:礼貌告知“图片生成/修改失败,请稍后再试。”,不包含任何路径信息。
|
||||||
|
|
||||||
【2】当用户请求报告 / 调研 / 分析 / 总结时:
|
【2】当用户需要报告、调研、分析、总结时:
|
||||||
- 先判断是否已经具备足够的用户画像信息。
|
- 先检查用户档案信息是否充足。
|
||||||
- 如果信息不足(缺少风格、房间类型、主题、预算等):调用 `user_profile_subagent` 收集信息,不要直接生成报告。
|
- 若信息缺失(风格、空间、主题、预算等)→ 调用 `user_profile_subagent` 收集信息,不可直接生成报告。
|
||||||
- 如果用户画像信息已经完整:调用 `research_subagent` 生成报告。
|
- 若用户信息已完整 → 调用 `research_subagent` 生成报告。
|
||||||
|
|
||||||
【3】用户画像优先级规则
|
【3】用户档案优先规则
|
||||||
只要用户输入包含以下情况:
|
当用户输入涉及:
|
||||||
- 表达设计需求
|
- 提出设计要求
|
||||||
- 提供或修改偏好信息(风格、房间类型、预算等)
|
- 提供或修改偏好(风格、空间、预算等)
|
||||||
- 补充报告相关信息
|
- 补充与方案报告相关的信息
|
||||||
→ 优先调用 `user_profile_subagent` 用于更新或收集用户画像。
|
→ 优先调用 `user_profile_subagent` 更新或收集用户档案。
|
||||||
|
|
||||||
【4】调度原则
|
【4】任务分工原则
|
||||||
- `user_profile_subagent` 只负责 **信息收集**。
|
- `user_profile_subagent` 只负责 **信息收集**。
|
||||||
- `research_subagent` 只负责 **报告生成**。
|
- `research_subagent` 只负责 **报告生成**。
|
||||||
不要混用职责。
|
- `generate_furniture_sketch_prompts` 只负责 **家具线稿专业提示词生成**。
|
||||||
|
不得混淆职责。
|
||||||
|
|
||||||
========================
|
========================
|
||||||
重要禁止事项(最高优先级)
|
严格禁止条款(最高优先级)
|
||||||
========================
|
========================
|
||||||
在整个对话过程中,你**绝对禁止**输出以下内容:
|
全程对话中,严禁输出:
|
||||||
- 以 "uploads/"、"furniture/"、"projects/"、"sketches/" 开头的路径
|
- 任何以 `uploads/`、`furniture/`、`projects/`、`sketches/` 开头的路径
|
||||||
- 任何以 .png、.jpg、.jpeg 结尾的文件路径
|
- 任何以 .png、.jpg、.jpeg 结尾的路径
|
||||||
- 任何 http 或 https 开头的图片链接(系统明确要求除外)
|
- 任何 http / https 图片链接(系统明确要求除外)
|
||||||
|
|
||||||
所有图片展示均由系统统一处理。你只需负责正确调用工具,并严格区分生成和编辑的不同规则,特别是编辑时的数量限制和默认行为。
|
所有图片展示由系统统一处理,你只负责正确调用工具,并严格遵守生成/编辑规则,尤其是数量与目标规则。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
SYSTEM_RULES_PROMPT_EN = """
|
SYSTEM_RULES_PROMPT_EN = """
|
||||||
@@ -130,10 +149,16 @@ Core Execution Rules (Must be strictly followed - Highest Priority)
|
|||||||
|
|
||||||
When the user requests to generate, modify, or edit furniture images (including keywords such as "generate", "draw", "create", "design", "modify", "edit", "change", "adjust", etc.):
|
When the user requests to generate, modify, or edit furniture images (including keywords such as "generate", "draw", "create", "design", "modify", "edit", "change", "adjust", etc.):
|
||||||
|
|
||||||
|
### === FOR IMAGE GENERATION (NEW MANDATORY FLOW) ===
|
||||||
|
When generating new furniture sketches:
|
||||||
|
1. FIRST call the **generate_furniture_sketch_prompts** tool with the user's furniture description.
|
||||||
|
2. Use the list of prompts returned by this tool as the `prompts` parameter for image generation.
|
||||||
|
3. Do NOT use raw user input directly as generation prompts — this is mandatory.
|
||||||
|
4. The tool will return 12 rule-compliant, distinct black-and-white line drawing prompts optimized for Flux2 klein.
|
||||||
|
|
||||||
### Generation Rules (for generate_furniture and other generation tools)
|
### Generation Rules (for generate_furniture and other generation tools)
|
||||||
- All generated furniture images **must be clean black-and-white line drawings** (furniture sketch / line drawing / concept sketch / technical line drawing).
|
- All generated furniture images **must be clean black-and-white line drawings** (furniture sketch / line drawing / concept sketch / technical line drawing).
|
||||||
- You may **appropriately beautify and improve** the images while staying faithful to the user's description.
|
- Prompts come from generate_furniture_sketch_prompts, ensuring lines are clean, proportions balanced, composition harmonious, and style professional.
|
||||||
This includes making lines cleaner, proportions more balanced, composition more harmonious, and overall aesthetics more professional.
|
|
||||||
- Do not significantly deviate from the user's core intent or add elements not mentioned by the user.
|
- Do not significantly deviate from the user's core intent or add elements not mentioned by the user.
|
||||||
|
|
||||||
### Editing Rules (for edit_furniture, edit_quote_upload_furniture, etc.)
|
### Editing Rules (for edit_furniture, edit_quote_upload_furniture, etc.)
|
||||||
@@ -152,7 +177,7 @@ When the user requests to generate, modify, or edit furniture images (including
|
|||||||
|
|
||||||
**Common Tool Calling Rules (for both generation and editing)**:
|
**Common Tool Calling Rules (for both generation and editing)**:
|
||||||
- Call image-related tools **only once** per response. Do not make multiple calls.
|
- Call image-related tools **only once** per response. Do not make multiple calls.
|
||||||
- Generation tools can produce a maximum of 12 images per call.
|
- Generation tools can produce a maximum of 12 images per call (matches the 12 prompts from the tool).
|
||||||
- If the user mentions "uploaded image", "the picture I provided", "this image", or provides a MinIO path → prioritize using `edit_quote_upload_furniture`.
|
- If the user mentions "uploaded image", "the picture I provided", "this image", or provides a MinIO path → prioritize using `edit_quote_upload_furniture`.
|
||||||
- If editing an image that was just generated in this conversation → use `edit_furniture`.
|
- If editing an image that was just generated in this conversation → use `edit_furniture`.
|
||||||
- When calling `generate_furniture` or `edit_quote_upload_furniture`, the `prompts` parameter **must be a list[str]**.
|
- When calling `generate_furniture` or `edit_quote_upload_furniture`, the `prompts` parameter **must be a list[str]**.
|
||||||
@@ -179,6 +204,7 @@ Whenever the user input involves:
|
|||||||
【4】Scheduling Principles
|
【4】Scheduling Principles
|
||||||
- `user_profile_subagent` is only responsible for **information collection**.
|
- `user_profile_subagent` is only responsible for **information collection**.
|
||||||
- `research_subagent` is only responsible for **report generation**.
|
- `research_subagent` is only responsible for **report generation**.
|
||||||
|
- `generate_furniture_sketch_prompts` is only responsible for **professional prompt generation for furniture sketches**.
|
||||||
Do not mix their responsibilities.
|
Do not mix their responsibilities.
|
||||||
|
|
||||||
========================
|
========================
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import asyncio
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
import uuid
|
import uuid
|
||||||
import logging
|
import logging
|
||||||
@@ -6,6 +8,7 @@ from langchain_core.runnables import RunnableConfig
|
|||||||
from minio import Minio
|
from minio import Minio
|
||||||
from langgraph.prebuilt import ToolRuntime
|
from langgraph.prebuilt import ToolRuntime
|
||||||
from src.core.config import settings
|
from src.core.config import settings
|
||||||
|
from src.server.utils.new_oss_client import oss_get_image
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
minio_client = Minio(settings.MINIO_URL, access_key=settings.MINIO_ACCESS, secret_key=settings.MINIO_SECRET, secure=settings.MINIO_SECURE)
|
minio_client = Minio(settings.MINIO_URL, access_key=settings.MINIO_ACCESS, secret_key=settings.MINIO_SECRET, secure=settings.MINIO_SECURE)
|
||||||
@@ -381,3 +384,11 @@ async def generate_or_edit_image(input_path=None, bucket_name="fida-public-bucke
|
|||||||
result = resp.json()
|
result = resp.json()
|
||||||
image_url = result.get("output_path", None)
|
image_url = result.get("output_path", None)
|
||||||
return image_url
|
return image_url
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
prompt = "A classic professional hand-drawn furniture concept sketch by an experienced senior furniture designer, strictly monochrome black and white. Centered is a modern minimalist three-seater sofa with slim solid oak legs and generously proportioned soft fabric cushions. Precise pencil linework with masterful varying line weights, elegant cross-hatching and fine marker shading to define volume, depth and comfortable silhouette. Light visible construction lines. Drawn on clean A3 white sketching paper with natural subtle paper grain and slight scan texture. Soft diffused studio light from the top left creates gentle grayscale shadows that emphasize the sofa's elegant proportions and relaxed form. Highly refined, technical yet artistic traditional furniture design sketch, clean and sophisticated."
|
||||||
|
|
||||||
|
url = asyncio.run(generate_or_edit_image(prompt=prompt))
|
||||||
|
img = oss_get_image(oss_client=minio_client, bucket=url.split('/')[0], object_name=url[url.find('/') + 1:])
|
||||||
|
img.show()
|
||||||
|
|||||||
112
src/server/deep_agent/tools/prompt_generation_tool.py
Normal file
112
src/server/deep_agent/tools/prompt_generation_tool.py
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
import logging
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from langchain_core.prompts import ChatPromptTemplate
|
||||||
|
from langchain_core.tools import tool
|
||||||
|
from langchain_qwq import ChatQwen
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
|
from src.core.config import settings
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
# 输入
|
||||||
|
class FurnitureSketchPromptInput(BaseModel):
|
||||||
|
user_description: str = Field(..., description="用户对家具的描述,例如:'一张工业风的皮革沙发,带有金属X型腿和宽扶手'")
|
||||||
|
num_variants: int = Field(default=12, description="要生成的变种数量,默认12个")
|
||||||
|
|
||||||
|
|
||||||
|
# 输出
|
||||||
|
class FurniturePromptsOutput(BaseModel):
|
||||||
|
prompts: List[str] = Field(..., description="12个(或指定数量)明显不同的黑白家具线稿 image prompt 列表")
|
||||||
|
|
||||||
|
|
||||||
|
PROMPT_GEN_SYSTEM = """你是一位顶级家具设计 prompt 工程师,专门为黑白线稿(furniture sketch)生成高质量 image prompt。
|
||||||
|
|
||||||
|
核心强制规则(所有12个 prompt 都必须严格遵守):
|
||||||
|
- 必须是 clean black and white line drawing only
|
||||||
|
- pure white background
|
||||||
|
- 焦点只在家具本身的物理形态:silhouette, proportions, structure, legs, base, frame, joints, armrests, backrest, seat shape 和所有设计细节
|
||||||
|
- 使用 refined linework with subtle line weight variation
|
||||||
|
- 只允许 minimal soft shading for depth only
|
||||||
|
- 严格禁止:no color, no fill, no heavy shadows, no hatching, no texture rendering, no environment, no background elements, no lighting effects, no atmosphere
|
||||||
|
- 视角默认 3/4 front view, eye-level perspective
|
||||||
|
- 构图:centered composition
|
||||||
|
- 整体风格:architectural line art style + modern industrial design sketch style
|
||||||
|
|
||||||
|
任务:
|
||||||
|
根据用户提供的家具描述,生成 **12 个明显不同的** image prompt 变种。
|
||||||
|
每个变种必须在以下家具设计细节维度上有清晰、可感知的差异:
|
||||||
|
- 线条特性(极细精确、粗细强烈对比、手绘流动、动态表现力等)
|
||||||
|
- 结构侧重(整体比例、机械连接、关节细节、腿部与底部、金属框架、曲线轮廓等)
|
||||||
|
- 阴影与深度处理(几乎无阴影、轻微结构暗示、适度体积感等)
|
||||||
|
- 艺术调性(极简技术制图、粗犷工业、手绘艺术、精确建筑、高细节精致、柔和形态等)
|
||||||
|
- 微调视角或构图(标准3/4、略低角度强调腿部、略强调对称等)
|
||||||
|
|
||||||
|
确保12个 prompt 各有特色,不要相似。用户描述要自然放在 prompt 开头,然后自然衔接风格描述。
|
||||||
|
整个 prompt 要简洁有力、适合 Flux2 klein模型直接使用。
|
||||||
|
|
||||||
|
输出要求:
|
||||||
|
必须以 JSON 格式返回,严格遵循以下结构,不要添加任何解释、markdown 或额外文字。
|
||||||
|
|
||||||
|
{{
|
||||||
|
"prompts": [
|
||||||
|
"第一个完整 prompt",
|
||||||
|
"第二个完整 prompt",
|
||||||
|
...
|
||||||
|
]
|
||||||
|
}}
|
||||||
|
"""
|
||||||
|
|
||||||
|
prompt_gen_llm = ChatQwen(
|
||||||
|
model="qwen-plus",
|
||||||
|
max_tokens=3_000,
|
||||||
|
timeout=None,
|
||||||
|
max_retries=2,
|
||||||
|
streaming=False,
|
||||||
|
temperature=0.25,
|
||||||
|
top_p=0.8,
|
||||||
|
api_key=settings.QWEN_API_KEY
|
||||||
|
)
|
||||||
|
|
||||||
|
prompt_template = ChatPromptTemplate.from_messages([
|
||||||
|
("system", PROMPT_GEN_SYSTEM),
|
||||||
|
("human", "家具描述: {user_description}\n生成数量: {num_variants}\n请严格以 JSON 格式输出。")
|
||||||
|
])
|
||||||
|
|
||||||
|
prompt_chain = prompt_template | prompt_gen_llm
|
||||||
|
|
||||||
|
|
||||||
|
# 你可以把之前我给你的 3 个经典 Prompt 作为 reference_examples 放进去(few-shot)
|
||||||
|
|
||||||
|
|
||||||
|
@tool(args_schema=FurnitureSketchPromptInput)
|
||||||
|
def generate_furniture_sketch_prompts(user_description: str, num_variants: int = 12) -> List[str]:
|
||||||
|
"""
|
||||||
|
生成12个明显不同的家具黑白线稿 prompt。
|
||||||
|
成功时返回 List[str](长度为 num_variants)。
|
||||||
|
失败时返回 [user_description],保证至少有一个可用 prompt。
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# 使用结构化输出
|
||||||
|
structured_llm = prompt_gen_llm.with_structured_output(schema=FurniturePromptsOutput, method="json_mode")
|
||||||
|
|
||||||
|
chain = prompt_template | structured_llm
|
||||||
|
|
||||||
|
result: FurniturePromptsOutput = chain.invoke({
|
||||||
|
"user_description": user_description,
|
||||||
|
"num_variants": num_variants
|
||||||
|
})
|
||||||
|
|
||||||
|
if isinstance(result.prompts, list) and len(result.prompts) > 0:
|
||||||
|
return result.prompts[:num_variants] # 防止意外多返回
|
||||||
|
|
||||||
|
# 如果返回的 list 为空,进入兜底
|
||||||
|
logger.warning("Structured output returned empty list, falling back to user description.")
|
||||||
|
return [user_description]
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
# 失败兜底:返回用户的原始描述
|
||||||
|
logger.error(f"Failed to generate structured furniture prompts: {e}. Falling back to user description.")
|
||||||
|
return [user_description]
|
||||||
Reference in New Issue
Block a user