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

相关推荐
土星碎冰机3 小时前
ai自学笔记 (2.prompt 踩坑与优化)
笔记·ai·prompt·claude
耕烟煮云6 小时前
从Prompt到Context Engineering再到Harness,AI工程的演进
人工智能·prompt
JAVA面经实录9177 小时前
Java Codex 企业标准Prompt库
java·开发语言·prompt
AI技术控9 小时前
Prompt Engineering 在企业大模型应用中的实践:从提示词模板到可控输出
人工智能·python·深度学习·语言模型·自然语言处理·prompt
AdCj310 小时前
Perplexity 如何设计 Agent Skills:从 Prompt Engineering 到 Context Engineering
prompt
闵孚龙10 小时前
Claude Code Prompt Cache 缓存中断检测系统全解析:AI Agent 上下文工程、可观测性、成本优化与性能治理
人工智能·缓存·prompt
闵孚龙11 小时前
Claude Code 缓存优化模式全解析:AI Agent 上下文工程、Prompt Cache、工具 Schema 缓存、Token 成本优化
人工智能·缓存·prompt
weixin_428005301 天前
C#调用 AI学习从0开始-第1阶段(基础与工具)-第2天Prompt工程基础
人工智能·学习·c#·prompt
怪祝浙1 天前
AI实战之提示词工程(prompt)以及dify中prompt应用
prompt
qcx231 天前
【AI Agent实战】多 Agent 编排架构:五层模型与 RL 优化
网络·人工智能·ai·架构·prompt·agent