Skip to content

Commit 46c8767

Browse files
chore: use lazy imports for resources
1 parent e1280b2 commit 46c8767

File tree

1 file changed

+253
-62
lines changed

1 file changed

+253
-62
lines changed

src/groq/_client.py

Lines changed: 253 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
import os
6-
from typing import Any, Union, Mapping
6+
from typing import TYPE_CHECKING, Any, Union, Mapping
77
from typing_extensions import Self, override
88

99
import httpx
@@ -20,31 +20,29 @@
2020
RequestOptions,
2121
)
2222
from ._utils import is_given, get_async_library
23+
from ._compat import cached_property
2324
from ._version import __version__
24-
from .resources import files, models, batches, embeddings
2525
from ._streaming import Stream as Stream, AsyncStream as AsyncStream
2626
from ._exceptions import GroqError, APIStatusError
2727
from ._base_client import (
2828
DEFAULT_MAX_RETRIES,
2929
SyncAPIClient,
3030
AsyncAPIClient,
3131
)
32-
from .resources.chat import chat
33-
from .resources.audio import audio
32+
33+
if TYPE_CHECKING:
34+
from .resources import chat, audio, files, models, batches, embeddings
35+
from .resources.files import Files, AsyncFiles
36+
from .resources.models import Models, AsyncModels
37+
from .resources.batches import Batches, AsyncBatches
38+
from .resources.chat.chat import Chat, AsyncChat
39+
from .resources.embeddings import Embeddings, AsyncEmbeddings
40+
from .resources.audio.audio import Audio, AsyncAudio
3441

3542
__all__ = ["Timeout", "Transport", "ProxiesTypes", "RequestOptions", "Groq", "AsyncGroq", "Client", "AsyncClient"]
3643

3744

3845
class Groq(SyncAPIClient):
39-
chat: chat.Chat
40-
embeddings: embeddings.Embeddings
41-
audio: audio.Audio
42-
models: models.Models
43-
batches: batches.Batches
44-
files: files.Files
45-
with_raw_response: GroqWithRawResponse
46-
with_streaming_response: GroqWithStreamedResponse
47-
4846
# client options
4947
api_key: str
5048

@@ -99,14 +97,49 @@ def __init__(
9997
_strict_response_validation=_strict_response_validation,
10098
)
10199

102-
self.chat = chat.Chat(self)
103-
self.embeddings = embeddings.Embeddings(self)
104-
self.audio = audio.Audio(self)
105-
self.models = models.Models(self)
106-
self.batches = batches.Batches(self)
107-
self.files = files.Files(self)
108-
self.with_raw_response = GroqWithRawResponse(self)
109-
self.with_streaming_response = GroqWithStreamedResponse(self)
100+
@cached_property
101+
def chat(self) -> Chat:
102+
from .resources.chat import Chat
103+
104+
return Chat(self)
105+
106+
@cached_property
107+
def embeddings(self) -> Embeddings:
108+
from .resources.embeddings import Embeddings
109+
110+
return Embeddings(self)
111+
112+
@cached_property
113+
def audio(self) -> Audio:
114+
from .resources.audio import Audio
115+
116+
return Audio(self)
117+
118+
@cached_property
119+
def models(self) -> Models:
120+
from .resources.models import Models
121+
122+
return Models(self)
123+
124+
@cached_property
125+
def batches(self) -> Batches:
126+
from .resources.batches import Batches
127+
128+
return Batches(self)
129+
130+
@cached_property
131+
def files(self) -> Files:
132+
from .resources.files import Files
133+
134+
return Files(self)
135+
136+
@cached_property
137+
def with_raw_response(self) -> GroqWithRawResponse:
138+
return GroqWithRawResponse(self)
139+
140+
@cached_property
141+
def with_streaming_response(self) -> GroqWithStreamedResponse:
142+
return GroqWithStreamedResponse(self)
110143

