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

相关推荐
小程故事多_8012 小时前
深度拆解Hermes Agent,动态Prompt与Learning Loop架构的底层逻辑
人工智能·架构·prompt·aigc
小超同学你好13 小时前
OpenClaw 深度解析与源代码导读 · 第5篇:Brain——Prompt/Context/Harness Engineering 与执行框架
人工智能·深度学习·语言模型·prompt
一叶知秋yyds1 天前
Prompt Engineering 完全指南:让大模型更懂你
prompt
deepdata_cn2 天前
提示工程(Prompt Engineering)
人工智能·prompt
njsgcs2 天前
固定几轮调用工具后,让ai根据执行内容自己改进prompt实现进化
人工智能·prompt
MRDONG12 天前
从 Prompt 到智能体系统:Function Calling、Memory 与 Synthetic RAG 的全栈解析
人工智能·深度学习·神经网络·语言模型·自然语言处理·prompt
Flying pigs~~2 天前
企业级模块化RAG项目(mysql➕redis➕milvus➕模型微调➕bm25➕fastapi➕ollama➕Prompt➕多策略选择)
人工智能·redis·mysql·docker·prompt·milvus·rag
颯沓如流星2 天前
从Prompt Engineering到Harness Engineering:游戏服务器开发的AI工程范式
人工智能·游戏·prompt
华农DrLai2 天前
什么是推荐系统中的负反馈?用户的“踩“和“不感兴趣“怎么用?
人工智能·算法·llm·prompt·知识图谱
冲上云霄的Jayden2 天前
LangGraph4j+LangChain4J 实验智能客服系统增加基于LLM 解决Prompt注入问题
prompt·agent·智能客服·langchain4j·agent安全·langgraph4j·prompt注入