-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
192 lines (159 loc) · 7.51 KB
/
test.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import openai
import os
import unittest
from gradio_client import Client
from unittest.mock import patch, MagicMock, Mock
from langchain.schema import HumanMessage, AIMessage, SystemMessage
from app import build_chat_context, inference, PossibleSystemPromptException, gr
url = os.environ.get("GRADIO_URL", "http://localhost:7860")
client = Client(url)
latest_message = "Why don't humans drink horse milk?"
history = [
{
"role": "user",
"metadata": None,
"content": "Hi!",
"options": None,
},
{
"role": "assistant",
"metadata": None,
"content": "Hello! How can I help you?",
"options": None,
},
]
class TestAPI(unittest.TestCase):
def test_gradio_api(self):
result = client.predict("Hi", api_name="/chat")
self.assertGreater(len(result), 0)
class TestBuildChatContext(unittest.TestCase):
@patch("app.settings")
@patch("app.INCLUDE_SYSTEM_PROMPT", True)
def test_chat_context_system_prompt(self, mock_settings):
mock_settings.model_instruction = "You are a helpful assistant."
context = build_chat_context(latest_message, history)
self.assertEqual(len(context), 4)
self.assertIsInstance(context[0], SystemMessage)
self.assertEqual(context[0].content, "You are a helpful assistant.")
self.assertIsInstance(context[1], HumanMessage)
self.assertEqual(context[1].content, history[0]["content"])
self.assertIsInstance(context[2], AIMessage)
self.assertEqual(context[2].content, history[1]["content"])
self.assertIsInstance(context[3], HumanMessage)
self.assertEqual(context[3].content, latest_message)
@patch("app.settings")
@patch("app.INCLUDE_SYSTEM_PROMPT", False)
def test_chat_context_human_prompt(self, mock_settings):
mock_settings.model_instruction = "You are a very helpful assistant."
context = build_chat_context(latest_message, history)
self.assertEqual(len(context), 3)
self.assertIsInstance(context[0], HumanMessage)
self.assertEqual(context[0].content, "You are a very helpful assistant.\n\nHi!")
self.assertIsInstance(context[1], AIMessage)
self.assertEqual(context[1].content, history[1]["content"])
self.assertIsInstance(context[2], HumanMessage)
self.assertEqual(context[2].content, latest_message)
class TestInference(unittest.TestCase):
@patch("app.settings")
@patch("app.llm")
@patch("app.log")
def test_inference_success(self, mock_logger, mock_llm, mock_settings):
mock_llm.stream.return_value = [MagicMock(content="response_chunk")]
mock_settings.model_instruction = "You are a very helpful assistant."
responses = list(inference(latest_message, history))
self.assertEqual(responses, ["response_chunk"])
mock_logger.debug.assert_any_call("Inference request received with history: %s", history)
@patch("app.llm")
@patch("app.build_chat_context")
def test_inference_thinking_tags(self, mock_build_chat_context, mock_llm):
mock_build_chat_context.return_value = ["mock_context"]
mock_llm.stream.return_value = [
MagicMock(content="<think>"),
MagicMock(content="processing"),
MagicMock(content="</think>"),
MagicMock(content="final response"),
]
responses = list(inference(latest_message, history))
self.assertEqual(responses, ["Thinking...", "Thinking...", "", "final response"])
@patch("app.llm")
@patch("app.INCLUDE_SYSTEM_PROMPT", True)
@patch("app.build_chat_context")
@patch("app.log")
def test_inference_PossibleSystemPromptException(self, mock_logger, mock_build_chat_context, mock_llm):
mock_build_chat_context.return_value = ["mock_context"]
mock_response = Mock()
mock_response.json.return_value = {"message": "Bad request"}
mock_llm.stream.side_effect = openai.BadRequestError(
message="Bad request",
response=mock_response,
body=None
)
with self.assertRaises(PossibleSystemPromptException):
list(inference(latest_message, history))
mock_logger.error.assert_called_once_with("Received BadRequestError from backend API: %s", mock_llm.stream.side_effect)
@patch("app.llm")
@patch("app.INCLUDE_SYSTEM_PROMPT", False)
@patch("app.build_chat_context")
@patch("app.log")
def test_inference_general_error(self, mock_logger, mock_build_chat_context, mock_llm):
mock_build_chat_context.return_value = ["mock_context"]
mock_response = Mock()
mock_response.json.return_value = {"message": "Bad request"}
mock_llm.stream.side_effect = openai.BadRequestError(
message="Bad request",
response=mock_response,
body=None
)
exception_message = "\'API Error received. This usually means the chosen LLM uses an incompatible prompt format. Error message was: Bad request\'"
with self.assertRaises(gr.Error) as gradio_error:
list(inference(latest_message, history))
self.assertEqual(str(gradio_error.exception), exception_message)
mock_logger.error.assert_called_once_with("Received BadRequestError from backend API: %s", mock_llm.stream.side_effect)
@patch("app.llm")
@patch("app.build_chat_context")
@patch("app.log")
@patch("app.gr")
@patch("app.BACKEND_INITIALISED", False)
def test_inference_APIConnectionError(self, mock_gr, mock_logger, mock_build_chat_context, mock_llm):
mock_build_chat_context.return_value = ["mock_context"]
mock_request = Mock()
mock_request.json.return_value = {"message": "Foo"}
mock_llm.stream.side_effect = openai.APIConnectionError(
message="Foo",
request=mock_request,
)
list(inference(latest_message, history))
mock_logger.info.assert_any_call("Backend API not yet ready")
mock_gr.Info.assert_any_call("Backend not ready - model may still be initialising - please try again later.")
@patch("app.llm")
@patch("app.build_chat_context")
@patch("app.log")
@patch("app.gr")
@patch("app.BACKEND_INITIALISED", True)
def test_inference_APIConnectionError_initialised(self, mock_gr, mock_logger, mock_build_chat_context, mock_llm):
mock_build_chat_context.return_value = ["mock_context"]
mock_request = Mock()
mock_request.json.return_value = {"message": "Foo"}
mock_llm.stream.side_effect = openai.APIConnectionError(
message="Foo",
request=mock_request,
)
list(inference(latest_message, history))
mock_logger.error.assert_called_once_with("Failed to connect to backend API: %s", mock_llm.stream.side_effect)
mock_gr.Warning.assert_any_call("Failed to connect to backend API.")
@patch("app.llm")
@patch("app.build_chat_context")
@patch("app.gr")
def test_inference_InternalServerError(self, mock_gr, mock_build_chat_context, mock_llm):
mock_build_chat_context.return_value = ["mock_context"]
mock_request = Mock()
mock_request.json.return_value = {"message": "Foo"}
mock_llm.stream.side_effect = openai.InternalServerError(
message="Foo",
response=mock_request,
body=None
)
list(inference(latest_message, history))
mock_gr.Warning.assert_any_call("Internal server error encountered in backend API - see API logs for details.")
if __name__ == "__main__":
unittest.main(verbosity=2)