mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-06-26 22:00:13 +00:00
feat: Warn users if Agent is called with only system messages (#9514)
* Add warning message and raise error in agent run method * Add tests * Add reno * Updates * Updates
This commit is contained in:
parent
580683b79d
commit
379df4ab84
@ -12,7 +12,7 @@ from haystack.core.pipeline.async_pipeline import AsyncPipeline
|
||||
from haystack.core.pipeline.pipeline import Pipeline
|
||||
from haystack.core.pipeline.utils import _deepcopy_with_exceptions
|
||||
from haystack.core.serialization import component_to_dict
|
||||
from haystack.dataclasses import ChatMessage
|
||||
from haystack.dataclasses import ChatMessage, ChatRole
|
||||
from haystack.dataclasses.streaming_chunk import StreamingCallbackT, select_streaming_callback
|
||||
from haystack.tools import Tool, Toolset, deserialize_tools_or_toolset_inplace, serialize_tools_or_toolset
|
||||
from haystack.utils.callable_serialization import deserialize_callable, serialize_callable
|
||||
@ -69,7 +69,7 @@ class Agent:
|
||||
max_agent_steps: int = 100,
|
||||
raise_on_tool_invocation_failure: bool = False,
|
||||
streaming_callback: Optional[StreamingCallbackT] = None,
|
||||
):
|
||||
) -> None:
|
||||
"""
|
||||
Initialize the agent component.
|
||||
|
||||
@ -237,6 +237,7 @@ class Agent:
|
||||
- "messages": List of all messages exchanged during the agent's run.
|
||||
- "last_message": The last message exchanged during the agent's run.
|
||||
- Any additional keys defined in the `state_schema`.
|
||||
:raises RuntimeError: If the Agent component wasn't warmed up before calling `run()`.
|
||||
"""
|
||||
if not self._is_warmed_up and hasattr(self.chat_generator, "warm_up"):
|
||||
raise RuntimeError("The component Agent wasn't warmed up. Run 'warm_up()' before calling 'run()'.")
|
||||
@ -244,6 +245,12 @@ class Agent:
|
||||
if self.system_prompt is not None:
|
||||
messages = [ChatMessage.from_system(self.system_prompt)] + messages
|
||||
|
||||
if all(m.is_from(ChatRole.SYSTEM) for m in messages):
|
||||
logger.warning(
|
||||
"All messages provided to the Agent component are system messages. This is not recommended as the "
|
||||
"Agent will not perform any actions specific to user input. Consider adding user messages to the input."
|
||||
)
|
||||
|
||||
state = State(schema=self.state_schema, data=kwargs)
|
||||
state.set("messages", messages)
|
||||
component_visits = dict.fromkeys(["chat_generator", "tool_invoker"], 0)
|
||||
@ -332,6 +339,7 @@ class Agent:
|
||||
- "messages": List of all messages exchanged during the agent's run.
|
||||
- "last_message": The last message exchanged during the agent's run.
|
||||
- Any additional keys defined in the `state_schema`.
|
||||
:raises RuntimeError: If the Agent component wasn't warmed up before calling `run_async()`.
|
||||
"""
|
||||
if not self._is_warmed_up and hasattr(self.chat_generator, "warm_up"):
|
||||
raise RuntimeError("The component Agent wasn't warmed up. Run 'warm_up()' before calling 'run_async()'.")
|
||||
@ -339,6 +347,12 @@ class Agent:
|
||||
if self.system_prompt is not None:
|
||||
messages = [ChatMessage.from_system(self.system_prompt)] + messages
|
||||
|
||||
if all(m.is_from(ChatRole.SYSTEM) for m in messages):
|
||||
logger.warning(
|
||||
"All messages provided to the Agent component are system messages. This is not recommended as the "
|
||||
"Agent will not perform any actions specific to user input. Consider adding user messages to the input."
|
||||
)
|
||||
|
||||
state = State(schema=self.state_schema, data=kwargs)
|
||||
state.set("messages", messages)
|
||||
component_visits = dict.fromkeys(["chat_generator", "tool_invoker"], 0)
|
||||
|
@ -0,0 +1,4 @@
|
||||
---
|
||||
enhancements:
|
||||
- |
|
||||
- If only system messages are provided as input a warning will be logged to the user indicating that this likely not intended and that they should probably also provide user messages.
|
@ -719,6 +719,21 @@ class TestAgent:
|
||||
with pytest.raises(RuntimeError, match="The component Agent wasn't warmed up."):
|
||||
agent.run([ChatMessage.from_user("What is the weather in Berlin?")])
|
||||
|
||||
def test_run_no_messages(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "fake-key")
|
||||
chat_generator = OpenAIChatGenerator()
|
||||
agent = Agent(chat_generator=chat_generator, tools=[])
|
||||
agent.warm_up()
|
||||
result = agent.run([])
|
||||
assert result["messages"] == []
|
||||
|
||||
def test_run_only_system_prompt(self, caplog):
|
||||
chat_generator = MockChatGeneratorWithoutRunAsync()
|
||||
agent = Agent(chat_generator=chat_generator, tools=[], system_prompt="This is a system prompt.")
|
||||
agent.warm_up()
|
||||
_ = agent.run([])
|
||||
assert "All messages provided to the Agent component are system messages." in caplog.text
|
||||
|
||||
@pytest.mark.skipif(not os.environ.get("OPENAI_API_KEY"), reason="OPENAI_API_KEY not set")
|
||||
@pytest.mark.integration
|
||||
def test_run(self, weather_tool):
|
||||
|
Loading…
x
Reference in New Issue
Block a user