多模态模型在做选择题时,如何设置Prompt,如何精准定位我们需要的选项

我们这里以Qwen2-VL-7B-instruct为例:

假设我们需要分析一张图片的情绪(从现有的情绪中进行选择),并且我们需要它以思维链的形式展现出来,我们可以这样设置prompt:

python 复制代码
emotion6_CoT = """
Analyze the given image and determine the emotion it represents.
Emotional options :(A) anger (B) disgust (C) fear (D) joy (E) sadness (F) surprise (G) neutral
Your output should follow this format strictly:
# analyze 
Your analyze here
# answer
Choice index, one of A-G
"""

这样设计的好处是,最终的answer中一定会有 # analyze 和 # answer 我们就可以利用正则表达式去进行准确提取:假设我们要提取其中的选项,我们可以这样写:

python 复制代码
def remove_words(s):
    # 定义需要删除的词汇列表
    words_to_remove = ['Choice', 'index', 'one', 'of', 'A-G']

    # 使用正则表达式删除这些词
    for word in words_to_remove:
        s = re.sub(r'\b' + word + r'\b', '', s)

    # 去除多余的空格
    s = re.sub(r'\s+', ' ', s).strip()

    return s

#ouput为输出列表,我们需要将里面的字符串进行提取,所以为output_text[0]。

option = re.search(r'[A-H]', remove_words(output_text[0].split("# answer")[1]))

最终,我们可以借用字典去匹配对应情绪即可。

注意:在一些推理能力不强的模型中(例如 Qwen2-base-7B),可能会遇到输出依然不遵循prompt的回答,这是正常的。

相关推荐
Patrick在香港4 小时前
Claude Prompt Caching 实测账单:第一轮贵 25%,从第二轮开始省 86%
python·prompt·claude·成本优化·prompt缓存·anthropic api
智嵌研习社5 小时前
从 Prompt 到工程化技能包:AI Skills 标准与 Continue 落地
人工智能·prompt·skill
会编程的土豆6 小时前
从零理解 AI Agent:概念、ReAct、Prompt 五要素与面试要点
人工智能·react.js·prompt
coft1 天前
Pi Agent 的 Token 成本控制:为什么核心是 Prompt Cache 前缀稳定性
prompt·ai编程·ai agent·pi agent
ShineWinsu2 天前
人工智能基础概念全景解析:从 AI 到 Transformer、LLM、Prompt、Token、RAG、Agent、对齐与安全
人工智能·prompt·transformer
大模型码小白2 天前
AI 提示词专栏:使用系统指令(System Prompt)实现全局约束
java·大数据·运维·开发语言·人工智能·python·prompt
仓三2 天前
从 Prompt Engineering 到 Context Engineering:2026 年 Agent 性能提升的隐藏杠杆
java·prompt·context
学习星球2 天前
2026年AI Agent全栈开发实战——从Prompt到Production
开发语言·人工智能·prompt
半夢半醒12 天前
查看 Oracle 数据库中的定时任务执行情况
人工智能·prompt
zzzll11112 天前
Skill 不只是 Prompt:从概念到实践
prompt