cohere-python

Форк
0
970 строк · 45.5 Кб
1
# This file was auto-generated by Fern from our API Definition.
2

3
import typing
4
import urllib.parse
5
from json.decoder import JSONDecodeError
6

7
from ..core.api_error import ApiError
8
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
9
from ..core.jsonable_encoder import jsonable_encoder
10
from ..core.remove_none_from_dict import remove_none_from_dict
11
from ..core.request_options import RequestOptions
12
from ..errors.bad_request_error import BadRequestError
13
from ..errors.forbidden_error import ForbiddenError
14
from ..errors.internal_server_error import InternalServerError
15
from ..errors.not_found_error import NotFoundError
16
from ..errors.too_many_requests_error import TooManyRequestsError
17
from ..types.create_connector_o_auth import CreateConnectorOAuth
18
from ..types.create_connector_response import CreateConnectorResponse
19
from ..types.create_connector_service_auth import CreateConnectorServiceAuth
20
from ..types.delete_connector_response import DeleteConnectorResponse
21
from ..types.get_connector_response import GetConnectorResponse
22
from ..types.list_connectors_response import ListConnectorsResponse
23
from ..types.o_auth_authorize_response import OAuthAuthorizeResponse
24
from ..types.update_connector_response import UpdateConnectorResponse
25

26
try:
27
    import pydantic.v1 as pydantic  # type: ignore
28
except ImportError:
29
    import pydantic  # type: ignore
30

31
# this is used as the default value for optional parameters
32
OMIT = typing.cast(typing.Any, ...)
33

34

35
class ConnectorsClient:
36
    def __init__(self, *, client_wrapper: SyncClientWrapper):
37
        self._client_wrapper = client_wrapper
38

39
    def list(
40
        self,
41
        *,
42
        limit: typing.Optional[float] = None,
43
        offset: typing.Optional[float] = None,
44
        request_options: typing.Optional[RequestOptions] = None,
45
    ) -> ListConnectorsResponse:
46
        """
47
        Returns a list of connectors ordered by descending creation date (newer first). See ['Managing your Connector'](https://docs.cohere.com/docs/managing-your-connector) for more information.
48

49
        Parameters:
50
            - limit: typing.Optional[float]. Maximum number of connectors to return [0, 100].
51

52
            - offset: typing.Optional[float]. Number of connectors to skip before returning results [0, inf].
53

54
            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
55
        ---
56
        from cohere.client import Client
57

58
        client = Client(
59
            client_name="YOUR_CLIENT_NAME",
60
            token="YOUR_TOKEN",
61
        )
62
        client.connectors.list()
63
        """
64
        _response = self._client_wrapper.httpx_client.request(
65
            "GET",
66
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "connectors"),
67
            params=jsonable_encoder(
68
                remove_none_from_dict(
69
                    {
70
                        "limit": limit,
71
                        "offset": offset,
72
                        **(
73
                            request_options.get("additional_query_parameters", {})
74
                            if request_options is not None
75
                            else {}
76
                        ),
77
                    }
78
                )
79
            ),
80
            headers=jsonable_encoder(
81
                remove_none_from_dict(
82
                    {
83
                        **self._client_wrapper.get_headers(),
84
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
85
                    }
86
                )
87
            ),
88
            timeout=request_options.get("timeout_in_seconds")
89
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
90
            else self._client_wrapper.get_timeout(),
91
            retries=0,
92
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
93
        )
94
        if 200 <= _response.status_code < 300:
95
            return pydantic.parse_obj_as(ListConnectorsResponse, _response.json())  # type: ignore
96
        if _response.status_code == 400:
97
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
98
        if _response.status_code == 429:
99
            raise TooManyRequestsError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
100
        if _response.status_code == 500:
101
            raise InternalServerError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
102
        try:
103
            _response_json = _response.json()
104
        except JSONDecodeError:
105
            raise ApiError(status_code=_response.status_code, body=_response.text)
106
        raise ApiError(status_code=_response.status_code, body=_response_json)
107

108
    def create(
109
        self,
110
        *,
111
        name: str,
112
        description: typing.Optional[str] = OMIT,
113
        url: str,
114
        excludes: typing.Optional[typing.Sequence[str]] = OMIT,
115
        oauth: typing.Optional[CreateConnectorOAuth] = OMIT,
116
        active: typing.Optional[bool] = OMIT,
117
        continue_on_failure: typing.Optional[bool] = OMIT,
118
        service_auth: typing.Optional[CreateConnectorServiceAuth] = OMIT,
119
        request_options: typing.Optional[RequestOptions] = None,
120
    ) -> CreateConnectorResponse:
