3
3
namespace LucianoTonet \GroqLaravel ;
4
4
5
5
use LucianoTonet \GroqPHP \Groq ;
6
+ use LucianoTonet \GroqPHP \GroqException ;
6
7
7
8
class GroqClient
8
9
{
10
+ /**
11
+ * The Groq PHP SDK client instance.
12
+ *
13
+ * @var \LucianoTonet\GroqPHP\Groq
14
+ */
9
15
protected Groq $ client ;
10
16
17
+ /**
18
+ * Create a new GroqClient instance.
19
+ *
20
+ * Priority for API key:
21
+ * 1. Constructor parameter
22
+ * 2. Environment variable (GROQ_API_KEY)
23
+ * 3. Config file (config/groq.php)
24
+ *
25
+ * @param string|null $apiKey Optional API key to override the one in config
26
+ * @throws \LucianoTonet\GroqPHP\GroqException
27
+ */
11
28
public function __construct (?string $ apiKey = null )
12
29
{
13
- $ apiKey = $ apiKey ?? config ('groq.api_key ' );
30
+ // Try to get API key from different sources in order of priority
31
+ $ apiKey = $ apiKey
32
+ ?? env ('GROQ_API_KEY ' )
33
+ ?? config ('groq.api_key ' );
34
+
35
+ // Validate API key
36
+ if (empty ($ apiKey )) {
37
+ throw new GroqException (
38
+ 'No API key found. Please provide it via: ' . PHP_EOL .
39
+ '1. Constructor parameter ' . PHP_EOL .
40
+ '2. Environment variable (GROQ_API_KEY) ' . PHP_EOL .
41
+ '3. Config file (config/groq.php) ' ,
42
+ GroqException::CODE_INVALID_REQUEST ,
43
+ GroqException::TYPE_INVALID_REQUEST ,
44
+ );
45
+ }
14
46
15
- $ this ->client = new Groq ($ apiKey , [
47
+ // Initialize client with API key and default config
48
+ $ this ->client = new Groq ($ apiKey , $ this ->getDefaultConfig ());
49
+ }
50
+
51
+ /**
52
+ * Get the default configuration from Laravel config.
53
+ *
54
+ * @return array
55
+ */
56
+ protected function getDefaultConfig (): array
57
+ {
58
+ return [
16
59
'baseUrl ' => config ('groq.api_base ' , 'https://api.groq.com/openai/v1 ' ),
17
60
'timeout ' => config ('groq.timeout ' , 30 ),
18
61
'model ' => config ('groq.model ' , 'llama3-8b-8192 ' ),
@@ -21,7 +64,7 @@ public function __construct(?string $apiKey = null)
21
64
'top_p ' => config ('groq.options.top_p ' , 1.0 ),
22
65
'frequency_penalty ' => config ('groq.options.frequency_penalty ' , 0 ),
23
66
'presence_penalty ' => config ('groq.options.presence_penalty ' , 0 ),
24
- ]) ;
67
+ ];
25
68
}
26
69
27
70
/**
@@ -67,7 +110,7 @@ public function audio()
67
110
/**
68
111
* Get files instance
69
112
*
70
- * @return \LucianoTonet\GroqPHP\Files
113
+ * @return \LucianoTonet\GroqPHP\FileManager
71
114
*/
72
115
public function files ()
73
116
{
@@ -77,7 +120,7 @@ public function files()
77
120
/**
78
121
* Get batches instance
79
122
*
80
- * @return \LucianoTonet\GroqPHP\Batches
123
+ * @return \LucianoTonet\GroqPHP\BatchManager
81
124
*/
82
125
public function batches ()
83
126
{
@@ -92,17 +135,21 @@ public function batches()
92
135
*/
93
136
public function setConfig (array $ options ): void
94
137
{
95
- $ this ->client ->setOptions ($ options );
138
+ $ this ->client = new Groq (
139
+ $ this ->client ->apiKey ,
140
+ array_merge ($ this ->getDefaultConfig (), $ options )
141
+ );
96
142
}
97
143
98
144
/**
99
- * Get configuration options
145
+ * Set configuration options (alias for setConfig)
100
146
*
101
- * @return array
147
+ * @param array $options
148
+ * @return void
102
149
*/
103
- public function getConfig ( ): array
150
+ public function setOptions ( array $ options ): void
104
151
{
105
- return $ this ->client -> getOptions ( );
152
+ $ this ->setConfig ( $ options );
106
153
}
107
154
108
155
/**
0 commit comments