多模态模型在做选择题时,如何设置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 小时前
从Prompt Engineering到RAG:LLM应用开发实战与性能优化全解析
人工智能·prompt
AI_小站12 小时前
Loop Engineering又是啥?一文讲清企业Agent落地的四层工程进化论
java·人工智能·架构·prompt·大模型开发·智能体·大模型应用
官乐2 天前
Prompt推荐:如何写一个好的Github README.md
prompt·github
user-猴子2 天前
AiPy + Kimi K3:2048小游戏生成的技术分析
人工智能·语言模型·大模型·prompt
思绪漂移3 天前
工作日报 / 周报 Agent——发芽板块(含prompt和应用示例)
prompt·agent
思绪漂移4 天前
工作日报 / 周报 Agent——Bob说板块(含prompt和应用示例)
prompt·agent
陌路204 天前
初识大模型 - Prompt Engineering 实战
prompt
学习日记5255 天前
【提示词工程系统教程 05】上下文工程:静态指令、动态检索与RAG架构
人工智能·prompt
小白说大模型5 天前
AI Agent 调试实战:链路追踪、Prompt 可视化与异常定位的系统方法
java·人工智能·python·算法·prompt
老谷子.AI原始技术5 天前
《Claude Code工程化实践》加课4-Claude Code 的底层内功:Prompt Engineering
prompt·claude code