多模态模型在做选择题时,如何设置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的回答,这是正常的。

相关推荐
TheRouter12 小时前
AI Agent 的Prompt Injection 防御实战:从EchoLeak 零点击外泄到6层防护栈(含可运行代码与对比表)
人工智能·ai·prompt
Nayxxu12 小时前
Claude Prompt Caching 详解:缓存写入、缓存读取与成本计算
缓存·prompt
AI布道师-wang12 小时前
第 6 章:Prompt 工程——和模型高效沟通
人工智能·机器学习·prompt
meilindehuzi_a13 小时前
Vibe Coding 实战:我用一条 Prompt 指挥 AI “盲盒式”生成 3D 积木物理世界
3d·prompt
霍格沃兹测试学院-小舟畅学1 天前
Anthropic 开源 Skills:Agent 工程化,开始从 Prompt 走向能力封装
prompt
玄米乌龙茶1231 天前
LLM 应用开发学习笔记:System Prompt 设计、注入风险与成本优化
笔记·学习·prompt
一条泥憨鱼1 天前
全面解析 AI 大模型中的 Prompt
人工智能·ai·prompt
sugar__salt1 天前
从Prompt到3D世界:用大模型精准构建你的迷你村庄
3d·ai·prompt·ai编程
闵孚龙1 天前
Claude Code 驾驭工程原则全解析:AI Agent、上下文工程、Prompt Cache、权限安全、A/B测试、长期记忆与多智能体架构底层方法论
人工智能·安全·prompt
记得多喝水o1 天前
Skill与Prompt区别解析
prompt