-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathtest_async_client.py
More file actions
353 lines (284 loc) · 12.3 KB
/
test_async_client.py
File metadata and controls
353 lines (284 loc) · 12.3 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
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
import os
import unittest
import httpx
import cohere
from cohere import (
ChatbotMessage,
Tool,
ToolParameterDefinitionsValue,
ToolResult,
UserMessage,
)
package_dir = os.path.dirname(os.path.abspath(__file__))
embed_job = os.path.join(package_dir, 'embed_job.jsonl')
class TestClient(unittest.IsolatedAsyncioTestCase):
co: cohere.AsyncClient
def setUp(self) -> None:
self.co = cohere.AsyncClient(timeout=10000)
async def test_token_falls_back_on_env_variable(self) -> None:
cohere.AsyncClient(api_key=None)
cohere.AsyncClient(None)
async def test_context_manager(self) -> None:
async with cohere.AsyncClient(api_key="xxx") as client:
self.assertIsNotNone(client)
async def test_custom_httpx_async_client_is_used_verbatim(self) -> None:
"""A plain httpx.AsyncClient must be passed through, not replaced by HttpxAiohttpClient."""
custom = httpx.AsyncClient(timeout=30.0)
try:
client = cohere.AsyncClient(api_key="xxx", httpx_client=custom)
self.assertIs(client._client_wrapper.httpx_client.httpx_client, custom)
finally:
await custom.aclose()
async def test_async_client_v2_custom_httpx_async_client(self) -> None:
custom = httpx.AsyncClient(timeout=30.0)
try:
client = cohere.AsyncClientV2(api_key="xxx", httpx_client=custom)
self.assertIs(client._client_wrapper.httpx_client.httpx_client, custom)
finally:
await custom.aclose()
async def test_chat(self) -> None:
chat = await self.co.chat(
model="command-a-03-2025",
chat_history=[
UserMessage(
message="Who discovered gravity?"),
ChatbotMessage(message="The man who is widely credited with discovering "
"gravity is Sir Isaac Newton")
],
message="What year was he born?",
)
print(chat)
async def test_chat_stream(self) -> None:
stream = self.co.chat_stream(
model="command-a-03-2025",
chat_history=[
UserMessage(
message="Who discovered gravity?"),
ChatbotMessage(message="The man who is widely credited with discovering "
"gravity is Sir Isaac Newton")
],
message="What year was he born?",
)
events = set()
async for chat_event in stream:
events.add(chat_event.event_type)
if chat_event.event_type == "text-generation":
print(chat_event.text)
self.assertTrue("text-generation" in events)
self.assertTrue("stream-start" in events)
self.assertTrue("stream-end" in events)
async def test_stream_equals_true(self) -> None:
with self.assertRaises(ValueError):
await self.co.chat(
stream=True, # type: ignore
message="What year was he born?",
)
async def test_deprecated_fn(self) -> None:
with self.assertRaises(ValueError):
await self.co.check_api_key("dummy", dummy="dummy") # type: ignore
async def test_moved_fn(self) -> None:
with self.assertRaises(ValueError):
await self.co.list_connectors("dummy", dummy="dummy") # type: ignore
@unittest.skipIf(os.getenv("CO_API_URL") is not None, "Doesn't work in staging.")
async def test_generate(self) -> None:
response = await self.co.generate(
prompt='Please explain to me how LLMs work',
)
print(response)
@unittest.skipIf(os.getenv("CO_API_URL") is not None, "Doesn't work in staging.")
async def test_embed(self) -> None:
response = await self.co.embed(
texts=['hello', 'goodbye'],
model='embed-english-v3.0',
input_type="classification"
)
print(response)
async def test_embed_batch_types(self) -> None:
# batch more than 96 texts
response = await self.co.embed(
texts=['hello']*100,
model='embed-english-v3.0',
input_type="classification",
embedding_types=["float", "int8", "uint8", "binary", "ubinary"]
)
if response.response_type == "embeddings_by_type":
self.assertEqual(len(response.texts or []), 100)
self.assertEqual(len(response.embeddings.float_ or []), 100)
self.assertEqual(len(response.embeddings.int8 or []), 100)
self.assertEqual(len(response.embeddings.uint8 or []), 100)
self.assertEqual(len(response.embeddings.binary or []), 100)
self.assertEqual(len(response.embeddings.ubinary or []), 100)
else:
self.fail("Expected embeddings_by_type response type")
print(response)
async def test_embed_batch_v1(self) -> None:
# batch more than 96 texts
response = await self.co.embed(
texts=['hello']*100,
model='embed-english-v3.0',
input_type="classification",
)
if response.response_type == "embeddings_floats":
self.assertEqual(len(response.embeddings), 100)
else:
self.fail("Expected embeddings_floats response type")
print(response)
@unittest.skip("temp")
async def test_embed_job_crud(self) -> None:
dataset = await self.co.datasets.create(
name="test",
type="embed-input",
data=open(embed_job, 'rb'),
)
result = await self.co.wait(dataset)
self.assertEqual(result.dataset.validation_status, "validated")
# start an embed job
job = await self.co.embed_jobs.create(
dataset_id=dataset.id or "",
input_type="search_document",
model='embed-english-v3.0')
print(job)
# list embed jobs
my_embed_jobs = await self.co.embed_jobs.list()
print(my_embed_jobs)
emb_result = await self.co.wait(job)
self.assertEqual(emb_result.status, "complete")
await self.co.embed_jobs.cancel(job.job_id)
await self.co.datasets.delete(dataset.id or "")
async def test_rerank(self) -> None:
docs = [
'Carson City is the capital city of the American state of Nevada.',
'The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.',
'Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.',
'Capital punishment (the death penalty) has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states.']
response = await self.co.rerank(
model='rerank-v3.5',
query='What is the capital of the United States?',
documents=docs,
top_n=3,
)
print(response)
@unittest.skipIf(os.getenv("CO_API_URL") is not None, "Doesn't work in staging.")
async def test_datasets_crud(self) -> None:
my_dataset = await self.co.datasets.create(
name="test",
type="embed-input",
data=open(embed_job, 'rb'),
)
print(my_dataset)
my_datasets = await self.co.datasets.list()
print(my_datasets)
dataset = await self.co.datasets.get(my_dataset.id or "")
print(dataset)
await self.co.datasets.delete(my_dataset.id or "")
@unittest.skipIf(os.getenv("CO_API_URL") is not None, "Doesn't work in staging.")
async def test_save_load(self) -> None:
my_dataset = await self.co.datasets.create(
name="test",
type="embed-input",
data=open(embed_job, 'rb'),
)
result = await self.co.wait(my_dataset)
self.co.utils.save_dataset(result.dataset, "dataset.jsonl")
# assert files equal
self.assertTrue(os.path.exists("dataset.jsonl"))
self.assertEqual(open(embed_job, 'rb').read(),
open("dataset.jsonl", 'rb').read())
print(result)
await self.co.datasets.delete(my_dataset.id or "")
async def test_tokenize(self) -> None:
response = await self.co.tokenize(
text='tokenize me! :D',
model="command-a-03-2025",
offline=False,
)
print(response)
async def test_detokenize(self) -> None:
response = await self.co.detokenize(
tokens=[10104, 12221, 1315, 34, 1420, 69],
model="command-a-03-2025",
offline=False,
)
print(response)
@unittest.skipIf(os.getenv("CO_API_URL") is not None, "Doesn't work in staging.")
async def test_tool_use(self) -> None:
tools = [
Tool(
name="sales_database",
description="Connects to a database about sales volumes",
parameter_definitions={
"day": ToolParameterDefinitionsValue(
description="Retrieves sales data from this day, formatted as YYYY-MM-DD.",
type="str",
required=True
)}
)
]
tool_parameters_response = await self.co.chat(
message="How good were the sales on September 29 2023?",
tools=tools,
model="command-nightly",
preamble="""
## Task Description
You help people answer their questions and other requests interactively. You will be asked a very wide array of requests on all kinds of topics. You will be equipped with a wide range of search engines or similar tools to help you, which you use to research your answer. You should focus on serving the user's needs as best you can, which will be wide-ranging.
## Style Guide
Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling.
"""
)
if tool_parameters_response.tool_calls is not None:
self.assertEqual(
tool_parameters_response.tool_calls[0].name, "sales_database")
self.assertEqual(tool_parameters_response.tool_calls[0].parameters, {
"day": "2023-09-29"})
else:
raise ValueError("Expected tool calls to be present")
local_tools = {
"sales_database": lambda day: {
"number_of_sales": 120,
"total_revenue": 48500,
"average_sale_value": 404.17,
"date": "2023-09-29"
}
}
tool_results = []
for tool_call in tool_parameters_response.tool_calls:
output = local_tools[tool_call.name](**tool_call.parameters)
outputs = [output]
tool_results.append(ToolResult(
call=tool_call,
outputs=outputs
))
cited_response = await self.co.chat(
message="How good were the sales on September 29?",
tools=tools,
tool_results=tool_results,
force_single_step=True,
model="command-a-03-2025",
)
self.assertEqual(cited_response.documents, [
{
"average_sale_value": "404.17",
"date": "2023-09-29",
"id": "sales_database:0:0",
"number_of_sales": "120",
"total_revenue": "48500",
}
])
async def test_local_tokenize(self) -> None:
response = await self.co.tokenize(
model="command-a-03-2025",
text="tokenize me! :D"
)
print(response)
async def test_local_detokenize(self) -> None:
response = await self.co.detokenize(
model="command-a-03-2025",
tokens=[10104, 12221, 1315, 34, 1420, 69]
)
print(response)
async def test_tokenize_async_context_with_sync_client(self) -> None:
# Test that the sync client can be used in an async context.
co = cohere.Client(timeout=10000)
print(co.tokenize(model="command-a-03-2025", text="tokenize me! :D"))
print(co.detokenize(model="command-a-03-2025", tokens=[
10104, 12221, 1315, 34, 1420, 69]))