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

相关推荐
XLYcmy7 小时前
大语言模型(LLM)核心技术梳理:从词元化到 RAG 的工程实践
自然语言处理·prompt·embedding·token·cot·tokenization·上下文工程
hrrrrxeeeee1 天前
文件读取→比对→风险标记,拆解采购 AI 完整工作链路
大数据·人工智能·机器学习·prompt
技灵AI2 天前
Wan 3.0 API怎么做多参考商品视频?从图片、视频、音频分工到30秒交付
人工智能·prompt·aigc·音视频·wan 3.0
“AI国潮设计-小江”2 天前
【SDXL实战】Python自动化生成3D潮汕美食IP,附ComfyUI工作流与商用变现思路
开发语言·人工智能·python·prompt·aigc
TK泰妞3 天前
2026TikTok带货视频怎么做?
人工智能·prompt·音视频
XLYcmy3 天前
Prompt 设计相关问题
网络安全·llm·prompt·agent·cot·漏洞检测·harness
七夜zippoe4 天前
Prompt 工程进阶:角色设定、任务分解、输出约束与示例引导
ai·prompt·openai·agent·工程进阶
小叶肥辉4 天前
LangChain链和LangGraph图的学习笔记【六】——提示语模板(3)——Few-Shot Prompting(少样本提示) 模板类
笔记·python·学习·langchain·prompt·aigc
znnnk4 天前
【AI应用】Agent:AI 为什么需要“自主决策”?
ai·prompt·agent·workflow·ai应用·skill·mcp
学习日记5254 天前
AI PPT生成系统实践:从自由生成到可控生成的工程演进复盘
人工智能·ai·prompt