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

相关推荐
微臣愚钝2 小时前
prompt
人工智能·深度学习·prompt
是小蟹呀^3 小时前
【总结】提示词工程
python·llm·prompt·agent
卷卷说风控7 小时前
Claude Code 技术架构深扒:Prompt / Context / Harness 三维设计实践
人工智能·架构·prompt
华农DrLai12 小时前
怎么用大模型生成推荐的训练数据?Data Augmentation怎么做?
数据库·人工智能·大模型·nlp·prompt
一江寒逸13 小时前
【30天做一个生产级RAG知识库系统】第5篇:Prompt工程与大模型调用封装,解决幻觉问题
人工智能·prompt
sun_tao114 小时前
Prompt工程实践
人工智能·llm·prompt·agent
华农DrLai1 天前
什么是LLM做推荐的三种范式?Prompt-based、Embedding-based、Fine-tuning深度解析
人工智能·深度学习·prompt·transformer·知识图谱·embedding
海海不掉头发1 天前
【大模型Prompt-Tuning方法进阶+提示词】-基础学习篇
人工智能·学习·prompt
黑金IT1 天前
从“抽卡”到“工业化”:多模态 Harness 如何重塑 AI 内容生产的反馈闭环
人工智能·prompt·harness工程
人工智能培训2 天前
多模态AI模型融合难?核心问题与解决思路
人工智能·机器学习·prompt·agent·智能体