121
        """
122
        Creates a new connector. The connector is tested during registration and will cancel registration when the test is unsuccessful. See ['Creating and Deploying a Connector'](https://docs.cohere.com/docs/creating-and-deploying-a-connector) for more information.
123

124
        Parameters:
125
            - name: str. A human-readable name for the connector.
126

127
            - description: typing.Optional[str]. A description of the connector.
128

129
            - url: str. The URL of the connector that will be used to search for documents.
130

131
            - excludes: typing.Optional[typing.Sequence[str]]. A list of fields to exclude from the prompt (fields remain in the document).
132

133
            - oauth: typing.Optional[CreateConnectorOAuth]. The OAuth 2.0 configuration for the connector. Cannot be specified if service_auth is specified.
134

135
            - active: typing.Optional[bool]. Whether the connector is active or not.
136

137
            - continue_on_failure: typing.Optional[bool]. Whether a chat request should continue or not if the request to this connector fails.
138

139
            - service_auth: typing.Optional[CreateConnectorServiceAuth]. The service to service authentication configuration for the connector. Cannot be specified if oauth is specified.
140

141
            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
142
        ---
143
        from cohere.client import Client
144

145
        client = Client(
146
            client_name="YOUR_CLIENT_NAME",
147
            token="YOUR_TOKEN",
148
        )
149
        client.connectors.create(
150
            name="name",
151
            url="url",
152
        )
153
        """
154
        _request: typing.Dict[str, typing.Any] = {"name": name, "url": url}
155
        if description is not OMIT:
156
            _request["description"] = description
157
        if excludes is not OMIT:
158
            _request["excludes"] = excludes
159
        if oauth is not OMIT:
160
            _request["oauth"] = oauth
161
        if active is not OMIT:
162
            _request["active"] = active
163
        if continue_on_failure is not OMIT:
164
            _request["continue_on_failure"] = continue_on_failure
165
        if service_auth is not OMIT:
166
            _request["service_auth"] = service_auth
167
        _response = self._client_wrapper.httpx_client.request(
168
            "POST",
169
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "connectors"),
170
            params=jsonable_encoder(
171
                request_options.get("additional_query_parameters") if request_options is not None else None
172
            ),
173
            json=jsonable_encoder(_request)
174
            if request_options is None or request_options.get("additional_body_parameters") is None
175
            else {
176
                **jsonable_encoder(_request),
177
                **(jsonable_encoder(remove_none_from_dict(request_options.get("additional_body_parameters", {})))),
178
            },
179
            headers=jsonable_encoder(
180
                remove_none_from_dict(
181
                    {
182
                        **self._client_wrapper.get_headers(),
183
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
184
                    }
185
                )
186
            ),
187
            timeout=request_options.get("timeout_in_seconds")
188
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
189
            else self._client_wrapper.get_timeout(),
190
            retries=0,
191
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
192
        )
193
        if 200 <= _response.status_code < 300:
194
            return pydantic.parse_obj_as(CreateConnectorResponse, _response.json())  # type: ignore
195
        if _response.status_code == 400:
196
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
197
        if _response.status_code == 403:
198
            raise ForbiddenError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
199
        if _response.status_code == 429:
200
            raise TooManyRequestsError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
201
        if _response.status_code == 500:
202
            raise InternalServerError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
203
        try:
204
            _response_json = _response.json()
205
        except JSONDecodeError:
206
            raise ApiError(status_code=_response.status_code, body=_response.text)
207
        raise ApiError(status_code=_response.status_code, body=_response_json)
208

209
    def get(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> GetConnectorResponse:
210
        """
211
        Retrieve a connector by ID. See ['Connectors'](https://docs.cohere.com/docs/connectors) for more information.
212

213
        Parameters:
214
            - id: str. The ID of the connector to retrieve.
215

216
            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
217
        ---
218
        from cohere.client import Client
219

220
        client = Client(
221
            client_name="YOUR_CLIENT_NAME",
222
            token="YOUR_TOKEN",
223
        )
224
        client.connectors.get(
225
            id="id",
226
        )
227
        """
228
        _response = self._client_wrapper.httpx_client.request(
229
            "GET",
230
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"connectors/{jsonable_encoder(id)}"),
231
            params=jsonable_encoder(
232
                request_options.get("additional_query_parameters") if request_options is not None else None
233
            ),
234
            headers=jsonable_encoder(
235
                remove_none_from_dict(
236
                    {
237
                        **self._client_wrapper.get_headers(),
238
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
239
                    }
240
                )
241
            ),
242
            timeout=request_options.get("timeout_in_seconds")
243
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
244
            else self._client_wrapper.get_timeout(),
245
            retries=0,
246
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
247
        )
