Skip to content

Commit a3dc256

Browse files
committed
Add build_chat_context unit tests
1 parent 6a4eafd commit a3dc256

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

web-apps/chat/test.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,62 @@
11
import os
22
import unittest
3+
from unittest.mock import patch
34

45
# from unittest import mock
56
from gradio_client import Client
7+
from app import build_chat_context, SystemMessage, HumanMessage, AIMessage
68

79
url = os.environ.get("GRADIO_URL", "http://localhost:7860")
810
client = Client(url)
911

1012
class TestSuite(unittest.TestCase):
11-
13+
# General tests
1214
def test_gradio_api(self):
1315
result = client.predict("Hi", api_name="/chat")
1416
self.assertGreater(len(result), 0)
1517

16-
# def test_mock_response(self):
17-
# with mock.patch('app.client.stream_response', return_value=(char for char in "Mocked")) as mock_response:
18-
# result = client.predict("Hi", api_name="/chat")
19-
# # mock_response.assert_called_once_with("Hi", [])
20-
# self.assertEqual(result, "Mocked")
18+
# build_chat_context function tests
19+
@patch("app.settings")
20+
@patch("app.INCLUDE_SYSTEM_PROMPT", True)
21+
def test_chat_context_system_prompt(self, mock_settings):
22+
mock_settings.model_instruction = "You are a helpful assistant."
23+
latest_message = "What is a mammal?"
24+
history = [
25+
{'role': 'user', 'metadata': None, 'content': 'Hi!', 'options': None},
26+
{"role": "assistant", 'metadata': None, "content": "Hello! How can I help you?", 'options': None},
27+
]
28+
29+
context = build_chat_context(latest_message, history)
30+
31+
self.assertEqual(len(context), 4)
32+
self.assertIsInstance(context[0], SystemMessage)
33+
self.assertEqual(context[0].content, "You are a helpful assistant.")
34+
self.assertIsInstance(context[1], HumanMessage)
35+
self.assertEqual(context[1].content, "Hi!")
36+
self.assertIsInstance(context[2], AIMessage)
37+
self.assertEqual(context[2].content, "Hello! How can I help you?")
38+
self.assertIsInstance(context[3], HumanMessage)
39+
self.assertEqual(context[3].content, latest_message)
40+
41+
@patch("app.settings")
42+
@patch("app.INCLUDE_SYSTEM_PROMPT", False)
43+
def test_chat_context_human_prompt(self, mock_settings):
44+
mock_settings.model_instruction = "You are a very helpful assistant."
45+
latest_message = "What is a fish?"
46+
history = [
47+
{"role": "user", 'metadata': None, "content": "Hi there!", 'options': None},
48+
{"role": "assistant", 'metadata': None, "content": "Hi! How can I help you?", 'options': None},
49+
]
50+
51+
context = build_chat_context(latest_message, history)
52+
53+
self.assertEqual(len(context), 3)
54+
self.assertIsInstance(context[0], HumanMessage)
55+
self.assertEqual(context[0].content, "You are a very helpful assistant.\n\nHi there!")
56+
self.assertIsInstance(context[1], AIMessage)
57+
self.assertEqual(context[1].content, "Hi! How can I help you?")
58+
self.assertIsInstance(context[2], HumanMessage)
59+
self.assertEqual(context[2].content, latest_message)
2160

2261
if __name__ == "__main__":
2362
unittest.main()

0 commit comments

Comments
 (0)