-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathclient.py
More file actions
603 lines (480 loc) · 19.7 KB
/
client.py
File metadata and controls
603 lines (480 loc) · 19.7 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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# This file was auto-generated by Fern from our API Definition.
import datetime as dt
import typing
from .. import core
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.dataset_type import DatasetType
from ..types.dataset_validation_status import DatasetValidationStatus
from .raw_client import AsyncRawDatasetsClient, RawDatasetsClient
from .types.datasets_create_response import DatasetsCreateResponse
from .types.datasets_get_response import DatasetsGetResponse
from .types.datasets_get_usage_response import DatasetsGetUsageResponse
from .types.datasets_list_response import DatasetsListResponse
# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)
class DatasetsClient:
def __init__(self, *, client_wrapper: SyncClientWrapper):
self._raw_client = RawDatasetsClient(client_wrapper=client_wrapper)
@property
def with_raw_response(self) -> RawDatasetsClient:
"""
Retrieves a raw implementation of this client that returns raw responses.
Returns
-------
RawDatasetsClient
"""
return self._raw_client
def list(
self,
*,
dataset_type: typing.Optional[str] = None,
before: typing.Optional[dt.datetime] = None,
after: typing.Optional[dt.datetime] = None,
limit: typing.Optional[float] = None,
offset: typing.Optional[float] = None,
validation_status: typing.Optional[DatasetValidationStatus] = None,
request_options: typing.Optional[RequestOptions] = None,
) -> DatasetsListResponse:
"""
List datasets that have been created.
Parameters
----------
dataset_type : typing.Optional[str]
optional filter by dataset type
before : typing.Optional[dt.datetime]
optional filter before a date
after : typing.Optional[dt.datetime]
optional filter after a date
limit : typing.Optional[float]
optional limit to number of results
offset : typing.Optional[float]
optional offset to start of results
validation_status : typing.Optional[DatasetValidationStatus]
optional filter by validation status
request_options : typing.Optional[RequestOptions]
Request-specific configuration.
Returns
-------
DatasetsListResponse
A successful response.
Examples
--------
import datetime
from cohere import Client
client = Client(
client_name="YOUR_CLIENT_NAME",
token="YOUR_TOKEN",
)
client.datasets.list(
dataset_type="datasetType",
before=datetime.datetime.fromisoformat(
"2024-01-15 09:30:00+00:00",
),
after=datetime.datetime.fromisoformat(
"2024-01-15 09:30:00+00:00",
),
limit=1.1,
offset=1.1,
validation_status="unknown",
)
"""
_response = self._raw_client.list(
dataset_type=dataset_type,
before=before,
after=after,
limit=limit,
offset=offset,
validation_status=validation_status,
request_options=request_options,
)
return _response.data
def create(
self,
*,
name: str,
type: DatasetType,
data: core.File,
keep_original_file: typing.Optional[bool] = None,
skip_malformed_input: typing.Optional[bool] = None,
keep_fields: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
optional_fields: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
text_separator: typing.Optional[str] = None,
csv_delimiter: typing.Optional[str] = None,
eval_data: typing.Optional[core.File] = OMIT,
request_options: typing.Optional[RequestOptions] = None,
) -> DatasetsCreateResponse:
"""
Create a dataset by uploading a file. See ['Dataset Creation'](https://docs.cohere.com/docs/datasets#dataset-creation) for more information.
Parameters
----------
name : str
The name of the uploaded dataset.
type : DatasetType
The dataset type, which is used to validate the data. The only valid type is `embed-input` used in conjunction with the Embed Jobs API.
data : core.File
See core.File for more documentation
keep_original_file : typing.Optional[bool]
Indicates if the original file should be stored.
skip_malformed_input : typing.Optional[bool]
Indicates whether rows with malformed input should be dropped (instead of failing the validation check). Dropped rows will be returned in the warnings field.
keep_fields : typing.Optional[typing.Union[str, typing.Sequence[str]]]
List of names of fields that will be persisted in the Dataset. By default the Dataset will retain only the required fields indicated in the [schema for the corresponding Dataset type](https://docs.cohere.com/docs/datasets#dataset-types). For example, datasets of type `embed-input` will drop all fields other than the required `text` field. If any of the fields in `keep_fields` are missing from the uploaded file, Dataset validation will fail.
optional_fields : typing.Optional[typing.Union[str, typing.Sequence[str]]]
List of names of fields that will be persisted in the Dataset. By default the Dataset will retain only the required fields indicated in the [schema for the corresponding Dataset type](https://docs.cohere.com/docs/datasets#dataset-types). For example, Datasets of type `embed-input` will drop all fields other than the required `text` field. If any of the fields in `optional_fields` are missing from the uploaded file, Dataset validation will pass.
text_separator : typing.Optional[str]
Raw .txt uploads will be split into entries using the text_separator value.
csv_delimiter : typing.Optional[str]
The delimiter used for .csv uploads.
eval_data : typing.Optional[core.File]
See core.File for more documentation
request_options : typing.Optional[RequestOptions]
Request-specific configuration.
Returns
-------
DatasetsCreateResponse
A successful response.
Examples
--------
from cohere import Client
client = Client(
client_name="YOUR_CLIENT_NAME",
token="YOUR_TOKEN",
)
client.datasets.create(
name="name",
type="embed-input",
keep_original_file=True,
skip_malformed_input=True,
text_separator="text_separator",
csv_delimiter="csv_delimiter",
)
"""
_response = self._raw_client.create(
name=name,
type=type,
data=data,
keep_original_file=keep_original_file,
skip_malformed_input=skip_malformed_input,
keep_fields=keep_fields,
optional_fields=optional_fields,
text_separator=text_separator,
csv_delimiter=csv_delimiter,
eval_data=eval_data,
request_options=request_options,
)
return _response.data
def get_usage(self, *, request_options: typing.Optional[RequestOptions] = None) -> DatasetsGetUsageResponse:
"""
View the dataset storage usage for your Organization. Each Organization can have up to 10GB of storage across all their users.
Parameters
----------
request_options : typing.Optional[RequestOptions]
Request-specific configuration.
Returns
-------
DatasetsGetUsageResponse
A successful response.
Examples
--------
from cohere import Client
client = Client(
client_name="YOUR_CLIENT_NAME",
token="YOUR_TOKEN",
)
client.datasets.get_usage()
"""
_response = self._raw_client.get_usage(request_options=request_options)
return _response.data
def get(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> DatasetsGetResponse:
"""
Retrieve a dataset by ID. See ['Datasets'](https://docs.cohere.com/docs/datasets) for more information.
Parameters
----------
id : str
request_options : typing.Optional[RequestOptions]
Request-specific configuration.
Returns
-------
DatasetsGetResponse
A successful response.
Examples
--------
from cohere import Client
client = Client(
client_name="YOUR_CLIENT_NAME",
token="YOUR_TOKEN",
)
client.datasets.get(
id="id",
)
"""
_response = self._raw_client.get(id, request_options=request_options)
return _response.data
def delete(
self, id: str, *, request_options: typing.Optional[RequestOptions] = None
) -> typing.Dict[str, typing.Any]:
"""
Delete a dataset by ID. Datasets are automatically deleted after 30 days, but they can also be deleted manually.
Parameters
----------
id : str
request_options : typing.Optional[RequestOptions]
Request-specific configuration.
Returns
-------
typing.Dict[str, typing.Any]
A successful response.
Examples
--------
from cohere import Client
client = Client(
client_name="YOUR_CLIENT_NAME",
token="YOUR_TOKEN",
)
client.datasets.delete(
id="id",
)
"""
_response = self._raw_client.delete(id, request_options=request_options)
return _response.data
class AsyncDatasetsClient:
def __init__(self, *, client_wrapper: AsyncClientWrapper):
self._raw_client = AsyncRawDatasetsClient(client_wrapper=client_wrapper)
@property
def with_raw_response(self) -> AsyncRawDatasetsClient:
"""
Retrieves a raw implementation of this client that returns raw responses.
Returns
-------
AsyncRawDatasetsClient
"""
return self._raw_client
async def list(
self,
*,
dataset_type: typing.Optional[str] = None,
before: typing.Optional[dt.datetime] = None,
after: typing.Optional[dt.datetime] = None,
limit: typing.Optional[float] = None,
offset: typing.Optional[float] = None,
validation_status: typing.Optional[DatasetValidationStatus] = None,
request_options: typing.Optional[RequestOptions] = None,
) -> DatasetsListResponse:
"""
List datasets that have been created.
Parameters
----------
dataset_type : typing.Optional[str]
optional filter by dataset type
before : typing.Optional[dt.datetime]
optional filter before a date
after : typing.Optional[dt.datetime]
optional filter after a date
limit : typing.Optional[float]
optional limit to number of results
offset : typing.Optional[float]
optional offset to start of results
validation_status : typing.Optional[DatasetValidationStatus]
optional filter by validation status
request_options : typing.Optional[RequestOptions]
Request-specific configuration.
Returns
-------
DatasetsListResponse
A successful response.
Examples
--------
import asyncio
import datetime
from cohere import AsyncClient
client = AsyncClient(
client_name="YOUR_CLIENT_NAME",
token="YOUR_TOKEN",
)
async def main() -> None:
await client.datasets.list(
dataset_type="datasetType",
before=datetime.datetime.fromisoformat(
"2024-01-15 09:30:00+00:00",
),
after=datetime.datetime.fromisoformat(
"2024-01-15 09:30:00+00:00",
),
limit=1.1,
offset=1.1,
validation_status="unknown",
)
asyncio.run(main())
"""
_response = await self._raw_client.list(
dataset_type=dataset_type,
before=before,
after=after,
limit=limit,
offset=offset,
validation_status=validation_status,
request_options=request_options,
)
return _response.data
async def create(
self,
*,
name: str,
type: DatasetType,
data: core.File,
keep_original_file: typing.Optional[bool] = None,
skip_malformed_input: typing.Optional[bool] = None,
keep_fields: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
optional_fields: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
text_separator: typing.Optional[str] = None,
csv_delimiter: typing.Optional[str] = None,
eval_data: typing.Optional[core.File] = OMIT,
request_options: typing.Optional[RequestOptions] = None,
) -> DatasetsCreateResponse:
"""
Create a dataset by uploading a file. See ['Dataset Creation'](https://docs.cohere.com/docs/datasets#dataset-creation) for more information.
Parameters
----------
name : str
The name of the uploaded dataset.
type : DatasetType
The dataset type, which is used to validate the data. The only valid type is `embed-input` used in conjunction with the Embed Jobs API.
data : core.File
See core.File for more documentation
keep_original_file : typing.Optional[bool]
Indicates if the original file should be stored.
skip_malformed_input : typing.Optional[bool]
Indicates whether rows with malformed input should be dropped (instead of failing the validation check). Dropped rows will be returned in the warnings field.
keep_fields : typing.Optional[typing.Union[str, typing.Sequence[str]]]
List of names of fields that will be persisted in the Dataset. By default the Dataset will retain only the required fields indicated in the [schema for the corresponding Dataset type](https://docs.cohere.com/docs/datasets#dataset-types). For example, datasets of type `embed-input` will drop all fields other than the required `text` field. If any of the fields in `keep_fields` are missing from the uploaded file, Dataset validation will fail.
optional_fields : typing.Optional[typing.Union[str, typing.Sequence[str]]]
List of names of fields that will be persisted in the Dataset. By default the Dataset will retain only the required fields indicated in the [schema for the corresponding Dataset type](https://docs.cohere.com/docs/datasets#dataset-types). For example, Datasets of type `embed-input` will drop all fields other than the required `text` field. If any of the fields in `optional_fields` are missing from the uploaded file, Dataset validation will pass.
text_separator : typing.Optional[str]
Raw .txt uploads will be split into entries using the text_separator value.
csv_delimiter : typing.Optional[str]
The delimiter used for .csv uploads.
eval_data : typing.Optional[core.File]
See core.File for more documentation
request_options : typing.Optional[RequestOptions]
Request-specific configuration.
Returns
-------
DatasetsCreateResponse
A successful response.
Examples
--------
import asyncio
from cohere import AsyncClient
client = AsyncClient(
client_name="YOUR_CLIENT_NAME",
token="YOUR_TOKEN",
)
async def main() -> None:
await client.datasets.create(
name="name",
type="embed-input",
keep_original_file=True,
skip_malformed_input=True,
text_separator="text_separator",
csv_delimiter="csv_delimiter",
)
asyncio.run(main())
"""
_response = await self._raw_client.create(
name=name,
type=type,
data=data,
keep_original_file=keep_original_file,
skip_malformed_input=skip_malformed_input,
keep_fields=keep_fields,
optional_fields=optional_fields,
text_separator=text_separator,
csv_delimiter=csv_delimiter,
eval_data=eval_data,
request_options=request_options,
)
return _response.data
async def get_usage(self, *, request_options: typing.Optional[RequestOptions] = None) -> DatasetsGetUsageResponse:
"""
View the dataset storage usage for your Organization. Each Organization can have up to 10GB of storage across all their users.
Parameters
----------
request_options : typing.Optional[RequestOptions]
Request-specific configuration.
Returns
-------
DatasetsGetUsageResponse
A successful response.
Examples
--------
import asyncio
from cohere import AsyncClient
client = AsyncClient(
client_name="YOUR_CLIENT_NAME",
token="YOUR_TOKEN",
)
async def main() -> None:
await client.datasets.get_usage()
asyncio.run(main())
"""
_response = await self._raw_client.get_usage(request_options=request_options)
return _response.data
async def get(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> DatasetsGetResponse:
"""
Retrieve a dataset by ID. See ['Datasets'](https://docs.cohere.com/docs/datasets) for more information.
Parameters
----------
id : str
request_options : typing.Optional[RequestOptions]
Request-specific configuration.
Returns
-------
DatasetsGetResponse
A successful response.
Examples
--------
import asyncio
from cohere import AsyncClient
client = AsyncClient(
client_name="YOUR_CLIENT_NAME",
token="YOUR_TOKEN",
)
async def main() -> None:
await client.datasets.get(
id="id",
)
asyncio.run(main())
"""
_response = await self._raw_client.get(id, request_options=request_options)
return _response.data
async def delete(
self, id: str, *, request_options: typing.Optional[RequestOptions] = None
) -> typing.Dict[str, typing.Any]:
"""
Delete a dataset by ID. Datasets are automatically deleted after 30 days, but they can also be deleted manually.
Parameters
----------
id : str
request_options : typing.Optional[RequestOptions]
Request-specific configuration.
Returns
-------
typing.Dict[str, typing.Any]
A successful response.
Examples
--------
import asyncio
from cohere import AsyncClient
client = AsyncClient(
client_name="YOUR_CLIENT_NAME",
token="YOUR_TOKEN",
)
async def main() -> None:
await client.datasets.delete(
id="id",
)
asyncio.run(main())
"""
_response = await self._raw_client.delete(id, request_options=request_options)
return _response.data