248
        if 200 <= _response.status_code < 300:
249
            return pydantic.parse_obj_as(GetConnectorResponse, _response.json())  # type: ignore
250
        if _response.status_code == 400:
251
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
252
        if _response.status_code == 404:
253
            raise NotFoundError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
254
        if _response.status_code == 429:
255
            raise TooManyRequestsError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
256
        if _response.status_code == 500:
257
            raise InternalServerError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
258
        try:
259
            _response_json = _response.json()
260
        except JSONDecodeError:
261
            raise ApiError(status_code=_response.status_code, body=_response.text)
262
        raise ApiError(status_code=_response.status_code, body=_response_json)
263

264
    def delete(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> DeleteConnectorResponse:
265
        """
266
        Delete a connector by ID. See ['Connectors'](https://docs.cohere.com/docs/connectors) for more information.
267

268
        Parameters:
269
            - id: str. The ID of the connector to delete.
270

271
            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
272
        ---
273
        from cohere.client import Client
274

275
        client = Client(
276
            client_name="YOUR_CLIENT_NAME",
277
            token="YOUR_TOKEN",
278
        )
279
        client.connectors.delete(
280
            id="id",
281
        )
282
        """
283
        _response = self._client_wrapper.httpx_client.request(
284
            "DELETE",
285
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"connectors/{jsonable_encoder(id)}"),
286
            params=jsonable_encoder(
287
                request_options.get("additional_query_parameters") if request_options is not None else None
288
            ),
289
            headers=jsonable_encoder(
290
                remove_none_from_dict(
291
                    {
292
                        **self._client_wrapper.get_headers(),
293
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
294
                    }
295
                )
296
            ),
297
            timeout=request_options.get("timeout_in_seconds")
298
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
299
            else self._client_wrapper.get_timeout(),
300
            retries=0,
301
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
302
        )
303
        if 200 <= _response.status_code < 300:
304
            return pydantic.parse_obj_as(DeleteConnectorResponse, _response.json())  # type: ignore
305
        if _response.status_code == 400:
306
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
307
        if _response.status_code == 403:
308
            raise ForbiddenError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
309
        if _response.status_code == 404:
310
            raise NotFoundError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
311
        if _response.status_code == 429:
312
            raise TooManyRequestsError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
313
        if _response.status_code == 500:
314
            raise InternalServerError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
315
        try:
316
            _response_json = _response.json()
317
        except JSONDecodeError:
318
            raise ApiError(status_code=_response.status_code, body=_response.text)
319
        raise ApiError(status_code=_response.status_code, body=_response_json)
320

321
    def update(
322
        self,
323
        id: str,
324
        *,
325
        name: typing.Optional[str] = OMIT,
326
        url: typing.Optional[str] = OMIT,
327
        excludes: typing.Optional[typing.Sequence[str]] = OMIT,
328
        oauth: typing.Optional[CreateConnectorOAuth] = OMIT,
329
        active: typing.Optional[bool] = OMIT,
330
        continue_on_failure: typing.Optional[bool] = OMIT,
331
        service_auth: typing.Optional[CreateConnectorServiceAuth] = OMIT,
332
        request_options: typing.Optional[RequestOptions] = None,
333
    ) -> UpdateConnectorResponse:
334
        """
335
        Update a connector by ID. Omitted fields will not be updated. See ['Managing your Connector'](https://docs.cohere.com/docs/managing-your-connector) for more information.
336

337
        Parameters:
338
            - id: str. The ID of the connector to update.
339

340
            - name: typing.Optional[str]. A human-readable name for the connector.
341

342
            - url: typing.Optional[str]. The URL of the connector that will be used to search for documents.
343

344
            - excludes: typing.Optional[typing.Sequence[str]]. A list of fields to exclude from the prompt (fields remain in the document).
345

346
            - oauth: typing.Optional[CreateConnectorOAuth]. The OAuth 2.0 configuration for the connector. Cannot be specified if service_auth is specified.
347

348
            - active: typing.Optional[bool].
349

350
            - continue_on_failure: typing.Optional[bool].
351

352
            - service_auth: typing.Optional[CreateConnectorServiceAuth]. The service to service authentication configuration for the connector. Cannot be specified if oauth is specified.
353

354
            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
355
        ---
356
        from cohere.client import Client
357

358
        client = Client(
359
            client_name="YOUR_CLIENT_NAME",
360
            token="YOUR_TOKEN",
361
        )
362
        client.connectors.update(
363
            id="id",
364
        )
365
        """
