instructor
193 строки · 4.5 Кб
1import json2import pytest3from instructor.utils import (4classproperty,5extract_json_from_codeblock,6extract_json_from_stream,7extract_json_from_stream_async,8merge_consecutive_messages,9)
10
11
12def test_extract_json_from_codeblock():13example = """14Here is a response
15
16```json
17{
18"key": "value"
19}
20```
21"""
22result = extract_json_from_codeblock(example)23assert json.loads(result) == {"key": "value"}24
25
26def test_extract_json_from_codeblock_no_end():27example = """28Here is a response
29
30```json
31{
32"key": "value",
33"another_key": [{"key": {"key": "value"}}]
34}
35"""
36result = extract_json_from_codeblock(example)37assert json.loads(result) == {38"key": "value",39"another_key": [{"key": {"key": "value"}}],40}41
42
43def test_extract_json_from_codeblock_no_start():44example = """45Here is a response
46
47{
48"key": "value",
49"another_key": [{"key": {"key": "value"}}, {"key": "value"}]
50}
51"""
52result = extract_json_from_codeblock(example)53assert json.loads(result) == {54"key": "value",55"another_key": [{"key": {"key": "value"}}, {"key": "value"}],56}57
58
59def test_stream_json():60text = """here is the json for you!61
62```json
63, here
64{
65"key": "value",
66"another_key": [{"key": {"key": "value"}}]
67}
68```
69
70What do you think?
71"""
72
73def batch_strings(chunks, n=2):74batch = ""75for chunk in chunks:76for char in chunk:77batch += char78if len(batch) == n:79yield batch80batch = ""81if batch: # Yield any remaining characters in the last batch82yield batch83
84result = json.loads(85"".join(list(extract_json_from_stream(batch_strings(text, n=3))))86)87assert result == {"key": "value", "another_key": [{"key": {"key": "value"}}]}88
89
90@pytest.mark.asyncio91async def test_stream_json_async():92text = """here is the json for you!93
94```json
95, here
96{
97"key": "value",
98"another_key": [{"key": {"key": "value"}}, {"key": "value"}]
99}
100```
101
102What do you think?
103"""
104
105async def batch_strings_async(chunks, n=2):106batch = ""107for chunk in chunks:108for char in chunk:109batch += char110if len(batch) == n:111yield batch112batch = ""113if batch: # Yield any remaining characters in the last batch114yield batch115
116result = json.loads(117"".join(118[119chunk
120async for chunk in extract_json_from_stream_async(121batch_strings_async(text, n=3)122)123]124)125)126assert result == {127"key": "value",128"another_key": [{"key": {"key": "value"}}, {"key": "value"}],129}130
131
132def test_merge_consecutive_messages():133messages = [134{"role": "user", "content": "Hello"},135{"role": "user", "content": "How are you"},136{"role": "assistant", "content": "Hello"},137{"role": "assistant", "content": "I am good"},138]139result = merge_consecutive_messages(messages)140assert result == [141{142"role": "user",143"content": [144{"type": "text", "text": "Hello"},145{"type": "text", "text": "How are you"},146],147},148{149"role": "assistant",150"content": [151{"type": "text", "text": "Hello"},152{"type": "text", "text": "I am good"},153],154},155]156
157
158def test_merge_consecutive_messages_empty():159messages = []160result = merge_consecutive_messages(messages)161assert result == []162
163
164def test_merge_consecutive_messages_single():165messages = [166{"role": "user", "content": "Hello"},167{"role": "assistant", "content": "Hello"},168]169result = merge_consecutive_messages(messages)170assert result == [171{"role": "user", "content": [{"type": "text", "text": "Hello"}]},172{"role": "assistant", "content": [{"type": "text", "text": "Hello"}]},173]174
175
176def test_classproperty():177"""Test custom `classproperty` descriptor."""178
179class MyClass:180@classproperty181def my_property(cls):182return cls183
184assert MyClass.my_property is MyClass185
186class MyClass:187clvar = 1188
189@classproperty190def my_property(cls):191return cls.clvar192
193assert MyClass.my_property == 1194