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

相关推荐
AI成长日志10 小时前
【AI原生开发实战】2.1 Prompt工程基础:编写高质量提示词
人工智能·prompt·ai-native
Bill Adams12 小时前
如何基于Harness Engineering设计一个Agent OS
人工智能·prompt·agent·智能体·harness
斯坦SteinY13 小时前
Git Worktree + Claude Code同时开发多个功能
人工智能·chatgpt·prompt·aigc·claude·并行开发
枫叶v.14 小时前
Prompt Engineering、Context Engineering、Harness Engineering:它们到底是什么关系呢
大数据·人工智能·prompt
飞Link1 天前
大模型时代的“语言编程”:Prompt Engineering (提示词工程) 深度解析与实战指南
开发语言·python·prompt
zzb15802 天前
系统提示词-System Prompt 动态组装
人工智能·后端·python·prompt
小橙子学AI2 天前
AI 编程的 Prompt 工程:如何写出高质量指令
人工智能·prompt
小林学编程2 天前
模型上下文协议(MCP)的理解
java·后端·llm·prompt·resource·tool·mcp协议
chQHk57BN2 天前
解密Prompt系列69. 从上下文管理到Runtime操作系统
prompt
北邮刘老师2 天前
暗数据:智能体探索世界的下一步
人工智能·大模型·prompt·智能体·智能体互联网