366
        _request: typing.Dict[str, typing.Any] = {}
367
        if name is not OMIT:
368
            _request["name"] = name
369
        if url is not OMIT:
370
            _request["url"] = url
371
        if excludes is not OMIT:
372
            _request["excludes"] = excludes
373
        if oauth is not OMIT:
374
            _request["oauth"] = oauth
375
        if active is not OMIT:
376
            _request["active"] = active
377
        if continue_on_failure is not OMIT:
378
            _request["continue_on_failure"] = continue_on_failure
379
        if service_auth is not OMIT:
380
            _request["service_auth"] = service_auth
381
        _response = self._client_wrapper.httpx_client.request(
382
            "PATCH",
383
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"connectors/{jsonable_encoder(id)}"),
384
            params=jsonable_encoder(
385
                request_options.get("additional_query_parameters") if request_options is not None else None
386
            ),
387
            json=jsonable_encoder(_request)
388
            if request_options is None or request_options.get("additional_body_parameters") is None
389
            else {
390
                **jsonable_encoder(_request),
391
                **(jsonable_encoder(remove_none_from_dict(request_options.get("additional_body_parameters", {})))),
392
            },
393
            headers=jsonable_encoder(
394
                remove_none_from_dict(
395
                    {
396
                        **self._client_wrapper.get_headers(),
397
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
398
                    }
399
                )
400
            ),
401
            timeout=request_options.get("timeout_in_seconds")
402
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
403
            else self._client_wrapper.get_timeout(),
404
            retries=0,
405
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
406
        )
407
        if 200 <= _response.status_code < 300:
408
            return pydantic.parse_obj_as(UpdateConnectorResponse, _response.json())  # type: ignore
409
        if _response.status_code == 400:
410
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
411
        if _response.status_code == 403:
412
            raise ForbiddenError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
413
        if _response.status_code == 404:
414
            raise NotFoundError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
415
        if _response.status_code == 429:
416
            raise TooManyRequestsError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
417
        if _response.status_code == 500:
418
            raise InternalServerError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
419
        try:
420
            _response_json = _response.json()
421
        except JSONDecodeError:
422
            raise ApiError(status_code=_response.status_code, body=_response.text)
423
        raise ApiError(status_code=_response.status_code, body=_response_json)
424

425
    def o_auth_authorize(
426
        self,
427
        id: str,
428
        *,
429
        after_token_redirect: typing.Optional[str] = None,
430
        request_options: typing.Optional[RequestOptions] = None,
431
    ) -> OAuthAuthorizeResponse:
432
        """
433
        Authorize the connector with the given ID for the connector oauth app. See ['Connector Authentication'](https://docs.cohere.com/docs/connector-authentication) for more information.
434

435
        Parameters:
436
            - id: str. The ID of the connector to authorize.
437

438
            - after_token_redirect: typing.Optional[str]. The URL to redirect to after the connector has been authorized.
439

440
            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
441
        ---
442
        from cohere.client import Client
443

444
        client = Client(
445
            client_name="YOUR_CLIENT_NAME",
446
            token="YOUR_TOKEN",
447
        )
448
        client.connectors.o_auth_authorize(
449
            id="id",
450
        )
451
        """
452
        _response = self._client_wrapper.httpx_client.request(
453
            "POST",
454
            urllib.parse.urljoin(
455
                f"{self._client_wrapper.get_base_url()}/", f"connectors/{jsonable_encoder(id)}/oauth/authorize"
456
            ),
457
            params=jsonable_encoder(
458
                remove_none_from_dict(
459
                    {
460
                        "after_token_redirect": after_token_redirect,
461
                        **(
462
                            request_options.get("additional_query_parameters", {})
463
                            if request_options is not None
464
                            else {}
465
                        ),
466
                    }
467
                )
468
            ),
469
            json=jsonable_encoder(remove_none_from_dict(request_options.get("additional_body_parameters", {})))
470
            if request_options is not None
471
            else None,
472
            headers=jsonable_encoder(
473
                remove_none_from_dict(
474
                    {
475
                        **self._client_wrapper.get_headers(),
476
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
477
                    }
478
                )
479
            ),
480
            timeout=request_options.get("timeout_in_seconds")
481
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
482
            else self._client_wrapper.get_timeout(),
483
            retries=0,
484
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
485
        )
