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

相关推荐
leo03088 小时前
7种流行Prompt设计模式详解:适用场景与最佳实践
设计模式·prompt
风雨中的小七1 天前
解密prompt系列60. Agent实战:从0搭建Jupter数据分析智能体
prompt
zzywxc7872 天前
AI在金融、医疗、教育、制造业等领域的落地案例(含代码、流程图、Prompt示例与图表)
人工智能·spring·机器学习·金融·数据挖掘·prompt·流程图
bboyzqh2 天前
任务型Agent:prompt工程实践
大模型·prompt·上下文工程
美人鱼战士爱学习2 天前
2024 arXiv Cost-Efficient Prompt Engineering for Unsupervised Entity Resolution
prompt
水的精神2 天前
写好 Prompt 的 12 条实践经验
prompt
Wilber的技术分享2 天前
【大模型实战笔记 1】Prompt-Tuning方法
人工智能·笔记·机器学习·大模型·llm·prompt
relis3 天前
解密llama.cpp:Prompt Processing如何实现高效推理?
prompt·llama
relis3 天前
解密大语言模型推理:Prompt Processing 的内存管理与计算优化
android·语言模型·prompt
relis4 天前
大语言模型推理的幕后英雄:深入解析Prompt Processing工作机制
人工智能·语言模型·prompt