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

相关推荐
Yeats_Liao9 小时前
微调决策树:何时使用Prompt Engineering,何时选择Fine-tuning?
前端·人工智能·深度学习·算法·决策树·机器学习·prompt
香芋Yu1 天前
【大模型教程——第四部分:大模型应用开发】第1章:提示工程与上下文学习 (Prompt Engineering & ICL)
学习·prompt
cheungxiongwei.com1 天前
使用 C++23 实现 Prompt DSL 的 Header-Only 解析器:从语法设计到工程落地
prompt·c++23
shangjian0071 天前
AI-大语言模型LLM-模型微调3-Prompt Tuning
人工智能·语言模型·prompt
Bruk.Liu2 天前
AI中的Agent、Prompt、MCP与Function Calling:从简单对话到智能执行
人工智能·prompt·mcp
猫头虎3 天前
中国开源大模型霸榜全球:全球开源大模型排行榜前十五名,全部由中国模型占据
langchain·开源·prompt·aigc·ai编程·agi·ai-native
坠金3 天前
prompt
prompt
花间相见4 天前
【LangChain】—— Prompt、Model、Chain与多模型执行链
前端·langchain·prompt
qiukapi4 天前
四. Model I/O 之 Prompt Template
prompt·prompttemplate
Familyism4 天前
Prompt概述
prompt