You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LlamaIndex中MistralAI大模型忽略系统提示词的技术问询

问题解决:让ReActAgent遵循自定义系统提示

问题分析

你给MistralAI实例设置了系统提示“表现得很愤怒,且每句话都使用banana一词”,但通过ReActAgent发起对话时,模型完全忽略了这个要求,返回了常规内容。这是因为ReActAgent有自己的默认prompt模板,会覆盖LLM实例的系统提示,导致你的风格要求没有被纳入Agent的思考和生成逻辑中。

解决方案

直接在初始化ReActAgent时,通过system_prompt参数传入你的风格要求,确保Agent的prompt模板包含这个规则。修改后的代码如下:

import nest_asyncio

nest_asyncio.apply()

from llama_index.llms.mistralai import MistralAI
from llama_index.embeddings.mistralai import MistralAIEmbedding
from llama_index.core import Settings
from llama_index.core import SimpleDirectoryReader
from llama_index.core import VectorStoreIndex
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.core.query_engine import SubQuestionQueryEngine
from prompts import new_prompt, instruction_str

# 初始化LLM时可保留系统提示,但关键是Agent要传入自定义规则
llm = MistralAI(model="mistral-large-latest", temperature=0.1)

embed_model = MistralAIEmbedding(model_name="mistral-embed")

Settings.llm = llm
Settings.embed_model = embed_model

gameRules = SimpleDirectoryReader(input_files=["./data/game_rules.txt"]).load_data()
gameMemories = SimpleDirectoryReader(input_files=["./data/game-memory.txt"]).load_data()

gameRules_index = VectorStoreIndex.from_documents(gameRules)
gameRules_query_engine = gameRules_index.as_query_engine(similarity_top_k=5, instruction_str=instruction_str)

gameMemories_index = VectorStoreIndex.from_documents(gameMemories)
# 修正拼写错误:sisimilarity_top_k → similarity_top_k
gameMemories_query_engine = gameMemories_index.as_query_engine(similarity_top_k=5)

query_engine_tools = [
    QueryEngineTool(
        query_engine=gameRules_query_engine,
        metadata=ToolMetadata(
            name="gameRules",
            description="Provides information about the game rules"
        ),
    ),
    QueryEngineTool(
        query_engine=gameMemories_query_engine,
        metadata=ToolMetadata(
            name="gameMemories",
            description="Provides information about the game Memories of the agent"
        ),
    ),
]

sub_question_query_engine = SubQuestionQueryEngine.from_defaults(
    query_engine_tools=query_engine_tools, llm=llm)

from llama_index.core.agent import ReActAgent

# 关键修改:初始化ReActAgent时传入自定义系统提示
agent = ReActAgent.from_tools(
    llm=llm, 
    verbose=True,
    system_prompt="Act like you're mad, and use the word banana in every sentence."
)  

response = agent.chat("tell me about the moon landing")
print(response)

额外优化

如果需要保留Agent原本的思考逻辑,同时加入风格要求,可以自定义prompt模板,将两者结合:

from llama_index.core.agent.react.prompts import REACT_SYSTEM_PROMPT

# 合并Agent默认系统提示与自定义风格要求
custom_system_prompt = f"""{REACT_SYSTEM_PROMPT}

额外要求:表现得很愤怒,且每句话都使用banana一词。"""

agent = ReActAgent.from_tools(
    llm=llm, 
    verbose=True,
    system_prompt=custom_system_prompt
)

这样修改后,Agent在执行思考和生成回复时,会严格遵循你设定的风格要求。

内容的提问来源于stack exchange,提问作者Mathieu Gouin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 13:17:40