486
        if 200 <= _response.status_code < 300:
487
            return pydantic.parse_obj_as(OAuthAuthorizeResponse, _response.json())  # type: ignore
488
        if _response.status_code == 400:
489
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
490
        if _response.status_code == 404:
491
            raise NotFoundError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
492
        if _response.status_code == 429:
493
            raise TooManyRequestsError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
494
        if _response.status_code == 500:
495
            raise InternalServerError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
496
        try:
497
            _response_json = _response.json()
498
        except JSONDecodeError:
499
            raise ApiError(status_code=_response.status_code, body=_response.text)
500
        raise ApiError(status_code=_response.status_code, body=_response_json)
501

502

503
class AsyncConnectorsClient:
504
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
505
        self._client_wrapper = client_wrapper
506

507
    async def list(
508
        self,
509
        *,
510
        limit: typing.Optional[float] = None,
511
        offset: typing.Optional[float] = None,
512
        request_options: typing.Optional[RequestOptions] = None,
513
    ) -> ListConnectorsResponse:
514
        """
515
        Returns a list of connectors ordered by descending creation date (newer first). See ['Managing your Connector'](https://docs.cohere.com/docs/managing-your-connector) for more information.
516

517
        Parameters:
518
            - limit: typing.Optional[float]. Maximum number of connectors to return [0, 100].
519

520
            - offset: typing.Optional[float]. Number of connectors to skip before returning results [0, inf].
521

522
            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
523
        ---
524
        from cohere.client import AsyncClient
525

526
        client = AsyncClient(
527
            client_name="YOUR_CLIENT_NAME",
528
            token="YOUR_TOKEN",
529
        )
530
        await client.connectors.list()
531
        """
532
        _response = await self._client_wrapper.httpx_client.request(
533
            "GET",
534
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "connectors"),
535
            params=jsonable_encoder(
536
                remove_none_from_dict(
537
                    {
538
                        "limit": limit,
539
                        "offset": offset,
540
                        **(
541
                            request_options.get("additional_query_parameters", {})
542
                            if request_options is not None
543
                            else {}
544
                        ),
545
                    }
546
                )
547
            ),
548
            headers=jsonable_encoder(
549
                remove_none_from_dict(
550
                    {
551
                        **self._client_wrapper.get_headers(),
552
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
553
                    }
554
                )
555
            ),
556
            timeout=request_options.get("timeout_in_seconds")
557
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
558
            else self._client_wrapper.get_timeout(),
559
            retries=0,
560
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
561
        )
562
        if 200 <= _response.status_code < 300:
563
            return pydantic.parse_obj_as(ListConnectorsResponse, _response.json())  # type: ignore
564
        if _response.status_code == 400:
565
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
566
        if _response.status_code == 429:
567
            raise TooManyRequestsError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
568
        if _response.status_code == 500:
569
            raise InternalServerError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
570
        try:
571
            _response_json = _response.json()
572
        except JSONDecodeError:
573
            raise ApiError(status_code=_response.status_code, body=_response.text)
574
        raise ApiError(status_code=_response.status_code, body=_response_json)
575

576
    async def create(
577
        self,
578
        *,
579
        name: str,
580
        description: typing.Optional[str] = OMIT,
581
        url: str,
582
        excludes: typing.Optional[typing.Sequence[str]] = OMIT,
583
        oauth: typing.Optional[CreateConnectorOAuth] = OMIT,
584
        active: typing.Optional[bool] = OMIT,
585
        continue_on_failure: typing.Optional[bool] = OMIT,
586
        service_auth: typing.Optional[CreateConnectorServiceAuth] = OMIT,
587
        request_options: typing.Optional[RequestOptions] = None,
588
    ) -> CreateConnectorResponse:
589
        """
590
        Creates a new connector. The connector is tested during registration and will cancel registration when the test is unsuccessful. See ['Creating and Deploying a Connector'](https://docs.cohere.com/docs/creating-and-deploying-a-connector) for more information.
591

592
        Parameters:
593
            - name: str. A human-readable name for the connector.
594

595
            - description: typing.Optional[str]. A description of the connector.
596

597
            - url: str. The URL of the connector that will be used to search for documents.
598

599
            - excludes: typing.Optional[typing.Sequence[str]]. A list of fields to exclude from the prompt (fields remain in the document).
600

601
            - oauth: typing.Optional[CreateConnectorOAuth]. The OAuth 2.0 configuration for the connector. Cannot be specified if service_auth is specified.
602

603
            - active: typing.Optional[bool]. Whether the connector is active or not.
604

605
            - continue_on_failure: typing.Optional[bool]. Whether a chat request should continue or not if the request to this connector fails.
606

607
            - service_auth: typing.Optional[CreateConnectorServiceAuth]. The service to service authentication configuration for the connector. Cannot be specified if oauth is specified.
608

609
            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
610
        ---
611
        from cohere.client import AsyncClient
612

613
        client = AsyncClient(
614
            client_name="YOUR_CLIENT_NAME",
615
            token="YOUR_TOKEN",
616
        )
617
        await client.connectors.create(
618
            name="name",
619
            url="url",
620
        )
621
        """
