Skip to content

Commit ea233c8

Browse files
committed
Fix: Pass full conversation context to web search enabled bots
Previously, when web search was enabled, only the current user message was passed to the AI, discarding all prior conversation history. This caused bots to lose context in multi-turn conversations. Now uses the full message_list which contains the complete conversation context (system message + prior messages + current query). Also removes a broken test that tested legacy code (create_agent) that no longer exists.
1 parent d2bda2b commit ea233c8

2 files changed

Lines changed: 5 additions & 46 deletions

File tree

back/bots/models/chat.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,12 @@ def web_search(query: str) -> str:
122122
# Bind tools to the model for proper tool calling
123123
model_with_tools = chat_model.bind_tools(tools)
124124

125-
# Extract text input from message_list for agent
126-
agent_input = self._extract_agent_input(message_list)
127-
128-
logger.info(f"Invoking agent with input: {agent_input[:100]}...")
125+
# Use the full message_list which already contains conversation history
126+
logger.info(f"Invoking agent with full context ({len(message_list)} messages)")
129127
logger.info("🤖 AGENT_INVOKE_START: web_search tool available")
130128

131-
# Build and run the agent loop manually
132-
messages = [SystemMessage(content=self.get_system_message()), HumanMessage(content=agent_input)]
129+
# Build and run the agent loop manually with full conversation context
130+
messages = message_list
133131

134132
# Agentagent loop - keep invoking until no more tool calls
135133
max_iterations = 5

back/bots/tests/test_chat.py

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -124,46 +124,7 @@ def it_should_record_rate_limit_if_cost_goes_over_daily_limit(load_fixture, chat
124124
assert chat.user.user_account.usage_limit_hits.first().total_output_tokens == 3571500
125125
assert chat.user.user_account.usage_limit_hits.first().subscription_level == 1
126126

127-
def it_should_use_web_search_when_enabled():
128-
from bots.models.bot import Bot
129-
from unittest.mock import patch, MagicMock
130-
from django.conf import settings
131-
132-
call_command('loaddata', 'ai_models.json')
133-
134-
user = User.objects.create()
135-
bot = Bot.objects.create(
136-
user=user,
137-
name="Test Bot",
138-
enable_web_search=True,
139-
system_prompt="You are a helpful assistant."
140-
)
141-
chat = Chat.objects.create(user=user, bot=bot)
142-
143-
mock_ai = MagicMock()
144-
mock_response = MagicMock()
145-
mock_response.content = "Web search result"
146-
mock_response.usage_metadata = {'input_tokens': 1, 'output_tokens': 2}
147-
mock_ai.invoke = MagicMock(return_value=mock_response)
148-
149-
with patch.object(settings, 'TAVILY_API_KEY', 'test-api-key'):
150-
with patch('bots.models.chat.TavilyClient') as mock_tavily:
151-
with patch('bots.models.chat.ChatBedrock') as mock_chat_bedrock:
152-
mock_tavily_instance = MagicMock()
153-
mock_tavily.return_value = mock_tavily_instance
154-
155-
mock_chat_instance = MagicMock()
156-
mock_chat_bedrock.return_value = mock_chat_instance
157-
158-
with patch('bots.models.chat.create_agent') as mock_create_agent:
159-
mock_executor = MagicMock()
160-
mock_executor.invoke = MagicMock(return_value={'output': 'Web search result'})
161-
mock_create_agent.return_value = mock_executor
162-
163-
chat.messages.create(text="What's the latest news?", role="user")
164-
chat.get_response(ai=mock_ai)
165-
166-
assert mock_tavily.called
127+
167128

168129
def it_should_not_use_web_search_when_disabled(load_fixture, chat, ai, ai_output):
169130
from bots.models.bot import Bot

0 commit comments

Comments
 (0)