|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +# pylint: disable=no-self-use,protected-access,too-many-locals |
| 5 | + |
| 6 | +from unittest.mock import MagicMock, patch |
| 7 | + |
| 8 | +import pytest |
| 9 | +from langchain.agents import AgentExecutor, create_tool_calling_agent |
| 10 | +from langchain_core.agents import AgentActionMessageLog |
| 11 | +from langchain_core.messages import AIMessage |
| 12 | +from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder |
| 13 | +from langchain_core.tools import Tool |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def mock_search_tool(): |
| 18 | + mock_tool = Tool( |
| 19 | + name="duckduckgo_results_json", |
| 20 | + func=MagicMock(return_value=[{"result": "Amazon founded in 1994"}]), |
| 21 | + description="Search for information", |
| 22 | + ) |
| 23 | + return mock_tool |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def mock_model(): |
| 28 | + model = MagicMock() |
| 29 | + model.bind_tools = MagicMock(return_value=model) |
| 30 | + |
| 31 | + # Return proper AgentActionMessageLog instead of raw AIMessage |
| 32 | + model.invoke = MagicMock( |
| 33 | + return_value=AIMessage( |
| 34 | + content="", |
| 35 | + additional_kwargs={ |
| 36 | + "tool_calls": [ |
| 37 | + { |
| 38 | + "id": "call_123", |
| 39 | + "type": "function", |
| 40 | + "function": { |
| 41 | + "name": "duckduckgo_results_json", |
| 42 | + "arguments": '{"query": "Amazon founding date"}', |
| 43 | + }, |
| 44 | + } |
| 45 | + ] |
| 46 | + }, |
| 47 | + ) |
| 48 | + ) |
| 49 | + return model |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture |
| 53 | +def mock_prompt(): |
| 54 | + return ChatPromptTemplate.from_messages( |
| 55 | + [ |
| 56 | + ("system", "You are a helpful assistant"), |
| 57 | + ("human", "{input}"), |
| 58 | + MessagesPlaceholder(variable_name="agent_scratchpad"), |
| 59 | + ] |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +def test_agents(instrument_langchain, span_exporter, mock_model, mock_search_tool, mock_prompt): |
| 64 | + tools = [mock_search_tool] |
| 65 | + |
| 66 | + agent = create_tool_calling_agent(mock_model, tools, mock_prompt) |
| 67 | + agent_executor = AgentExecutor(agent=agent, tools=tools) |
| 68 | + |
| 69 | + # Mock the agent's intermediate steps |
| 70 | + with patch("langchain.agents.AgentExecutor._iter_next_step") as mock_iter: |
| 71 | + mock_iter.return_value = [ |
| 72 | + ( |
| 73 | + AgentActionMessageLog( |
| 74 | + tool="duckduckgo_results_json", |
| 75 | + tool_input={"query": "Amazon founding date"}, |
| 76 | + log="", |
| 77 | + message_log=[AIMessage(content="")], |
| 78 | + ), |
| 79 | + "Tool result", |
| 80 | + ) |
| 81 | + ] |
| 82 | + |
| 83 | + span_exporter.clear() |
| 84 | + agent_executor.invoke({"input": "When was Amazon founded?"}) |
| 85 | + |
| 86 | + spans = span_exporter.get_finished_spans() |
| 87 | + assert {span.name for span in spans} == { |
| 88 | + "chain AgentExecutor", |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | +def test_agents_with_events_with_content( |
| 93 | + instrument_with_content, span_exporter, mock_model, mock_search_tool, mock_prompt |
| 94 | +): |
| 95 | + tools = [mock_search_tool] |
| 96 | + |
| 97 | + agent = create_tool_calling_agent(mock_model, tools, mock_prompt) |
| 98 | + agent_executor = AgentExecutor(agent=agent, tools=tools) |
| 99 | + |
| 100 | + with patch("langchain.agents.AgentExecutor._iter_next_step") as mock_iter: |
| 101 | + mock_iter.return_value = [ |
| 102 | + ( |
| 103 | + AgentActionMessageLog( |
| 104 | + tool="duckduckgo_results_json", |
| 105 | + tool_input={"query": "AWS definition"}, |
| 106 | + log="", |
| 107 | + message_log=[AIMessage(content="")], |
| 108 | + ), |
| 109 | + "Tool result", |
| 110 | + ) |
| 111 | + ] |
| 112 | + |
| 113 | + span_exporter.clear() |
| 114 | + agent_executor.invoke({"input": "What is AWS?"}) |
| 115 | + |
| 116 | + spans = span_exporter.get_finished_spans() |
| 117 | + assert {span.name for span in spans} == { |
| 118 | + "chain AgentExecutor", |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | +def test_agents_with_events_with_no_content( |
| 123 | + instrument_langchain, span_exporter, mock_model, mock_search_tool, mock_prompt |
| 124 | +): |
| 125 | + tools = [mock_search_tool] |
| 126 | + |
| 127 | + agent = create_tool_calling_agent(mock_model, tools, mock_prompt) |
| 128 | + agent_executor = AgentExecutor(agent=agent, tools=tools) |
| 129 | + |
| 130 | + with patch("langchain.agents.AgentExecutor._iter_next_step") as mock_iter: |
| 131 | + mock_iter.return_value = [ |
| 132 | + ( |
| 133 | + AgentActionMessageLog( |
| 134 | + tool="duckduckgo_results_json", |
| 135 | + tool_input={"query": "AWS information"}, |
| 136 | + log="", |
| 137 | + message_log=[AIMessage(content="")], |
| 138 | + ), |
| 139 | + "Tool result", |
| 140 | + ) |
| 141 | + ] |
| 142 | + |
| 143 | + span_exporter.clear() |
| 144 | + agent_executor.invoke({"input": "What is AWS?"}) |
| 145 | + |
| 146 | + spans = span_exporter.get_finished_spans() |
| 147 | + assert {span.name for span in spans} == { |
| 148 | + "chain AgentExecutor", |
| 149 | + } |
0 commit comments