111144
@property
112145
@override
@@ -214,15 +247,6 @@ def _make_status_error(
214247

215248

216249
class AsyncGroq(AsyncAPIClient):
217-
chat: chat.AsyncChat
218-
embeddings: embeddings.AsyncEmbeddings
219-
audio: audio.AsyncAudio
220-
models: models.AsyncModels
221-
batches: batches.AsyncBatches
222-
files: files.AsyncFiles
223-
with_raw_response: AsyncGroqWithRawResponse
224-
with_streaming_response: AsyncGroqWithStreamedResponse
225-
226250
# client options
227251
api_key: str
228252

@@ -277,14 +301,49 @@ def __init__(
277301
_strict_response_validation=_strict_response_validation,
278302
)
279303

280-
self.chat = chat.AsyncChat(self)
281-
self.embeddings = embeddings.AsyncEmbeddings(self)
282-
self.audio = audio.AsyncAudio(self)
283-
self.models = models.AsyncModels(self)
284-
self.batches = batches.AsyncBatches(self)
285-
self.files = files.AsyncFiles(self)
286-
self.with_raw_response = AsyncGroqWithRawResponse(self)
287-
self.with_streaming_response = AsyncGroqWithStreamedResponse(self)
304+
@cached_property
305+
def chat(self) -> AsyncChat:
306+
from .resources.chat import AsyncChat
307+
308+
return AsyncChat(self)
309+
310+
@cached_property
311+
def embeddings(self) -> AsyncEmbeddings:
312+
from .resources.embeddings import AsyncEmbeddings
313+
314+
return AsyncEmbeddings(self)
315+
316+
@cached_property
317+
def audio(self) -> AsyncAudio:
318+
from .resources.audio import AsyncAudio
319+
320+
return AsyncAudio(self)
321+
322+
@cached_property
323+
def models(self) -> AsyncModels:
324+
from .resources.models import AsyncModels
325+
326+
return AsyncModels(self)
327+
328+
@cached_property
329+
def batches(self) -> AsyncBatches:
330+
from .resources.batches import AsyncBatches
331+
332+
return AsyncBatches(self)
333+
334+
@cached_property
335+
def files(self) -> AsyncFiles:
336+
from .resources.files import AsyncFiles
337+
338+
return AsyncFiles(self)
339+
340+
@cached_property
341+
def with_raw_response(self) -> AsyncGroqWithRawResponse:
342+
return AsyncGroqWithRawResponse(self)
343+
344+
@cached_property
345+
def with_streaming_response(self) -> AsyncGroqWithStreamedResponse:
346+
return AsyncGroqWithStreamedResponse(self)
288347

289348
@property
290349
@override
@@ -392,43 +451,175 @@ def _make_status_error(
392451

393452

394453
class GroqWithRawResponse:
454+
_client: Groq
455+
395456
def __init__(self, client: Groq) -> None:
396-
self.chat = chat.ChatWithRawResponse(client.chat)
397-
self.embeddings = embeddings.EmbeddingsWithRawResponse(client.embeddings)
398-
self.audio = audio.AudioWithRawResponse(client.audio)
399-
self.models = models.ModelsWithRawResponse(client.models)
400-
self.batches = batches.BatchesWithRawResponse(client.batches)
401-
self.files = files.FilesWithRawResponse(client.files)
457+
self._client = client
458+
459+
@cached_property
460+
def chat(self) -> chat.ChatWithRawResponse:
461+
from .resources.chat import ChatWithRawResponse
462+
463+
return ChatWithRawResponse(self._client.chat)
464+
465+
@cached_property
466+
def embeddings(self) -> embeddings.EmbeddingsWithRawResponse:
467+
from .resources.embeddings import EmbeddingsWithRawResponse
468+
469+
return EmbeddingsWithRawResponse(self._client.embeddings)
470+
471+
@cached_property
472+
def audio(self) -> audio.AudioWithRawResponse:
473+
from .resources.audio import AudioWithRawResponse
474+
475+
return AudioWithRawResponse(self._client.audio)
476+
477+
@cached_property
478+
def models(self) -> models.ModelsWithRawResponse:
479+
from .resources.models import ModelsWithRawResponse
480+
481+
return ModelsWithRawResponse(self._client.models)
482+
483+
@cached_property
484+
def batches(self) -> batches.BatchesWithRawResponse:
485+
from .resources.batches import BatchesWithRawResponse
486+
487+
return BatchesWithRawResponse(self._client.batches)
488+
489+
@cached_property
490+
def files(self) -> files.FilesWithRawResponse:
491+
from .resources.files import FilesWithRawResponse
492+
493+
return FilesWithRawResponse(self._client.files)
402494

403495

404496
class AsyncGroqWithRawResponse:
497+
_client: AsyncGroq
498+
405499
def __init__(self, client: AsyncGroq) -> None:
406-
self.chat = chat.AsyncChatWithRawResponse(client.chat)
407-
self.embeddings = embeddings.AsyncEmbeddingsWithRawResponse(client.embeddings)
408-
self.audio = audio.AsyncAudioWithRawResponse(client.audio)
409-
self.models = models.AsyncModelsWithRawResponse(client.models)
410-
self.batches = batches.AsyncBatchesWithRawResponse(client.batches)
411-
self.files = files.AsyncFilesWithRawResponse(client.files)
500+
self._client = client
501+
502+
@cached_property
503+
def chat(self) -> chat.AsyncChatWithRawResponse:
504+
from .resources.chat import AsyncChatWithRawResponse
505+
506+
return AsyncChatWithRawResponse(self._client.chat)
507+
508+
@cached_property
509+
def embeddings(self) -> embeddings.AsyncEmbeddingsWithRawResponse:
510+
from .resources.embeddings import AsyncEmbeddingsWithRawResponse
511+
512+
return AsyncEmbeddingsWithRawResponse(self._client.embeddings)
513+
514+
@cached_property
515+
def audio(self) -> audio.AsyncAudioWithRawResponse:
516+
from .resources.audio import AsyncAudioWithRawResponse
517+
518+
return AsyncAudioWithRawResponse(self._client.audio)
519+
520+
@cached_property
521+
def models(self) -> models.AsyncModelsWithRawResponse:
522+
from .resources.models import AsyncModelsWithRawResponse
523+
524+
return AsyncModelsWithRawResponse(self._client.models)
525+
526+
@cached_property
527+
def batches(self) -> batches.AsyncBatchesWithRawResponse:
528+
from .resources.batches import AsyncBatchesWithRawResponse
529+
530+
return AsyncBatchesWithRawResponse(self._client.batches)
531+
532+
@cached_property
533+
def files(self) -> files.AsyncFilesWithRawResponse:
534+
from .resources.files import AsyncFilesWithRawResponse
535+
536+
return AsyncFilesWithRawResponse(self._client.files)
412537

413538

414539
class GroqWithStreamedResponse:
540+
_client: Groq
541+
415542
def __init__(self, client: Groq) -> None:
416-
self.chat = chat.ChatWithStreamingResponse(client.chat)
417-
self.embeddings = embeddings.EmbeddingsWithStreamingResponse(client.embeddings)
418-
self.audio = audio.AudioWithStreamingResponse(client.audio)
419-
self.models = models.ModelsWithStreamingResponse(client.models)
420-
self.batches = batches.BatchesWithStreamingResponse(client.batches)
421-
self.files = files.FilesWithStreamingResponse(client.files)
543+
self._client = client
544+
545+
@cached_property
546+
def chat(self) -> chat.ChatWithStreamingResponse:
547+
from .resources.chat import ChatWithStreamingResponse
548+
549+
return ChatWithStreamingResponse(self._client.chat)
550+
551+
@cached_property
552+
def embeddings(self) -> embeddings.EmbeddingsWithStreamingResponse:
553+
from .resources.embeddings import EmbeddingsWithStreamingResponse
554+
555+
return EmbeddingsWithStreamingResponse(self._client.embeddings)
556+
557+
@cached_property
558+
def audio(self) -> audio.AudioWithStreamingResponse:
559+
from .resources.audio import AudioWithStreamingResponse
560+
561+
return AudioWithStreamingResponse(self._client.audio)
562+
563+
@cached_property
564+
def models(self) -> models.ModelsWithStreamingResponse:
565+
from .resources.models import ModelsWithStreamingResponse
566+
567+
return ModelsWithStreamingResponse(self._client.models)
568+
569+
@cached_property
570+
def batches(self) -> batches.BatchesWithStreamingResponse:
571+
from .resources.batches import BatchesWithStreamingResponse
572+
573+
return BatchesWithStreamingResponse(self._client.batches)
574+
575+
@cached_property
576+
def files(self) -> files.FilesWithStreamingResponse:
577+
from .resources.files import FilesWithStreamingResponse
578+
579+
return FilesWithStreamingResponse(self._client.files)
422580

423581

424582
class AsyncGroqWithStreamedResponse:
583+
_client: AsyncGroq
584+
425585
def __init__(self, client: AsyncGroq) -> None:
426-
self.chat = chat.AsyncChatWithStreamingResponse(client.chat)
427-
self.embeddings = embeddings.AsyncEmbeddingsWithStreamingResponse(client.embeddings)
428-
self.audio = audio.AsyncAudioWithStreamingResponse(client.audio)
429-
self.models = models.AsyncModelsWithStreamingResponse(client.models)
430-
self.batches = batches.AsyncBatchesWithStreamingResponse(client.batches)
431-
self.files = files.AsyncFilesWithStreamingResponse(client.files)
586+
self._client = client
587+
588+
@cached_property
589+
def chat(self) -> chat.AsyncChatWithStreamingResponse:
590+
from .resources.chat import AsyncChatWithStreamingResponse
591+
592+
return AsyncChatWithStreamingResponse(self._client.chat)
593+
594+
@cached_property
595+
def embeddings(self) -> embeddings.AsyncEmbeddingsWithStreamingResponse:
596+
from .resources.embeddings import AsyncEmbeddingsWithStreamingResponse
597+
598+
return AsyncEmbeddingsWithStreamingResponse(self._client.embeddings)
599+
600+
@cached_property
601+
def audio(self) -> audio.AsyncAudioWithStreamingResponse:
602+
from .resources.audio import AsyncAudioWithStreamingResponse
603+
604+
return AsyncAudioWithStreamingResponse(self._client.audio)
605+
606+
@cached_property
607+
def models(self) -> models.AsyncModelsWithStreamingResponse:
608+
from .resources.models import AsyncModelsWithStreamingResponse
609+
610+
return AsyncModelsWithStreamingResponse(self._client.models)
611+
612+
@cached_property
613+
def batches(self) -> batches.AsyncBatchesWithStreamingResponse:
614+
from .resources.batches import AsyncBatchesWithStreamingResponse
615+
616+
return AsyncBatchesWithStreamingResponse(self._client.batches)
617+
618+
@cached_property
619+
def files(self) -> files.AsyncFilesWithStreamingResponse:
620+
from .resources.files import AsyncFilesWithStreamingResponse
621+
622+
return AsyncFilesWithStreamingResponse(self._client.files)
432623

433624

434625
Client = Groq

0 commit comments

Comments
 (0)