-
Notifications
You must be signed in to change notification settings - Fork 15.8k
/
test_chat_models.py
407 lines (340 loc) Β· 13.5 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
243
244
245
246
247
248
249
250
251
252
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
"""Test ChatGroq chat model."""
import json
from typing import Any, Optional
import pytest
from langchain_core.messages import (
AIMessage,
AIMessageChunk,
BaseMessage,
BaseMessageChunk,
HumanMessage,
SystemMessage,
)
from langchain_core.outputs import ChatGeneration, LLMResult
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_groq import ChatGroq
from tests.unit_tests.fake.callbacks import (
FakeCallbackHandler,
FakeCallbackHandlerWithChatStart,
)
#
# Smoke test Runnable interface
#
@pytest.mark.scheduled
def test_invoke() -> None:
"""Test Chat wrapper."""
chat = ChatGroq( # type: ignore[call-arg]
temperature=0.7,
base_url=None,
groq_proxy=None,
timeout=10.0,
max_retries=3,
http_client=None,
n=1,
max_tokens=10,
default_headers=None,
default_query=None,
)
message = HumanMessage(content="Welcome to the Groqetship")
response = chat.invoke([message])
assert isinstance(response, BaseMessage)
assert isinstance(response.content, str)
@pytest.mark.scheduled
async def test_ainvoke() -> None:
"""Test ainvoke tokens from ChatGroq."""
chat = ChatGroq(max_tokens=10) # type: ignore[call-arg]
result = await chat.ainvoke("Welcome to the Groqetship!", config={"tags": ["foo"]})
assert isinstance(result, BaseMessage)
assert isinstance(result.content, str)
@pytest.mark.scheduled
def test_batch() -> None:
"""Test batch tokens from ChatGroq."""
chat = ChatGroq(max_tokens=10) # type: ignore[call-arg]
result = chat.batch(["Hello!", "Welcome to the Groqetship!"])
for token in result:
assert isinstance(token, BaseMessage)
assert isinstance(token.content, str)
@pytest.mark.scheduled
async def test_abatch() -> None:
"""Test abatch tokens from ChatGroq."""
chat = ChatGroq(max_tokens=10) # type: ignore[call-arg]
result = await chat.abatch(["Hello!", "Welcome to the Groqetship!"])
for token in result:
assert isinstance(token, BaseMessage)
assert isinstance(token.content, str)
@pytest.mark.scheduled
async def test_stream() -> None:
"""Test streaming tokens from Groq."""
chat = ChatGroq(max_tokens=10) # type: ignore[call-arg]
for token in chat.stream("Welcome to the Groqetship!"):
assert isinstance(token, BaseMessageChunk)
assert isinstance(token.content, str)
@pytest.mark.scheduled
async def test_astream() -> None:
"""Test streaming tokens from Groq."""
chat = ChatGroq(max_tokens=10) # type: ignore[call-arg]
full: Optional[BaseMessageChunk] = None
chunks_with_token_counts = 0
async for token in chat.astream("Welcome to the Groqetship!"):
assert isinstance(token, AIMessageChunk)
assert isinstance(token.content, str)
full = token if full is None else full + token
if token.usage_metadata is not None:
chunks_with_token_counts += 1
if chunks_with_token_counts != 1:
raise AssertionError(
"Expected exactly one chunk with token counts. "
"AIMessageChunk aggregation adds counts. Check that "
"this is behaving properly."
)
assert isinstance(full, AIMessageChunk)
assert full.usage_metadata is not None
assert full.usage_metadata["input_tokens"] > 0
assert full.usage_metadata["output_tokens"] > 0
assert (
full.usage_metadata["input_tokens"] + full.usage_metadata["output_tokens"]
== full.usage_metadata["total_tokens"]
)
#
# Test Legacy generate methods
#
@pytest.mark.scheduled
def test_generate() -> None:
"""Test sync generate."""
n = 1
chat = ChatGroq(max_tokens=10) # type: ignore[call-arg]
message = HumanMessage(content="Hello", n=1)
response = chat.generate([[message], [message]])
assert isinstance(response, LLMResult)
assert len(response.generations) == 2
assert response.llm_output
assert response.llm_output["model_name"] == chat.model_name
for generations in response.generations:
assert len(generations) == n
for generation in generations:
assert isinstance(generation, ChatGeneration)
assert isinstance(generation.text, str)
assert generation.text == generation.message.content
@pytest.mark.scheduled
async def test_agenerate() -> None:
"""Test async generation."""
n = 1
chat = ChatGroq(max_tokens=10, n=1) # type: ignore[call-arg]
message = HumanMessage(content="Hello")
response = await chat.agenerate([[message], [message]])
assert isinstance(response, LLMResult)
assert len(response.generations) == 2
assert response.llm_output
assert response.llm_output["model_name"] == chat.model_name
for generations in response.generations:
assert len(generations) == n
for generation in generations:
assert isinstance(generation, ChatGeneration)
assert isinstance(generation.text, str)
assert generation.text == generation.message.content
#
# Test streaming flags in invoke and generate
#
@pytest.mark.scheduled
def test_invoke_streaming() -> None:
"""Test that streaming correctly invokes on_llm_new_token callback."""
callback_handler = FakeCallbackHandler()
chat = ChatGroq( # type: ignore[call-arg]
max_tokens=2,
streaming=True,
temperature=0,
callbacks=[callback_handler],
)
message = HumanMessage(content="Welcome to the Groqetship")
response = chat.invoke([message])
assert callback_handler.llm_streams > 0
assert isinstance(response, BaseMessage)
@pytest.mark.scheduled
async def test_agenerate_streaming() -> None:
"""Test that streaming correctly invokes on_llm_new_token callback."""
callback_handler = FakeCallbackHandlerWithChatStart()
chat = ChatGroq( # type: ignore[call-arg]
max_tokens=10,
streaming=True,
temperature=0,
callbacks=[callback_handler],
)
message = HumanMessage(content="Welcome to the Groqetship")
response = await chat.agenerate([[message], [message]])
assert callback_handler.llm_streams > 0
assert isinstance(response, LLMResult)
assert len(response.generations) == 2
assert response.llm_output is not None
assert response.llm_output["model_name"] == chat.model_name
for generations in response.generations:
assert len(generations) == 1
for generation in generations:
assert isinstance(generation, ChatGeneration)
assert isinstance(generation.text, str)
assert generation.text == generation.message.content
#
# Misc tests
#
def test_streaming_generation_info() -> None:
"""Test that generation info is preserved when streaming."""
class _FakeCallback(FakeCallbackHandler):
saved_things: dict = {}
def on_llm_end(
self,
*args: Any,
**kwargs: Any,
) -> Any:
# Save the generation
self.saved_things["generation"] = args[0]
callback = _FakeCallback()
chat = ChatGroq( # type: ignore[call-arg]
max_tokens=2,
temperature=0,
callbacks=[callback],
)
list(chat.stream("Respond with the single word Hello", stop=["o"]))
generation = callback.saved_things["generation"]
# `Hello!` is two tokens, assert that that is what is returned
assert isinstance(generation, LLMResult)
assert generation.generations[0][0].text == "Hell"
def test_system_message() -> None:
"""Test ChatGroq wrapper with system message."""
chat = ChatGroq(max_tokens=10) # type: ignore[call-arg]
system_message = SystemMessage(content="You are to chat with the user.")
human_message = HumanMessage(content="Hello")
response = chat.invoke([system_message, human_message])
assert isinstance(response, BaseMessage)
assert isinstance(response.content, str)
@pytest.mark.xfail(reason="Groq tool_choice doesn't currently force a tool call")
def test_tool_choice() -> None:
"""Test that tool choice is respected."""
llm = ChatGroq() # type: ignore[call-arg]
class MyTool(BaseModel):
name: str
age: int
with_tool = llm.bind_tools([MyTool], tool_choice="MyTool")
resp = with_tool.invoke("Who was the 27 year old named Erick?")
assert isinstance(resp, AIMessage)
assert resp.content == "" # should just be tool call
tool_calls = resp.additional_kwargs["tool_calls"]
assert len(tool_calls) == 1
tool_call = tool_calls[0]
assert tool_call["function"]["name"] == "MyTool"
assert json.loads(tool_call["function"]["arguments"]) == {
"age": 27,
"name": "Erick",
}
assert tool_call["type"] == "function"
assert isinstance(resp.tool_calls, list)
assert len(resp.tool_calls) == 1
tool_call = resp.tool_calls[0]
assert tool_call["name"] == "MyTool"
assert tool_call["args"] == {"name": "Erick", "age": 27}
@pytest.mark.xfail(reason="Groq tool_choice doesn't currently force a tool call")
def test_tool_choice_bool() -> None:
"""Test that tool choice is respected just passing in True."""
llm = ChatGroq() # type: ignore[call-arg]
class MyTool(BaseModel):
name: str
age: int
with_tool = llm.bind_tools([MyTool], tool_choice=True)
resp = with_tool.invoke("Who was the 27 year old named Erick?")
assert isinstance(resp, AIMessage)
assert resp.content == "" # should just be tool call
tool_calls = resp.additional_kwargs["tool_calls"]
assert len(tool_calls) == 1
tool_call = tool_calls[0]
assert tool_call["function"]["name"] == "MyTool"
assert json.loads(tool_call["function"]["arguments"]) == {
"age": 27,
"name": "Erick",
}
assert tool_call["type"] == "function"
@pytest.mark.xfail(reason="Groq tool_choice doesn't currently force a tool call")
def test_streaming_tool_call() -> None:
"""Test that tool choice is respected."""
llm = ChatGroq() # type: ignore[call-arg]
class MyTool(BaseModel):
name: str
age: int
with_tool = llm.bind_tools([MyTool], tool_choice="MyTool")
resp = with_tool.stream("Who was the 27 year old named Erick?")
additional_kwargs = None
for chunk in resp:
assert isinstance(chunk, AIMessageChunk)
assert chunk.content == "" # should just be tool call
additional_kwargs = chunk.additional_kwargs
assert additional_kwargs is not None
tool_calls = additional_kwargs["tool_calls"]
assert len(tool_calls) == 1
tool_call = tool_calls[0]
assert tool_call["function"]["name"] == "MyTool"
assert json.loads(tool_call["function"]["arguments"]) == {
"age": 27,
"name": "Erick",
}
assert tool_call["type"] == "function"
assert isinstance(chunk, AIMessageChunk)
assert isinstance(chunk.tool_call_chunks, list)
assert len(chunk.tool_call_chunks) == 1
tool_call_chunk = chunk.tool_call_chunks[0]
assert tool_call_chunk["name"] == "MyTool"
assert isinstance(tool_call_chunk["args"], str)
assert json.loads(tool_call_chunk["args"]) == {"name": "Erick", "age": 27}
@pytest.mark.xfail(reason="Groq tool_choice doesn't currently force a tool call")
async def test_astreaming_tool_call() -> None:
"""Test that tool choice is respected."""
llm = ChatGroq() # type: ignore[call-arg]
class MyTool(BaseModel):
name: str
age: int
with_tool = llm.bind_tools([MyTool], tool_choice="MyTool")
resp = with_tool.astream("Who was the 27 year old named Erick?")
additional_kwargs = None
async for chunk in resp:
assert isinstance(chunk, AIMessageChunk)
assert chunk.content == "" # should just be tool call
additional_kwargs = chunk.additional_kwargs
assert additional_kwargs is not None
tool_calls = additional_kwargs["tool_calls"]
assert len(tool_calls) == 1
tool_call = tool_calls[0]
assert tool_call["function"]["name"] == "MyTool"
assert json.loads(tool_call["function"]["arguments"]) == {
"age": 27,
"name": "Erick",
}
assert tool_call["type"] == "function"
assert isinstance(chunk, AIMessageChunk)
assert isinstance(chunk.tool_call_chunks, list)
assert len(chunk.tool_call_chunks) == 1
tool_call_chunk = chunk.tool_call_chunks[0]
assert tool_call_chunk["name"] == "MyTool"
assert isinstance(tool_call_chunk["args"], str)
assert json.loads(tool_call_chunk["args"]) == {"name": "Erick", "age": 27}
@pytest.mark.scheduled
def test_json_mode_structured_output() -> None:
"""Test with_structured_output with json"""
class Joke(BaseModel):
"""Joke to tell user."""
setup: str = Field(description="question to set up a joke")
punchline: str = Field(description="answer to resolve the joke")
chat = ChatGroq().with_structured_output(Joke, method="json_mode") # type: ignore[call-arg]
result = chat.invoke(
"Tell me a joke about cats, respond in JSON with `setup` and `punchline` keys"
)
assert type(result) == Joke
assert len(result.setup) != 0
assert len(result.punchline) != 0
# Groq does not currently support N > 1
# @pytest.mark.scheduled
# def test_chat_multiple_completions() -> None:
# """Test ChatGroq wrapper with multiple completions."""
# chat = ChatGroq(max_tokens=10, n=5)
# message = HumanMessage(content="Hello")
# response = chat._generate([message])
# assert isinstance(response, ChatResult)
# assert len(response.generations) == 5
# for generation in response.generations:
# assert isinstance(generation.message, BaseMessage)
# assert isinstance(generation.message.content, str)