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

相关推荐
手揽回忆怎么睡4 小时前
一套 Prompt 组合
prompt
AI Echoes5 小时前
LangChain Runnable组件重试与回退机制降低程序错误率
人工智能·python·langchain·prompt·agent
wasp5207 小时前
Banana Slides 深度解析:AI Core 架构设计与 Prompt 工程实践
人工智能·prompt·bash
小明_GLC11 小时前
大模型幻觉以及如何设计合适的prompt回答用户问题
人工智能·prompt
彷徨的蜗牛12 小时前
用 Claude Code 重新定义编程效率:从 Prompt 工程到完整项目实战
prompt·ai 编程·claude code
石去皿1 天前
Depth Viewer: 16-bit 深度图可视化工具
人工智能·chatgpt·prompt
猫头虎1 天前
2026全网最热Claude Skills工具箱,GitHub上最受欢迎的7大Skills开源AI技能库
langchain·开源·prompt·github·aigc·ai编程·agi
伊甸31 天前
基于LangChain4j从0到1搭建自己的的AI智能体并部署上线-1
java·langchain·prompt
汉克老师1 天前
小学生0基础学大语言模型应用(第 19 课《字符串提示词训练(Prompt Thinking)》)
人工智能·深度学习·机器学习·语言模型·prompt·提示词
北京地铁1号线1 天前
4.1 提示词(Prompt)工程
prompt