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

相关推荐
贺国亚10 小时前
RAG检索增强-向量库与Chunking
prompt·embedding
Jing_jing_X12 小时前
从 Prompt 对话到 OpenClaw:Agent 是怎么一步步发展出来的?
ai·prompt·个人开发·ai应用开发
城事漫游Molly12 小时前
AI与质性研究的融合(三):AI赋能质性数据分析——从编码到理论构建的新范式
大数据·人工智能·机器学习·prompt·ai for science·智能体·定性研究
麦哲思科技任甲林13 小时前
白话skills之二:Prompt和Skills的区别是什么?
prompt·sop·skills
人工智能培训13 小时前
数字孪生赋能建筑行业 解锁工程全周期智慧管理
大数据·人工智能·机器学习·prompt·agent
-停泊2 天前
Skill和Prompt有何不同
prompt·agent·skill
贺国亚2 天前
01-Prompt工程-Few-Shot-CoT与Tool-Use
prompt
z小猫不吃鱼2 天前
12 Prompt Engineering 入门:提示词为什么会影响模型行为?
人工智能·gpt·自然语言处理·prompt
csdn_aspnet2 天前
Gemini赋能安全工程师,自动写PoC脚本,探索Gemini在网络安全领域辅助漏洞验证与POC生成的实战路径
安全·web安全·prompt·poc·gemini·工程师
AskHarries2 天前
Agent Prompt 是怎么影响行为的
prompt