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

相关推荐
沛沛老爹11 小时前
AI助手专业能力评估体系构建:四大维度+工具链实战
人工智能·自动化·prompt·agent·评估模板
琅琊榜首202012 小时前
AI+Python高效撰写高质量短剧:从Prompt到脚本落地实操
人工智能·python·prompt
deephub12 小时前
Prompt 缓存的四种策略:从精确匹配到语义检索
spring·缓存·prompt
reddingtons1 天前
Scenario: SLG 地图铺到吐?搭建“轴测流水线”,量产建筑不重样
游戏·3d·prompt·aigc·设计师·游戏美术·slg
deephub2 天前
LLM创造力可以被度量吗?一个基于提示词变更的探索性实验
人工智能·prompt·大语言模型
SunnyRivers2 天前
提示工程(Prompt Engineering)
prompt·提示词
大傻^2 天前
【Prompt工程实战】从0到1构建智能学员辅导系统:Text2SQL+逆向提示词全解析
prompt·提示词工程
logocode_li2 天前
Prompt,除了使用外,你了解其核心原理么?
ai·prompt·指令调优
曦云沐2 天前
第六篇:LangChain 1.0 消息系统与 Prompt 工程:从入门到精通的完整教程
人工智能·langchain·prompt·大模型开发框架
风雨中的小七2 天前
解密Prompt系列69. 从上下文管理到Runtime操作系统
prompt