622
        _request: typing.Dict[str, typing.Any] = {"name": name, "url": url}
623
        if description is not OMIT:
624
            _request["description"] = description
625
        if excludes is not OMIT:
626
            _request["excludes"] = excludes
627
        if oauth is not OMIT:
628
            _request["oauth"] = oauth
629
        if active is not OMIT:
630
            _request["active"] = active
631
        if continue_on_failure is not OMIT:
632
            _request["continue_on_failure"] = continue_on_failure
633
        if service_auth is not OMIT:
634
            _request["service_auth"] = service_auth
635
        _response = await self._client_wrapper.httpx_client.request(
636
            "POST",
637
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "connectors"),
638
            params=jsonable_encoder(
639
                request_options.get("additional_query_parameters") if request_options is not None else None
640
            ),
641
            json=jsonable_encoder(_request)
642
            if request_options is None or request_options.get("additional_body_parameters") is None
643
            else {
644
                **jsonable_encoder(_request),
645
                **(jsonable_encoder(remove_none_from_dict(request_options.get("additional_body_parameters", {})))),
646
            },
647
            headers=jsonable_encoder(
648
                remove_none_from_dict(
649
                    {
650
                        **self._client_wrapper.get_headers(),
651
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
652
                    }
653
                )
654
            ),
655
            timeout=request_options.get("timeout_in_seconds")
656
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
657
            else self._client_wrapper.get_timeout(),
658
            retries=0,
659
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
660
        )
661
        if 200 <= _response.status_code < 300:
662
            return pydantic.parse_obj_as(CreateConnectorResponse, _response.json())  # type: ignore
663
        if _response.status_code == 400:
664
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
665
        if _response.status_code == 403:
666
            raise ForbiddenError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
667
        if _response.status_code == 429:
668
            raise TooManyRequestsError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
669
        if _response.status_code == 500:
670
            raise InternalServerError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
671
        try:
672
            _response_json = _response.json()
673
        except JSONDecodeError:
674
            raise ApiError(status_code=_response.status_code, body=_response.text)
675
        raise ApiError(status_code=_response.status_code, body=_response_json)
676

677
    async def get(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> GetConnectorResponse:
678
        """
679
        Retrieve a connector by ID. See ['Connectors'](https://docs.cohere.com/docs/connectors) for more information.
680

681
        Parameters:
682
            - id: str. The ID of the connector to retrieve.
683

684
            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
685
        ---
686
        from cohere.client import AsyncClient
687

688
        client = AsyncClient(
689
            client_name="YOUR_CLIENT_NAME",
690
            token="YOUR_TOKEN",
691
        )
692
        await client.connectors.get(
693
            id="id",
694
        )
695
        """
696
        _response = await self._client_wrapper.httpx_client.request(
697
            "GET",
698
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"connectors/{jsonable_encoder(id)}"),
699
            params=jsonable_encoder(
700
                request_options.get("additional_query_parameters") if request_options is not None else None
701
            ),
702
            headers=jsonable_encoder(
703
                remove_none_from_dict(
704
                    {
705
                        **self._client_wrapper.get_headers(),
706
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
707
                    }
708
                )
709
            ),
710
            timeout=request_options.get("timeout_in_seconds")
711
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
712
            else self._client_wrapper.get_timeout(),
713
            retries=0,
714
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
715
        )
716
        if 200 <= _response.status_code < 300:
717
            return pydantic.parse_obj_as(GetConnectorResponse, _response.json())  # type: ignore
718
        if _response.status_code == 400:
719
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
720
        if _response.status_code == 404:
721
            raise NotFoundError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
722
        if _response.status_code == 429:
723
            raise TooManyRequestsError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
724
        if _response.status_code == 500:
725
            raise InternalServerError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
726
        try:
727
            _response_json = _response.json()
728
        except JSONDecodeError:
729
            raise ApiError(status_code=_response.status_code, body=_response.text)
730
        raise ApiError(status_code=_response.status_code, body=_response_json)
