|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import os |
6 | | -from typing import Any, Mapping |
| 6 | +from typing import TYPE_CHECKING, Any, Mapping |
7 | 7 | from typing_extensions import Self, override |
8 | 8 |
|
9 | 9 | import httpx |
|
20 | 20 | not_given, |
21 | 21 | ) |
22 | 22 | from ._utils import is_given, get_async_library |
| 23 | +from ._compat import cached_property |
23 | 24 | from ._version import __version__ |
24 | | -from .resources import search |
25 | 25 | from ._streaming import Stream as Stream, AsyncStream as AsyncStream |
26 | 26 | from ._exceptions import APIStatusError, PerplexityError |
27 | 27 | from ._base_client import ( |
28 | 28 | DEFAULT_MAX_RETRIES, |
29 | 29 | SyncAPIClient, |
30 | 30 | AsyncAPIClient, |
31 | 31 | ) |
32 | | -from .resources.chat import chat |
33 | | -from .resources.async_ import async_ |
| 32 | + |
| 33 | +if TYPE_CHECKING: |
| 34 | + from .resources import chat, async_, search |
| 35 | + from .resources.search import SearchResource, AsyncSearchResource |
| 36 | + from .resources.chat.chat import ChatResource, AsyncChatResource |
| 37 | + from .resources.async_.async_ import AsyncResource, AsyncAsyncResource |
34 | 38 |
|
35 | 39 | __all__ = [ |
36 | 40 | "Timeout", |
|
45 | 49 |
|
46 | 50 |
|
47 | 51 | class Perplexity(SyncAPIClient): |
48 | | - chat: chat.ChatResource |
49 | | - search: search.SearchResource |
50 | | - async_: async_.AsyncResource |
51 | | - with_raw_response: PerplexityWithRawResponse |
52 | | - with_streaming_response: PerplexityWithStreamedResponse |
53 | | - |
54 | 52 | # client options |
55 | 53 | api_key: str |
56 | 54 |
|
@@ -107,11 +105,31 @@ def __init__( |
107 | 105 |
|
108 | 106 | self._default_stream_cls = Stream |
109 | 107 |
|
110 | | - self.chat = chat.ChatResource(self) |
111 | | - self.search = search.SearchResource(self) |
112 | | - self.async_ = async_.AsyncResource(self) |
113 | | - self.with_raw_response = PerplexityWithRawResponse(self) |
114 | | - self.with_streaming_response = PerplexityWithStreamedResponse(self) |
| 108 | + @cached_property |
| 109 | + def chat(self) -> ChatResource: |
| 110 | + from .resources.chat import ChatResource |
| 111 | + |
| 112 | + return ChatResource(self) |
| 113 | + |
| 114 | + @cached_property |
| 115 | + def search(self) -> SearchResource: |
| 116 | + from .resources.search import SearchResource |
| 117 | + |
| 118 | + return SearchResource(self) |
| 119 | + |
| 120 | + @cached_property |
| 121 | + def async_(self) -> AsyncResource: |
| 122 | + from .resources.async_ import AsyncResource |
| 123 | + |
| 124 | + return AsyncResource(self) |
| 125 | + |
| 126 | + @cached_property |
| 127 | + def with_raw_response(self) -> PerplexityWithRawResponse: |
| 128 | + return PerplexityWithRawResponse(self) |
| 129 | + |
| 130 | + @cached_property |
| 131 | + def with_streaming_response(self) -> PerplexityWithStreamedResponse: |
| 132 | + return PerplexityWithStreamedResponse(self) |
115 | 133 |
|
116 | 134 | @property |
117 | 135 | @override |
@@ -219,12 +237,6 @@ def _make_status_error( |
219 | 237 |
|
220 | 238 |
|
221 | 239 | class AsyncPerplexity(AsyncAPIClient): |
222 | | - chat: chat.AsyncChatResource |
223 | | - search: search.AsyncSearchResource |
224 | | - async_: async_.AsyncAsyncResource |
225 | | - with_raw_response: AsyncPerplexityWithRawResponse |
226 | | - with_streaming_response: AsyncPerplexityWithStreamedResponse |
227 | | - |
228 | 240 | # client options |
229 | 241 | api_key: str |
230 | 242 |
|
@@ -281,11 +293,31 @@ def __init__( |
281 | 293 |
|
282 | 294 | self._default_stream_cls = AsyncStream |
283 | 295 |
|
284 | | - self.chat = chat.AsyncChatResource(self) |
285 | | - self.search = search.AsyncSearchResource(self) |
286 | | - self.async_ = async_.AsyncAsyncResource(self) |
287 | | - self.with_raw_response = AsyncPerplexityWithRawResponse(self) |
288 | | - self.with_streaming_response = AsyncPerplexityWithStreamedResponse(self) |
| 296 | + @cached_property |
| 297 | + def chat(self) -> AsyncChatResource: |
| 298 | + from .resources.chat import AsyncChatResource |
| 299 | + |
| 300 | + return AsyncChatResource(self) |
| 301 | + |
| 302 | + @cached_property |
| 303 | + def search(self) -> AsyncSearchResource: |
| 304 | + from .resources.search import AsyncSearchResource |
| 305 | + |
| 306 | + return AsyncSearchResource(self) |
| 307 | + |
| 308 | + @cached_property |
| 309 | + def async_(self) -> AsyncAsyncResource: |
| 310 | + from .resources.async_ import AsyncAsyncResource |
| 311 | + |
| 312 | + return AsyncAsyncResource(self) |
| 313 | + |
| 314 | + @cached_property |
| 315 | + def with_raw_response(self) -> AsyncPerplexityWithRawResponse: |
| 316 | + return AsyncPerplexityWithRawResponse(self) |
| 317 | + |
| 318 | + @cached_property |
| 319 | + def with_streaming_response(self) -> AsyncPerplexityWithStreamedResponse: |
| 320 | + return AsyncPerplexityWithStreamedResponse(self) |
289 | 321 |
|
290 | 322 | @property |
291 | 323 | @override |
@@ -393,31 +425,103 @@ def _make_status_error( |
393 | 425 |
|
394 | 426 |
|
395 | 427 | class PerplexityWithRawResponse: |
| 428 | + _client: Perplexity |
| 429 | + |
396 | 430 | def __init__(self, client: Perplexity) -> None: |
397 | | - self.chat = chat.ChatResourceWithRawResponse(client.chat) |
398 | | - self.search = search.SearchResourceWithRawResponse(client.search) |
399 | | - self.async_ = async_.AsyncResourceWithRawResponse(client.async_) |
| 431 | + self._client = client |
| 432 | + |
| 433 | + @cached_property |
| 434 | + def chat(self) -> chat.ChatResourceWithRawResponse: |
| 435 | + from .resources.chat import ChatResourceWithRawResponse |
| 436 | + |
| 437 | + return ChatResourceWithRawResponse(self._client.chat) |
| 438 | + |
| 439 | + @cached_property |
| 440 | + def search(self) -> search.SearchResourceWithRawResponse: |
| 441 | + from .resources.search import SearchResourceWithRawResponse |
| 442 | + |
| 443 | + return SearchResourceWithRawResponse(self._client.search) |
| 444 | + |
| 445 | + @cached_property |
| 446 | + def async_(self) -> async_.AsyncResourceWithRawResponse: |
| 447 | + from .resources.async_ import AsyncResourceWithRawResponse |
| 448 | + |
| 449 | + return AsyncResourceWithRawResponse(self._client.async_) |
400 | 450 |
|
401 | 451 |
|
402 | 452 | class AsyncPerplexityWithRawResponse: |
| 453 | + _client: AsyncPerplexity |
| 454 | + |
403 | 455 | def __init__(self, client: AsyncPerplexity) -> None: |
404 | | - self.chat = chat.AsyncChatResourceWithRawResponse(client.chat) |
405 | | - self.search = search.AsyncSearchResourceWithRawResponse(client.search) |
406 | | - self.async_ = async_.AsyncAsyncResourceWithRawResponse(client.async_) |
| 456 | + self._client = client |
| 457 | + |
| 458 | + @cached_property |
| 459 | + def chat(self) -> chat.AsyncChatResourceWithRawResponse: |
| 460 | + from .resources.chat import AsyncChatResourceWithRawResponse |
| 461 | + |
| 462 | + return AsyncChatResourceWithRawResponse(self._client.chat) |
| 463 | + |
| 464 | + @cached_property |
| 465 | + def search(self) -> search.AsyncSearchResourceWithRawResponse: |
| 466 | + from .resources.search import AsyncSearchResourceWithRawResponse |
| 467 | + |
| 468 | + return AsyncSearchResourceWithRawResponse(self._client.search) |
| 469 | + |
| 470 | + @cached_property |
| 471 | + def async_(self) -> async_.AsyncAsyncResourceWithRawResponse: |
| 472 | + from .resources.async_ import AsyncAsyncResourceWithRawResponse |
| 473 | + |
| 474 | + return AsyncAsyncResourceWithRawResponse(self._client.async_) |
407 | 475 |
|
408 | 476 |
|
409 | 477 | class PerplexityWithStreamedResponse: |
| 478 | + _client: Perplexity |
| 479 | + |
410 | 480 | def __init__(self, client: Perplexity) -> None: |
411 | | - self.chat = chat.ChatResourceWithStreamingResponse(client.chat) |
412 | | - self.search = search.SearchResourceWithStreamingResponse(client.search) |
413 | | - self.async_ = async_.AsyncResourceWithStreamingResponse(client.async_) |
| 481 | + self._client = client |
| 482 | + |
| 483 | + @cached_property |
| 484 | + def chat(self) -> chat.ChatResourceWithStreamingResponse: |
| 485 | + from .resources.chat import ChatResourceWithStreamingResponse |
| 486 | + |
| 487 | + return ChatResourceWithStreamingResponse(self._client.chat) |
| 488 | + |
| 489 | + @cached_property |
| 490 | + def search(self) -> search.SearchResourceWithStreamingResponse: |
| 491 | + from .resources.search import SearchResourceWithStreamingResponse |
| 492 | + |
| 493 | + return SearchResourceWithStreamingResponse(self._client.search) |
| 494 | + |
| 495 | + @cached_property |
| 496 | + def async_(self) -> async_.AsyncResourceWithStreamingResponse: |
| 497 | + from .resources.async_ import AsyncResourceWithStreamingResponse |
| 498 | + |
| 499 | + return AsyncResourceWithStreamingResponse(self._client.async_) |
414 | 500 |
|
415 | 501 |
|
416 | 502 | class AsyncPerplexityWithStreamedResponse: |
| 503 | + _client: AsyncPerplexity |
| 504 | + |
417 | 505 | def __init__(self, client: AsyncPerplexity) -> None: |
418 | | - self.chat = chat.AsyncChatResourceWithStreamingResponse(client.chat) |
419 | | - self.search = search.AsyncSearchResourceWithStreamingResponse(client.search) |
420 | | - self.async_ = async_.AsyncAsyncResourceWithStreamingResponse(client.async_) |
| 506 | + self._client = client |
| 507 | + |
| 508 | + @cached_property |
| 509 | + def chat(self) -> chat.AsyncChatResourceWithStreamingResponse: |
| 510 | + from .resources.chat import AsyncChatResourceWithStreamingResponse |
| 511 | + |
| 512 | + return AsyncChatResourceWithStreamingResponse(self._client.chat) |
| 513 | + |
| 514 | + @cached_property |
| 515 | + def search(self) -> search.AsyncSearchResourceWithStreamingResponse: |
| 516 | + from .resources.search import AsyncSearchResourceWithStreamingResponse |
| 517 | + |
| 518 | + return AsyncSearchResourceWithStreamingResponse(self._client.search) |
| 519 | + |
| 520 | + @cached_property |
| 521 | + def async_(self) -> async_.AsyncAsyncResourceWithStreamingResponse: |
| 522 | + from .resources.async_ import AsyncAsyncResourceWithStreamingResponse |
| 523 | + |
| 524 | + return AsyncAsyncResourceWithStreamingResponse(self._client.async_) |
421 | 525 |
|
422 | 526 |
|
423 | 527 | Client = Perplexity |
|
0 commit comments