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

相关推荐
猿类崛起@1 天前
2025秋招LLM大模型多模态面试题:110道大模型面试常见问题及答案,助你拿下AI工程师岗位!
人工智能·机器学习·ai·性能优化·大模型·prompt·大模型训练
海边夕阳20061 天前
【每天一个AI小知识】:什么是Prompt?
人工智能·prompt
极速learner1 天前
n8n本地安装的两种方法:小白入门大白话版本
人工智能·prompt
nvd111 天前
Agent架构升级:解决Gemini超大Prompt处理问题
架构·prompt
阿杰学AI1 天前
AI核心知识23——大语言模型之System Prompt(简洁且通俗易懂版)
人工智能·ai·语言模型·prompt·aigc·system prompt
AhaPuPu1 天前
LLM Agent Attack- Indirect Prompt Injection
网络·人工智能·prompt
优选资源分享1 天前
Prompt Optimizer v2.2.1:开源AI提示词优化工具
prompt·实用工具
风雨中的小七1 天前
解密Prompt系列65. 三巨头关于大模型内景的硬核论文
prompt
技术传感器1 天前
Prompt工程的艺术与科学:从“对话“到“编程“,掌握与大模型高效协作的元技能
人工智能·microsoft·架构·prompt·aigc
红蒲公英1 天前
( 教学 )Agent 构建 Prompt(提示词)2. CommaSeparatedListOutputParser
人工智能·python·langchain·prompt·langgraph