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

相关推荐
easy_coder15 小时前
《工程化视角下的Prompt设计与迭代:云诊断与CICD变更风控中的实践》
人工智能·云计算·prompt
码点滴16 小时前
上下文压缩不是“丢数据“:Context Compressor 的血缘追踪与 Prefix Cache 保护
人工智能·python·架构·prompt·ai编程
Generalzy19 小时前
用 prompt_toolkit + Rich 打造一个现代终端应用
prompt
AI木马人1 天前
3.【Prompt工程实战】如何设计一个可复用的Prompt系统?(避免每次手写提示词)
linux·服务器·人工智能·深度学习·prompt
胡利光2 天前
Harness Engineering 01|从 Prompt Engineering 到 Harness Engineering
prompt
干洋芋果果2 天前
AI念咒_浏览器测试自动化
prompt
qcx233 天前
【解构】DeepSeek V4 发布:技术报告深度解读 + 横向对比六大开源模型,我们的判断是……
人工智能·chatgpt·prompt
蓝色的音乐3 天前
GPT Image 2 提示词怎么写?分享一个 400+ 案例 Prompt Gallery
gpt·prompt
迦南的迦 亚索的索3 天前
AI_05_基于Prompt工程的金融行业项目
人工智能·金融·prompt
2501_940041743 天前
AI创建小游戏指令词
人工智能·游戏·prompt