Files
FoodClassifier/exp_multimodal/prompts.py
T

41 lines
1.6 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.
from typing import Dict, List, Optional
def build_closedset_prompt(
labels: List[str],
fewshot_hints: Optional[Dict[str, str]] = None,
ingredient_only: bool = False,
) -> str:
label_str = "".join(labels)
hint_lines: List[str] = []
if fewshot_hints:
hint_lines.append("以下是少量文字示例帮助你区分:")
for k, v in list(fewshot_hints.items())[:20]:
hint_lines.append(f"- {k}{v}")
hints = "\n".join(hint_lines)
constraint = (
"你只需输出食材名称,不要包含任何烹饪方式或形态描述(如切丝/切片/炒/煮/红烧/清炒等)。\n"
if ingredient_only
else ""
)
return (
"【重要】你必须且只能输出JSON格式,严禁输出任何其他内容(包括英文描述、解释、分析等)。\n\n"
"你是食品图像分类专家。任务:从给定标签集合中选择最符合图片的一个标签。\n"
f"标签集合(封闭集,中文):{label_str}\n\n"
f"{constraint}"
f"{hints}\n"
"要求:\n"
"1. 必须从标签集合中选择,禁止输出集合外的词\n"
"2. 给出0-1的置信度分数\n"
"3. 严格按以下JSON格式输出,不允许有任何偏差\n\n"
'输出格式(必须严格遵守):\n{"label":"<标签>","confidence":0.95}\n\n'
"4. 如果明显不在这些分类中,输出格式如下:"
' \n{"label":"<未知>","confidence":0.95}\n\n'
"【再次强调】只输出上述JSON,不要有任何额外文字、描述、解释或其他内容。"
)