-
Notifications
You must be signed in to change notification settings - Fork 16.1k
/
Copy pathtest_chat_models.py
242 lines (212 loc) Β· 7.38 KB
/
test_chat_models.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from typing import Any, Dict, List # type: ignore[import-not-found]
from unittest.mock import MagicMock, Mock, patch
import pytest # type: ignore[import-not-found]
from langchain_core.messages import (
AIMessage,
BaseMessage,
ChatMessage,
HumanMessage,
SystemMessage,
)
from langchain_core.outputs import ChatResult
from langchain_core.tools import BaseTool
from langchain_huggingface.chat_models import ( # type: ignore[import]
TGI_MESSAGE,
ChatHuggingFace,
_convert_message_to_chat_message,
_convert_TGI_message_to_LC_message,
)
from langchain_huggingface.llms.huggingface_endpoint import (
HuggingFaceEndpoint,
)
@pytest.mark.parametrize(
("message", "expected"),
[
(
SystemMessage(content="Hello"),
dict(role="system", content="Hello"),
),
(
HumanMessage(content="Hello"),
dict(role="user", content="Hello"),
),
(
AIMessage(content="Hello"),
dict(role="assistant", content="Hello", tool_calls=None),
),
(
ChatMessage(role="assistant", content="Hello"),
dict(role="assistant", content="Hello"),
),
],
)
def test_convert_message_to_chat_message(
message: BaseMessage, expected: Dict[str, str]
) -> None:
result = _convert_message_to_chat_message(message)
assert result == expected
@pytest.mark.parametrize(
("tgi_message", "expected"),
[
(
TGI_MESSAGE(role="assistant", content="Hello", tool_calls=[]),
AIMessage(content="Hello"),
),
(
TGI_MESSAGE(role="assistant", content="", tool_calls=[]),
AIMessage(content=""),
),
(
TGI_MESSAGE(
role="assistant",
content="",
tool_calls=[{"function": {"arguments": "'function string'"}}],
),
AIMessage(
content="",
additional_kwargs={
"tool_calls": [{"function": {"arguments": '"function string"'}}]
},
),
),
],
)
def test_convert_TGI_message_to_LC_message(
tgi_message: TGI_MESSAGE, expected: BaseMessage
) -> None:
result = _convert_TGI_message_to_LC_message(tgi_message)
assert result == expected
@pytest.fixture
def mock_llm() -> Mock:
llm = Mock(spec=HuggingFaceEndpoint)
llm.inference_server_url = "test endpoint url"
return llm
@pytest.fixture
@patch(
"langchain_huggingface.chat_models.huggingface.ChatHuggingFace._resolve_model_id"
)
def chat_hugging_face(mock_resolve_id: Any, mock_llm: Any) -> ChatHuggingFace:
chat_hf = ChatHuggingFace(llm=mock_llm, tokenizer=MagicMock())
return chat_hf
def test_create_chat_result(chat_hugging_face: Any) -> None:
mock_response = MagicMock()
mock_response.choices = [
MagicMock(
message=TGI_MESSAGE(
role="assistant", content="test message", tool_calls=[]
),
finish_reason="test finish reason",
)
]
mock_response.usage = {"tokens": 420}
result = chat_hugging_face._create_chat_result(mock_response)
assert isinstance(result, ChatResult)
assert result.generations[0].message.content == "test message"
assert (
result.generations[0].generation_info["finish_reason"] == "test finish reason" # type: ignore[index]
)
assert result.llm_output["token_usage"]["tokens"] == 420 # type: ignore[index]
assert result.llm_output["model"] == chat_hugging_face.llm.inference_server_url # type: ignore[index]
@pytest.mark.parametrize(
"messages, expected_error",
[
([], "At least one HumanMessage must be provided!"),
(
[HumanMessage(content="Hi"), AIMessage(content="Hello")],
"Last message must be a HumanMessage!",
),
],
)
def test_to_chat_prompt_errors(
chat_hugging_face: Any, messages: List[BaseMessage], expected_error: str
) -> None:
with pytest.raises(ValueError) as e:
chat_hugging_face._to_chat_prompt(messages)
assert expected_error in str(e.value)
def test_to_chat_prompt_valid_messages(chat_hugging_face: Any) -> None:
messages = [AIMessage(content="Hello"), HumanMessage(content="How are you?")]
expected_prompt = "Generated chat prompt"
chat_hugging_face.tokenizer.apply_chat_template.return_value = expected_prompt
result = chat_hugging_face._to_chat_prompt(messages)
assert result == expected_prompt
chat_hugging_face.tokenizer.apply_chat_template.assert_called_once_with(
[
{"role": "assistant", "content": "Hello"},
{"role": "user", "content": "How are you?"},
],
tokenize=False,
add_generation_prompt=True,
)
@pytest.mark.parametrize(
("message", "expected"),
[
(
SystemMessage(content="You are a helpful assistant."),
{"role": "system", "content": "You are a helpful assistant."},
),
(
AIMessage(content="How can I help you?"),
{"role": "assistant", "content": "How can I help you?"},
),
(
HumanMessage(content="Hello"),
{"role": "user", "content": "Hello"},
),
],
)
def test_to_chatml_format(
chat_hugging_face: Any, message: BaseMessage, expected: Dict[str, str]
) -> None:
result = chat_hugging_face._to_chatml_format(message)
assert result == expected
def test_to_chatml_format_with_invalid_type(chat_hugging_face: Any) -> None:
message = "Invalid message type"
with pytest.raises(ValueError) as e:
chat_hugging_face._to_chatml_format(message)
assert "Unknown message type:" in str(e.value)
def tool_mock() -> Dict:
return {"function": {"name": "test_tool"}}
@pytest.mark.parametrize(
"tools, tool_choice, expected_exception, expected_message",
[
([tool_mock()], ["invalid type"], ValueError, "Unrecognized tool_choice type."),
(
[tool_mock(), tool_mock()],
"test_tool",
ValueError,
"must provide exactly one tool.",
),
(
[tool_mock()],
{"type": "function", "function": {"name": "other_tool"}},
ValueError,
"Tool choice {'type': 'function', 'function': {'name': 'other_tool'}} "
"was specified, but the only provided tool was test_tool.",
),
],
)
def test_bind_tools_errors(
chat_hugging_face: Any,
tools: Dict[str, str],
tool_choice: Any,
expected_exception: Any,
expected_message: str,
) -> None:
with patch(
"langchain_huggingface.chat_models.huggingface.convert_to_openai_tool",
side_effect=lambda x: x,
):
with pytest.raises(expected_exception) as excinfo:
chat_hugging_face.bind_tools(tools, tool_choice=tool_choice)
assert expected_message in str(excinfo.value)
def test_bind_tools(chat_hugging_face: Any) -> None:
tools = [MagicMock(spec=BaseTool)]
with patch(
"langchain_huggingface.chat_models.huggingface.convert_to_openai_tool",
side_effect=lambda x: x,
), patch("langchain_core.runnables.base.Runnable.bind") as mock_super_bind:
chat_hugging_face.bind_tools(tools, tool_choice="auto")
mock_super_bind.assert_called_once()
_, kwargs = mock_super_bind.call_args
assert kwargs["tools"] == tools
assert kwargs["tool_choice"] == "auto"