Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
|
@@ -174,8 +174,20 @@ class ToolCallingAgentChat:
|
|
| 174 |
logger.info(f"Message: {message}")
|
| 175 |
logger.info(f"History length: {len(history)}")
|
| 176 |
|
| 177 |
-
# Convert history to messages for context
|
| 178 |
-
messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
for user_msg, assistant_msg in history:
|
| 180 |
messages.append(HumanMessage(content=user_msg))
|
| 181 |
if assistant_msg: # Only add if assistant responded
|
|
@@ -194,19 +206,43 @@ class ToolCallingAgentChat:
|
|
| 194 |
if ENABLE_DETAILED_LOGGING:
|
| 195 |
logger.info(f"=== LLM RESPONSE ===")
|
| 196 |
logger.info(f"Response type: {type(response)}")
|
|
|
|
| 197 |
logger.info(f"Has tool calls: {bool(response.tool_calls if hasattr(response, 'tool_calls') else False)}")
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
# Check if LLM wants to call tools
|
|
|
|
|
|
|
|
|
|
| 200 |
if hasattr(response, 'tool_calls') and response.tool_calls:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
if ENABLE_DETAILED_LOGGING:
|
| 202 |
logger.info(f"=== TOOL CALLS DETECTED ===")
|
| 203 |
-
logger.info(f"Number of tool calls: {len(
|
| 204 |
|
| 205 |
# Add the LLM response to messages
|
| 206 |
messages.append(response)
|
| 207 |
|
| 208 |
# Execute tool calls
|
| 209 |
-
for tool_call in
|
| 210 |
if ENABLE_DETAILED_LOGGING:
|
| 211 |
logger.info(f"Executing tool: {tool_call['name']} with args: {tool_call['args']}")
|
| 212 |
|
|
|
|
| 174 |
logger.info(f"Message: {message}")
|
| 175 |
logger.info(f"History length: {len(history)}")
|
| 176 |
|
| 177 |
+
# Convert history to messages for context with system message
|
| 178 |
+
from langchain_core.messages import SystemMessage
|
| 179 |
+
|
| 180 |
+
messages = [SystemMessage(content="""You are a helpful AI assistant with web search capabilities.
|
| 181 |
+
|
| 182 |
+
IMPORTANT: You have access to a web_search tool. Use it when users ask about:
|
| 183 |
+
- Current events, news, or recent information
|
| 184 |
+
- Today's date or current time
|
| 185 |
+
- Real-time data or recent updates
|
| 186 |
+
- Specific facts that may have changed recently
|
| 187 |
+
- When users explicitly ask you to search the web
|
| 188 |
+
|
| 189 |
+
For general knowledge questions that don't require current information, answer directly without using tools.""")]
|
| 190 |
+
|
| 191 |
for user_msg, assistant_msg in history:
|
| 192 |
messages.append(HumanMessage(content=user_msg))
|
| 193 |
if assistant_msg: # Only add if assistant responded
|
|
|
|
| 206 |
if ENABLE_DETAILED_LOGGING:
|
| 207 |
logger.info(f"=== LLM RESPONSE ===")
|
| 208 |
logger.info(f"Response type: {type(response)}")
|
| 209 |
+
logger.info(f"Response content: {response.content}")
|
| 210 |
logger.info(f"Has tool calls: {bool(response.tool_calls if hasattr(response, 'tool_calls') else False)}")
|
| 211 |
+
if hasattr(response, 'tool_calls') and response.tool_calls:
|
| 212 |
+
logger.info(f"Tool calls: {response.tool_calls}")
|
| 213 |
+
logger.info(f"Response additional_kwargs: {getattr(response, 'additional_kwargs', {})}")
|
| 214 |
|
| 215 |
# Check if LLM wants to call tools
|
| 216 |
+
tool_calls_to_execute = []
|
| 217 |
+
|
| 218 |
+
# Method 1: Proper tool calls
|
| 219 |
if hasattr(response, 'tool_calls') and response.tool_calls:
|
| 220 |
+
tool_calls_to_execute = response.tool_calls
|
| 221 |
+
|
| 222 |
+
# Method 2: Fallback - check if model mentioned search in content and user asked for current info
|
| 223 |
+
elif ("search" in message.lower() or "today" in message.lower() or "current" in message.lower() or "recent" in message.lower()) and self.tools:
|
| 224 |
+
if ENABLE_DETAILED_LOGGING:
|
| 225 |
+
logger.info(f"=== FALLBACK TOOL CALLING ===")
|
| 226 |
+
logger.info(f"Detected need for search based on keywords")
|
| 227 |
+
|
| 228 |
+
# Manually trigger web search
|
| 229 |
+
import uuid
|
| 230 |
+
tool_calls_to_execute = [{
|
| 231 |
+
'name': 'web_search',
|
| 232 |
+
'args': {'query': message},
|
| 233 |
+
'id': str(uuid.uuid4())
|
| 234 |
+
}]
|
| 235 |
+
|
| 236 |
+
if tool_calls_to_execute:
|
| 237 |
if ENABLE_DETAILED_LOGGING:
|
| 238 |
logger.info(f"=== TOOL CALLS DETECTED ===")
|
| 239 |
+
logger.info(f"Number of tool calls: {len(tool_calls_to_execute)}")
|
| 240 |
|
| 241 |
# Add the LLM response to messages
|
| 242 |
messages.append(response)
|
| 243 |
|
| 244 |
# Execute tool calls
|
| 245 |
+
for tool_call in tool_calls_to_execute:
|
| 246 |
if ENABLE_DETAILED_LOGGING:
|
| 247 |
logger.info(f"Executing tool: {tool_call['name']} with args: {tool_call['args']}")
|
| 248 |
|