-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_chatbot_tools.py
More file actions
97 lines (71 loc) · 3.3 KB
/
test_chatbot_tools.py
File metadata and controls
97 lines (71 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
from unittest.mock import MagicMock
from chatbot import HuggingFaceChatbot
def mock_get_flood_probability(site_code=None, lat=None, lon=None):
print(f"Tool called with: site_code={site_code}, lat={lat}, lon={lon}")
return "The flood probability is 45% (Moderate Risk)."
def test_tool_calling():
# Mock tools
tools = {
"get_flood_probability": mock_get_flood_probability
}
# Initialize bot with a dummy token so it doesn't complain
bot = HuggingFaceChatbot(api_token="dummy_token", tools=tools)
# Mock the client
bot.client = MagicMock()
# --- Test 1: Tool Call ---
print("--- Test 1: Simulate Tool Call ---")
# Mock the first response (Tool Call)
mock_tool_call = MagicMock()
mock_tool_call.id = "call_123"
mock_tool_call.function.name = "get_flood_probability"
mock_tool_call.function.arguments = '{"site_code": "03432400"}'
mock_message_tool = MagicMock()
mock_message_tool.tool_calls = [mock_tool_call]
mock_message_tool.content = None # Content is usually null when tool_calls are present
mock_choice_tool = MagicMock()
mock_choice_tool.message = mock_message_tool
mock_completion_tool = MagicMock()
mock_completion_tool.choices = [mock_choice_tool]
# Mock the second response (Final Answer)
mock_message_final = MagicMock()
mock_message_final.content = "The flood probability for site 03432400 is 45% (Moderate Risk)."
mock_message_final.tool_calls = None
mock_choice_final = MagicMock()
mock_choice_final.message = mock_message_final
mock_completion_final = MagicMock()
mock_completion_final.choices = [mock_choice_final]
# Set side_effect to return tool call first, then final answer
bot.client.chat.completions.create.side_effect = [mock_completion_tool, mock_completion_final]
response = bot.get_response("What is the flood probability for site 03432400?")
print(f"Final Response: {response}\n")
# Verify tool was called
# We can't easily assert here without a proper test framework, but the print in mock_get_flood_probability will show up.
# Verify client calls
assert bot.client.chat.completions.create.call_count == 2
# Check first call args (should have tools)
first_call_args = bot.client.chat.completions.create.call_args_list[0]
assert "tools" in first_call_args.kwargs
assert first_call_args.kwargs["tool_choice"] == "auto"
print("Test passed!")
def test_think_tag_filtering():
print("--- Test 2: Filter <think> Tags ---")
bot = HuggingFaceChatbot(api_token="dummy_token")
bot.client = MagicMock()
# Mock response with <think> tags
mock_message = MagicMock()
mock_message.content = "<think>This is internal thought.</think>Hello, user!"
mock_message.tool_calls = None
mock_choice = MagicMock()
mock_choice.message = mock_message
mock_completion = MagicMock()
mock_completion.choices = [mock_choice]
bot.client.chat.completions.create.return_value = mock_completion
response = bot.get_response("Hi")
print(f"Response: '{response}'")
assert response == "Hello, user!"
print("Test passed!")
if __name__ == "__main__":
test_tool_calling()
print("\n")
test_think_tag_filtering()