-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathtest_chat_model_on_cpu.py
More file actions
70 lines (53 loc) · 2.2 KB
/
Copy pathtest_chat_model_on_cpu.py
File metadata and controls
70 lines (53 loc) · 2.2 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
# Copyright 2024 Bytedance Ltd. and/or its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
from recipe.langgraph_agent.chat_model import ChatModel
from transformers import BatchEncoding
class _BatchEncodingTokenizer:
def apply_chat_template(self, *args, **kwargs):
del args, kwargs
return BatchEncoding({"input_ids": [101, 102]})
def _make_chat_model() -> ChatModel:
return ChatModel.model_construct(
model_name="dummy-model",
client=None,
tokenizer=_BatchEncodingTokenizer(),
max_tokens=16,
)
@pytest.mark.asyncio
async def test_preprocess_normalizes_batch_encoding_to_token_ids():
model = _make_chat_model()
_, prompt_ids, response_mask = await model._preprocess([HumanMessage(content="hello")])
assert prompt_ids == [101, 102]
assert response_mask == []
@pytest.mark.asyncio
async def test_preprocess_normalizes_batch_encoding_for_tool_response():
model = _make_chat_model()
messages = [
HumanMessage(content="hello"),
AIMessage(
content="",
response_metadata={"request_id": "request-1", "prompt_ids": [1, 2], "response_mask": [1]},
),
ToolMessage(content="tool result", tool_call_id="tool-call-1"),
]
request_id, prompt_ids, response_mask = await model._preprocess(messages, system_prompt=[101])
assert request_id == "request-1"
assert prompt_ids == [1, 2, 102]
assert response_mask == [1, 0]
def test_bind_tools_normalizes_system_prompt_to_token_ids():
model = _make_chat_model()
bound = model.bind_tools([])
assert bound.kwargs["system_prompt"] == [101, 102]