731

732
    async def delete(
733
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
734
    ) -> DeleteConnectorResponse:
735
        """
736
        Delete a connector by ID. See ['Connectors'](https://docs.cohere.com/docs/connectors) for more information.
737

738
        Parameters:
739
            - id: str. The ID of the connector to delete.
740

741
            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
742
        ---
743
        from cohere.client import AsyncClient
744

745
        client = AsyncClient(
746
            client_name="YOUR_CLIENT_NAME",
747
            token="YOUR_TOKEN",
748
        )
749
        await client.connectors.delete(
750
            id="id",
751
        )
752
        """
753
        _response = await self._client_wrapper.httpx_client.request(
754
            "DELETE",
755
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"connectors/{jsonable_encoder(id)}"),
756
            params=jsonable_encoder(
757
                request_options.get("additional_query_parameters") if request_options is not None else None
758
            ),
759
            headers=jsonable_encoder(
760
                remove_none_from_dict(
761
                    {
762
                        **self._client_wrapper.get_headers(),
763
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
764
                    }
765
                )
766
            ),
767
            timeout=request_options.get("timeout_in_seconds")
768
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
769
            else self._client_wrapper.get_timeout(),
770
            retries=0,
771
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
772
        )
773
        if 200 <= _response.status_code < 300:
774
            return pydantic.parse_obj_as(DeleteConnectorResponse, _response.json())  # type: ignore
775
        if _response.status_code == 400:
776
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
777
        if _response.status_code == 403:
778
            raise ForbiddenError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
779
        if _response.status_code == 404:
780
            raise NotFoundError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
781
        if _response.status_code == 429:
782
            raise TooManyRequestsError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
783
        if _response.status_code == 500:
784
            raise InternalServerError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
785
        try:
786
            _response_json = _response.json()
787
        except JSONDecodeError:
788
            raise ApiError(status_code=_response.status_code, body=_response.text)
789
        raise ApiError(status_code=_response.status_code, body=_response_json)
790

791
    async def update(
792
        self,
793
        id: str,
794
        *,
795
        name: typing.Optional[str] = OMIT,
796
        url: typing.Optional[str] = OMIT,
797
        excludes: typing.Optional[typing.Sequence[str]] = OMIT,
798
        oauth: typing.Optional[CreateConnectorOAuth] = OMIT,
799
        active: typing.Optional[bool] = OMIT,
800
        continue_on_failure: typing.Optional[bool] = OMIT,
801
        service_auth: typing.Optional[CreateConnectorServiceAuth] = OMIT,
802
        request_options: typing.Optional[RequestOptions] = None,
803
    ) -> UpdateConnectorResponse:
804
        """
805
        Update a connector by ID. Omitted fields will not be updated. See ['Managing your Connector'](https://docs.cohere.com/docs/managing-your-connector) for more information.
806

807
        Parameters:
808
            - id: str. The ID of the connector to update.
809

810
            - name: typing.Optional[str]. A human-readable name for the connector.
811

812
            - url: typing.Optional[str]. The URL of the connector that will be used to search for documents.
813

814
            - excludes: typing.Optional[typing.Sequence[str]]. A list of fields to exclude from the prompt (fields remain in the document).
815

816
            - oauth: typing.Optional[CreateConnectorOAuth]. The OAuth 2.0 configuration for the connector. Cannot be specified if service_auth is specified.
817

818
            - active: typing.Optional[bool].
819

820
            - continue_on_failure: typing.Optional[bool].
821

822
            - service_auth: typing.Optional[CreateConnectorServiceAuth]. The service to service authentication configuration for the connector. Cannot be specified if oauth is specified.
823

824
            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
825
        ---
826
        from cohere.client import AsyncClient
827

828
        client = AsyncClient(
829
            client_name="YOUR_CLIENT_NAME",
830
            token="YOUR_TOKEN",
831
        )
832
        await client.connectors.update(
833
            id="id",
834
        )
835
        """
836
        _request: typing.Dict[str, typing.Any] = {}
837
        if name is not OMIT:
838
            _request["name"] = name
839
        if url is not OMIT:
840
            _request["url"] = url
841
        if excludes is not OMIT:
842
            _request["excludes"] = excludes
843
        if oauth is not OMIT:
844
            _request["oauth"] = oauth
845
        if active is not OMIT:
846
            _request["active"] = active
847
        if continue_on_failure is not OMIT:
848
            _request["continue_on_failure"] = continue_on_failure
849
        if service_auth is not OMIT:
