test_tools_llama.py 9.88 KB
Newer Older
jixx's avatar
init  
jixx committed
1
2
3
4
5
6
import pytest


@pytest.fixture(scope="module")
def flash_llama_grammar_tools_handle(launcher):
    with launcher(
jixx's avatar
jixx committed
7
8
9
        "meta-llama/Meta-Llama-3.1-8B-Instruct",
        num_shard=2,
        disable_grammar_support=False,
jixx's avatar
init  
jixx committed
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
    ) as handle:
        yield handle


@pytest.fixture(scope="module")
async def flash_llama_grammar_tools(flash_llama_grammar_tools_handle):
    await flash_llama_grammar_tools_handle.health(300)
    return flash_llama_grammar_tools_handle.client


# tools to be used in the following tests
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "Get the current weather",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA",
                    },
                    "format": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": "The temperature unit to use. Infer this from the users location.",
                    },
                },
                "required": ["location", "format"],
jixx's avatar
jixx committed
41
                "additionalProperties": False,
jixx's avatar
init  
jixx committed
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
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "get_n_day_weather_forecast",
            "description": "Get an N-day weather forecast",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA",
                    },
                    "format": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": "The temperature unit to use. Infer this from the users location.",
                    },
                    "num_days": {
                        "type": "integer",
                        "description": "The number of days to forecast",
                    },
                },
                "required": ["location", "format", "num_days"],
jixx's avatar
jixx committed
68
                "additionalProperties": False,
jixx's avatar
init  
jixx committed
69
70
71
72
73
74
75
76
77
78
79
80
81
            },
        },
    },
]


@pytest.mark.asyncio
@pytest.mark.private
async def test_flash_llama_grammar_tools(flash_llama_grammar_tools, response_snapshot):
    response = await flash_llama_grammar_tools.chat(
        max_tokens=100,
        seed=1,
        tools=tools,
jixx's avatar
jixx committed
82
        temperature=0.0,
jixx's avatar
init  
jixx committed
83
84
85
86
87
88
89
90
91
92
93
        messages=[
            {
                "role": "system",
                "content": "Youre a helpful assistant! Answer the users question best you can.",
            },
            {
                "role": "user",
                "content": "What is the weather like in Brooklyn, New York?",
            },
        ],
    )
jixx's avatar
jixx committed
94
    assert response.choices[0].message.content is None
jixx's avatar
init  
jixx committed
95
96
    assert response.choices[0].message.tool_calls == [
        {
jixx's avatar
jixx committed
97
            "id": "0",
jixx's avatar
init  
jixx committed
98
99
100
101
            "type": "function",
            "function": {
                "description": None,
                "name": "get_current_weather",
jixx's avatar
jixx committed
102
                "arguments": {"format": "celsius", "location": "Brooklyn, NY"},
jixx's avatar
init  
jixx committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
            },
        }
    ]
    assert response == response_snapshot


@pytest.mark.asyncio
@pytest.mark.private
async def test_flash_llama_grammar_tools_auto(
    flash_llama_grammar_tools, response_snapshot
):
    response = await flash_llama_grammar_tools.chat(
        max_tokens=100,
        seed=1,
        tools=tools,
jixx's avatar
jixx committed
118
        temperature=0.0,
jixx's avatar
init  
jixx committed
119
120
121
122
123
124
125
126
127
128
129
130
        tool_choice="auto",
        messages=[
            {
                "role": "system",
                "content": "Youre a helpful assistant! Answer the users question best you can.",
            },
            {
                "role": "user",
                "content": "What is the weather like in Brooklyn, New York?",
            },
        ],
    )
jixx's avatar
jixx committed
131
    assert response.choices[0].message.content is None
jixx's avatar
init  
jixx committed
132
133
    assert response.choices[0].message.tool_calls == [
        {
jixx's avatar
jixx committed
134
            "id": "0",
jixx's avatar
init  
jixx committed
135
136
137
138
            "type": "function",
            "function": {
                "description": None,
                "name": "get_current_weather",
jixx's avatar
jixx committed
139
                "arguments": {"format": "celsius", "location": "Brooklyn, NY"},
jixx's avatar
init  
jixx committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
            },
        }
    ]

    assert response == response_snapshot


@pytest.mark.asyncio
@pytest.mark.private
async def test_flash_llama_grammar_tools_choice(
    flash_llama_grammar_tools, response_snapshot
):
    response = await flash_llama_grammar_tools.chat(
        max_tokens=100,
        seed=1,
        tools=tools,
jixx's avatar
jixx committed
156
        temperature=0.0,
jixx's avatar
init  
jixx committed
157
158
159
160
161
162
163
164
165
166
167
168
        tool_choice="get_current_weather",
        messages=[
            {
                "role": "system",
                "content": "Youre a helpful assistant! Answer the users question best you can.",
            },
            {
                "role": "user",
                "content": "What is the weather like in Brooklyn, New York?",
            },
        ],
    )
jixx's avatar
jixx committed
169
    assert response.choices[0].message.content is None
