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

相关推荐
寂寞恋上夜13 小时前
从需求到开发任务:WBS拆解的4个层级(附排期模板)
人工智能·prompt·markdown转xmind·deepseek思维导图
一个处女座的程序猿17 小时前
LLMs之Prompt:Fabric的简介、安装和使用方法、案例应用之详细攻略
llm·prompt·fabric
Biehmltym18 小时前
【AI】07 AI Agent可控风格LLM 问答(含FastAPI 求/返回/路由、跨域访问CORS、System Prompt)
人工智能·prompt·fastapi
gentle coder19 小时前
【Prompt】 提示词工程(持续更新中~)
ai·prompt·agent·提示词工程·#广告顾问
Joy T1 天前
【PE 最终章】思维链、自洽性与 Prompt 安全防御指南
网络·prompt·prompt engineer·提示词工程
张彦峰ZYF2 天前
提示词工程(Prompt Engineering):人与大语言模型之间的对话艺术与工程体系
人工智能·机器学习·prompt·提示词工程·提示词要素与框架
Blossom.1182 天前
Prompt工程与思维链优化实战:从零构建动态Few-Shot与CoT推理引擎
人工智能·分布式·python·智能手机·django·prompt·边缘计算
laplace01233 天前
讲清楚 Prompt, Agent, MCP 是什么+大模型测评流程
人工智能·语言模型·prompt·transformer
名誉寒冰3 天前
AI大模型-Prompt工程参考学习
人工智能·学习·大模型·prompt