850
            _request["service_auth"] = service_auth
851
        _response = await self._client_wrapper.httpx_client.request(
852
            "PATCH",
853
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", f"connectors/{jsonable_encoder(id)}"),
854
            params=jsonable_encoder(
855
                request_options.get("additional_query_parameters") if request_options is not None else None
856
            ),
857
            json=jsonable_encoder(_request)
858
            if request_options is None or request_options.get("additional_body_parameters") is None
859
            else {
860
                **jsonable_encoder(_request),
861
                **(jsonable_encoder(remove_none_from_dict(request_options.get("additional_body_parameters", {})))),
862
            },
863
            headers=jsonable_encoder(
864
                remove_none_from_dict(
865
                    {
866
                        **self._client_wrapper.get_headers(),
867
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
868
                    }
869
                )
870
            ),
871
            timeout=request_options.get("timeout_in_seconds")
872
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
873
            else self._client_wrapper.get_timeout(),
874
            retries=0,
875
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
876
        )
877
        if 200 <= _response.status_code < 300:
878
            return pydantic.parse_obj_as(UpdateConnectorResponse, _response.json())  # type: ignore
879
        if _response.status_code == 400:
880
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
881
        if _response.status_code == 403:
882
            raise ForbiddenError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
883
        if _response.status_code == 404:
884
            raise NotFoundError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
885
        if _response.status_code == 429:
886
            raise TooManyRequestsError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
887
        if _response.status_code == 500:
888
            raise InternalServerError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
889
        try:
890
            _response_json = _response.json()
891
        except JSONDecodeError:
892
            raise ApiError(status_code=_response.status_code, body=_response.text)
893
        raise ApiError(status_code=_response.status_code, body=_response_json)
894

895
    async def o_auth_authorize(
896
        self,
897
        id: str,
898
        *,
899
        after_token_redirect: typing.Optional[str] = None,
900
        request_options: typing.Optional[RequestOptions] = None,
901
    ) -> OAuthAuthorizeResponse:
902
        """
903
        Authorize the connector with the given ID for the connector oauth app. See ['Connector Authentication'](https://docs.cohere.com/docs/connector-authentication) for more information.
904

905
        Parameters:
906
            - id: str. The ID of the connector to authorize.
907

908
            - after_token_redirect: typing.Optional[str]. The URL to redirect to after the connector has been authorized.
909

910
            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
911
        ---
912
        from cohere.client import AsyncClient
913

914
        client = AsyncClient(
915
            client_name="YOUR_CLIENT_NAME",
916
            token="YOUR_TOKEN",
917
        )
918
        await client.connectors.o_auth_authorize(
919
            id="id",
920
        )
921
        """
922
        _response = await self._client_wrapper.httpx_client.request(
923
            "POST",
924
            urllib.parse.urljoin(
925
                f"{self._client_wrapper.get_base_url()}/", f"connectors/{jsonable_encoder(id)}/oauth/authorize"
926
            ),
927
            params=jsonable_encoder(
928
                remove_none_from_dict(
929
                    {
930
                        "after_token_redirect": after_token_redirect,
931
                        **(
932
                            request_options.get("additional_query_parameters", {})
933
                            if request_options is not None
934
                            else {}
935
                        ),
936
                    }
937
                )
938
            ),
939
            json=jsonable_encoder(remove_none_from_dict(request_options.get("additional_body_parameters", {})))
940
            if request_options is not None
941
            else None,
942
            headers=jsonable_encoder(
943
                remove_none_from_dict(
944
                    {
945
                        **self._client_wrapper.get_headers(),
946
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
947
                    }
948
                )
949
            ),
950
            timeout=request_options.get("timeout_in_seconds")
951
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
952
            else self._client_wrapper.get_timeout(),
953
            retries=0,
954
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
955
        )
956
        if 200 <= _response.status_code < 300:
957
            return pydantic.parse_obj_as(OAuthAuthorizeResponse, _response.json())  # type: ignore
958
        if _response.status_code == 400:
959
            raise BadRequestError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
960
        if _response.status_code == 404:
961
            raise NotFoundError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
962
        if _response.status_code == 429:
963
            raise TooManyRequestsError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
964
        if _response.status_code == 500:
965
            raise InternalServerError(pydantic.parse_obj_as(typing.Any, _response.json()))  # type: ignore
966
        try:
967
            _response_json = _response.json()
968
        except JSONDecodeError:
969
            raise ApiError(status_code=_response.status_code, body=_response.text)
970
        raise ApiError(status_code=_response.status_code, body=_response_json)
971

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.