jixx's avatar
init  
jixx committed
170
171
    assert response.choices[0].message.tool_calls == [
        {
jixx's avatar
jixx committed
172
            "id": "0",
jixx's avatar
init  
jixx committed
173
174
175
176
            "type": "function",
            "function": {
                "description": None,
                "name": "get_current_weather",
jixx's avatar
jixx committed
177
                "arguments": {"format": "celsius", "location": "Brooklyn, NY"},
jixx's avatar
init  
jixx committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
            },
        }
    ]

    assert response == response_snapshot


@pytest.mark.asyncio
@pytest.mark.private
async def test_flash_llama_grammar_tools_stream(
    flash_llama_grammar_tools, response_snapshot
):
    responses = await flash_llama_grammar_tools.chat(
        max_tokens=100,
        seed=1,
        tools=tools,
jixx's avatar
jixx committed
194
        temperature=0.0,
jixx's avatar
init  
jixx committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
        tool_choice="get_current_weather",
        messages=[
            {
                "role": "system",
                "content": "Youre a helpful assistant! Answer the users question best you can.",
            },
            {
                "role": "user",
                "content": "What is the weather like in Paris, France?",
            },
        ],
        stream=True,
    )

    count = 0
jixx's avatar
jixx committed
210
211
    tool_calls_generated = ""
    last_response = None
jixx's avatar
init  
jixx committed
212
213
    async for response in responses:
        count += 1
jixx's avatar
jixx committed
214
215
216
        tool_calls_generated += response.choices[0].delta.tool_calls.function.arguments
        last_response = response
        assert response.choices[0].delta.content is None
jixx's avatar
init  
jixx committed
217

jixx's avatar
jixx committed
218
219
220
221
222
223
    assert (
        tool_calls_generated
        == '{"function": {"_name": "get_current_weather", "format": "celsius", "location": "Paris, France"}}<|eot_id|>'
    )
    assert count == 28
    assert last_response == response_snapshot
jixx's avatar
init  
jixx committed
224
225
226
227
228
229
230
231
232


@pytest.mark.asyncio
@pytest.mark.private
async def test_flash_llama_grammar_tools_insufficient_information(
    flash_llama_grammar_tools, response_snapshot
):
    responses = await flash_llama_grammar_tools.chat(
        max_tokens=100,
jixx's avatar
jixx committed
233
        seed=24,
jixx's avatar
init  
jixx committed
234
235
236
237
238
        tools=tools,
        tool_choice="auto",
        messages=[
            {
                "role": "system",
jixx's avatar
jixx committed
239
                "content": "You're a helpful assistant! Answer the users question best you can.",
jixx's avatar
init  
jixx committed
240
241
242
            },
            {
                "role": "user",
jixx's avatar
jixx committed
243
                "content": "Who are you?",
jixx's avatar
init  
jixx committed
244
245
246
247
248
            },
        ],
        stream=False,
    )

jixx's avatar
jixx committed
249
250
    assert responses.choices[0].message.tool_calls is None
    assert responses.choices[0].message.content == "I am an AI assistant"
jixx's avatar
init  
jixx committed
251
252

    assert responses == response_snapshot
jixx's avatar
jixx committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329


@pytest.mark.asyncio
@pytest.mark.private
async def test_flash_llama_grammar_tools_insufficient_information_stream(
    flash_llama_grammar_tools, response_snapshot
):
    responses = await flash_llama_grammar_tools.chat(
        max_tokens=100,
        seed=24,
        tools=tools,
        tool_choice="auto",
        messages=[
            {
                "role": "system",
                "content": "You're a helpful assistant! Answer the users question best you can.",
            },
            {
                "role": "user",
                "content": "Who are you?",
            },
        ],
        stream=True,
    )

    count = 0
    content_generated = ""
    last_response = None
    async for response in responses:
        count += 1
        content_generated += response.choices[0].delta.content
        last_response = response
        assert response.choices[0].delta.tool_calls is None

    assert count == 5
    assert content_generated == "I am an AI assistant"
    assert last_response == response_snapshot


@pytest.mark.asyncio
@pytest.mark.private
async def test_flash_llama_grammar_tools_sea_creatures_stream(
    flash_llama_grammar_tools, response_snapshot
):
    responses = await flash_llama_grammar_tools.chat(
        max_tokens=100,
        seed=24,
        tools=tools,
        tool_choice="auto",
        messages=[
            {
                "role": "system",
                "content": "You're a helpful assistant! Answer the users question best you can. If the question is not answerable by the tools, just generate a response.",
            },
            {
                "role": "user",
                "content": "Tell me a story about 3 sea creatures",
            },
        ],
        stream=True,
    )

    count = 0
    content_generated = ""
    last_response = None
    async for response in responses:
        count += 1
        content_generated += response.choices[0].delta.content
        last_response = response
        assert response.choices[0].delta.tool_calls is None

    assert count == 62
    assert (
        content_generated
        == "Once upon a time, in the ocean, there lived three sea creatures. There was a wise old octopus named Bob, a mischievous seagull named Sam, and a gentle sea turtle named Luna. They all lived together in a beautiful coral reef, surrounded by colorful fish and swaying sea fans"
    )
    assert last_response == response_snapshot