Skip to content

Commit c1f5b5e

Browse files
committed
fix: Corrige erro de método getOptions inexistente e melhora validação da API key
1 parent bd1a267 commit c1f5b5e

File tree

2 files changed

+58
-108
lines changed

2 files changed

+58
-108
lines changed

src/Facades/Groq.php

Lines changed: 1 addition & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
namespace LucianoTonet\GroqLaravel\Facades;
44

55
use Illuminate\Support\Facades\Facade;
6-
use LucianoTonet\GroqPHP\Groq as GroqPHP;
7-
use LucianoTonet\GroqPHP\GroqException;
8-
use Groq\Resources\Chat\ChatCompletionResponse;
9-
use Groq\Resources\Completions\CompletionResponse;
10-
use Groq\Resources\Models\ModelsResponse;
116

127
/**
138
* @method static \LucianoTonet\GroqPHP\Chat chat()
@@ -17,6 +12,7 @@
1712
* @method static \LucianoTonet\GroqPHP\Files files()
1813
* @method static \LucianoTonet\GroqPHP\Batches batches()
1914
* @method static void setConfig(array $options)
15+
* @method static void setOptions(array $options)
2016
* @method static \LucianoTonet\GroqPHP\Groq getClient()
2117
*
2218
* @see \LucianoTonet\GroqLaravel\GroqClient
@@ -32,97 +28,4 @@ protected static function getFacadeAccessor(): string
3228
{
3329
return 'groq';
3430
}
35-
36-
/**
37-
* Initiate a chat session with the Groq API.
38-
*
39-
* @return \LucianoTonet\GroqPHP\Chat An instance of the Chat class for managing chat sessions.
40-
* @throws GroqException
41-
*/
42-
public static function chat(): \LucianoTonet\GroqPHP\Chat
43-
{
44-
return app(GroqPHP::class)->chat();
45-
}
46-
47-
/**
48-
* Initiate an audio session with the Groq API.
49-
*
50-
* @return \LucianoTonet\GroqPHP\Audio An instance of the Audio class for managing audio sessions.
51-
* @throws GroqException
52-
*/
53-
public static function audio(): \LucianoTonet\GroqPHP\Audio
54-
{
55-
return app(GroqPHP::class)->audio();
56-
}
57-
58-
/**
59-
* Retrieve the list of available models from the Groq API.
60-
*
61-
* @return \LucianoTonet\GroqPHP\Models An instance of the Models class containing available models.
62-
* @throws GroqException
63-
*/
64-
public static function models(): \LucianoTonet\GroqPHP\Models
65-
{
66-
return app(GroqPHP::class)->models();
67-
}
68-
69-
/**
70-
* Set configuration options for the Groq instance.
71-
*
72-
* @param array $options An associative array of options to configure the Groq instance.
73-
* @return void
74-
* @throws GroqException
75-
*/
76-
public static function setOptions(array $options): void
77-
{
78-
$instance = app(GroqPHP::class);
79-
80-
// If apiKey is present in options, update the instance
81-
if (isset($options['apiKey'])) {
82-
app()->forgetInstance(GroqPHP::class);
83-
app()->instance(GroqPHP::class, new GroqPHP(
84-
$options['apiKey'],
85-
array_merge(
86-
['baseUrl' => config('groq.api_base', 'https://api.groq.com/openai/v1')],
87-
$options
88-
)
89-
));
90-
return;
91-
}
92-
93-
// If baseUrl is present in options, update the instance
94-
if (isset($options['baseUrl'])) {
95-
app()->forgetInstance(GroqPHP::class);
96-
app()->instance(GroqPHP::class, new GroqPHP(
97-
$instance->getOptions()['apiKey'],
98-
$options
99-
));
100-
return;
101-
}
102-
103-
// If no apiKey or baseUrl, just update the existing options
104-
$instance->setOptions($options);
105-
}
106-
107-
/**
108-
* Get configuration options from the Groq instance.
109-
*
110-
* @return array The current configuration options
111-
* @throws GroqException
112-
*/
113-
public static function getOptions(): array
114-
{
115-
return app(GroqPHP::class)->getOptions();
116-
}
117-
118-
/**
119-
* Iniciar uma sessão de visão com a API Groq.
120-
*
121-
* @return \LucianoTonet\GroqPHP\Vision Uma instância da classe Vision para gerenciar sessões de visão.
122-
* @throws GroqException
123-
*/
124-
public static function vision(): \LucianoTonet\GroqPHP\Vision
125-
{
126-
return app(GroqPHP::class)->vision();
127-
}
12831
}

src/GroqClient.php

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,59 @@
33
namespace LucianoTonet\GroqLaravel;
44

55
use LucianoTonet\GroqPHP\Groq;
6+
use LucianoTonet\GroqPHP\GroqException;
67

78
class GroqClient
89
{
10+
/**
11+
* The Groq PHP SDK client instance.
12+
*
13+
* @var \LucianoTonet\GroqPHP\Groq
14+
*/
915
protected Groq $client;
1016

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+
*/
1128
public function __construct(?string $apiKey = null)
1229
{
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+
}
1446

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 [
1659
'baseUrl' => config('groq.api_base', 'https://api.groq.com/openai/v1'),
1760
'timeout' => config('groq.timeout', 30),
1861
'model' => config('groq.model', 'llama3-8b-8192'),
@@ -21,7 +64,7 @@ public function __construct(?string $apiKey = null)
2164
'top_p' => config('groq.options.top_p', 1.0),
2265
'frequency_penalty' => config('groq.options.frequency_penalty', 0),
2366
'presence_penalty' => config('groq.options.presence_penalty', 0),
24-
]);
67+
];
2568
}
2669

2770
/**
@@ -67,7 +110,7 @@ public function audio()
67110
/**
68111
* Get files instance
69112
*
70-
* @return \LucianoTonet\GroqPHP\Files
113+
* @return \LucianoTonet\GroqPHP\FileManager
71114
*/
72115
public function files()
73116
{
@@ -77,7 +120,7 @@ public function files()
77120
/**
78121
* Get batches instance
79122
*
80-
* @return \LucianoTonet\GroqPHP\Batches
123+
* @return \LucianoTonet\GroqPHP\BatchManager
81124
*/
82125
public function batches()
83126
{
@@ -92,17 +135,21 @@ public function batches()
92135
*/
93136
public function setConfig(array $options): void
94137
{
95-
$this->client->setOptions($options);
138+
$this->client = new Groq(
139+
$this->client->apiKey,
140+
array_merge($this->getDefaultConfig(), $options)
141+
);
96142
}
97143

98144
/**
99-
* Get configuration options
145+
* Set configuration options (alias for setConfig)
100146
*
101-
* @return array
147+
* @param array $options
148+
* @return void
102149
*/
103-
public function getConfig(): array
150+
public function setOptions(array $options): void
104151
{
105-
return $this->client->getOptions();
152+
$this->setConfig($options);
106153
}
107154

108155
/**

0 commit comments

Comments
 (0)