Skip to content

Commit b2d31be

Browse files
committed
refactor: Implement CodeRabbit suggestions and add project rules
1 parent c1f5b5e commit b2d31be

File tree

5 files changed

+83
-21
lines changed

5 files changed

+83
-21
lines changed

.cursor/rules/codebase.mdc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

.cursorrules

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Groq Laravel Project Rules
2+
3+
## Project Info
4+
- Package: Laravel integration for GroqCloud API
5+
- Uses PHP 8.1+ / Laravel support
6+
- Composer package: lucianotonet/groq-laravel
7+
- Base dependency: lucianotonet/groq-php
8+
9+
## Key Guidelines
10+
11+
### Code Style
12+
- Use English for all code and comments
13+
- Follow PSR-12 coding standards
14+
- Properly document all public methods with PHPDoc
15+
16+
### Models
17+
- Default model: 'llama-3.1-8b-instant' (standardized throughout the codebase)
18+
- Model name should be consistent in config and code
19+
20+
### Configuration
21+
- Config priorities:
22+
1. Runtime options (setConfig/setOptions)
23+
2. Environment variables (.env)
24+
3. Default values in config file
25+
26+
### Testing
27+
- Integration tests should not rely on external resources (URLs, APIs)
28+
- Mock API responses when possible
29+
- Use base64 encoded test images instead of external URLs
30+
- Tests should have descriptive names and assertions
31+
32+
### Error Handling
33+
- Validate API key early with clear error messages
34+
- Use proper exception types from GroqException
35+
36+
### Important Learnings
37+
- GroqClient should preserve previously set options when calling setConfig()
38+
- The Facade is just a proxy to the underlying client instance
39+
- Avoid inconsistencies between default values in different parts of code

config/groq.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
| da API Groq. Esse modelo será usado se nenhum for especificado na requisição.
3131
|
3232
*/
33-
'model' => env('GROQ_MODEL', 'llama3-8b-8192'),
33+
'model' => env('GROQ_MODEL', 'llama-3.1-8b-instant'),
3434

3535
/*
3636
|--------------------------------------------------------------------------

src/GroqClient.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function getDefaultConfig(): array
5858
return [
5959
'baseUrl' => config('groq.api_base', 'https://api.groq.com/openai/v1'),
6060
'timeout' => config('groq.timeout', 30),
61-
'model' => config('groq.model', 'llama3-8b-8192'),
61+
'model' => config('groq.model', 'llama-3.1-8b-instant'),
6262
'max_tokens' => config('groq.options.max_tokens', 150),
6363
'temperature' => config('groq.options.temperature', 0.7),
6464
'top_p' => config('groq.options.top_p', 1.0),
@@ -135,9 +135,26 @@ public function batches()
135135
*/
136136
public function setConfig(array $options): void
137137
{
138+
// Get current client options if available
139+
$currentOptions = [];
140+
if (property_exists($this->client, 'options')) {
141+
$currentOptions = $this->client->options;
142+
}
143+
144+
// Merge in order of precedence:
145+
// 1. New options (highest priority)
146+
// 2. Current options (if they exist)
147+
// 3. Default config (lowest priority)
148+
$mergedOptions = array_merge(
149+
$this->getDefaultConfig(),
150+
$currentOptions,
151+
$options
152+
);
153+
154+
// Create new client with updated options
138155
$this->client = new Groq(
139156
$this->client->apiKey,
140-
array_merge($this->getDefaultConfig(), $options)
157+
$mergedOptions
141158
);
142159
}
143160

tests/Feature/GroqIntegrationTest.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ protected function getPackageProviders($app)
1515

1616
protected function getEnvironmentSetUp($app)
1717
{
18-
// Use a chave real da API para testes de integração
18+
// Use the real API key for integration tests
1919
$app['config']->set('groq.api_key', env('GROQ_API_KEY'));
20-
$app['config']->set('groq.model', 'llama3-8b-8192');
20+
$app['config']->set('groq.model', 'llama-3.1-8b-instant');
2121
}
2222

2323
/** @test */
@@ -34,9 +34,9 @@ public function it_can_list_models()
3434
public function it_can_create_chat_completion()
3535
{
3636
$response = Groq::chat()->completions()->create([
37-
'model' => 'llama3-8b-8192',
37+
'model' => 'llama-3.1-8b-instant',
3838
'messages' => [
39-
['role' => 'user', 'content' => 'Diga olá em português']
39+
['role' => 'user', 'content' => 'Say hello in Portuguese']
4040
]
4141
]);
4242

@@ -50,9 +50,10 @@ public function it_can_create_chat_completion()
5050
/** @test */
5151
public function it_can_analyze_image()
5252
{
53-
$imageUrl = 'https://raw.githubusercontent.com/lucianotonet/groq-laravel/main/docs/art.png';
53+
// Using a small embedded test image as Base64 to avoid external dependency
54+
$base64Image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAFElEQVR42mP8z8BQz0AEYBxVSF+FABJADveWkH6oAAAAAElFTkSuQmCC';
5455

55-
$response = Groq::vision()->analyze($imageUrl, 'Descreva esta imagem');
56+
$response = Groq::vision()->analyze($base64Image, 'Describe this image');
5657

5758
$this->assertIsArray($response);
5859
$this->assertArrayHasKey('choices', $response);
@@ -67,18 +68,18 @@ public function it_can_handle_errors_gracefully()
6768
$this->expectException(\LucianoTonet\GroqPHP\GroqException::class);
6869

6970
Groq::chat()->completions()->create([
70-
'model' => 'llama3-8b-8192',
71-
'messages' => [] // Mensagens vazias devem gerar erro
71+
'model' => 'llama-3.1-8b-instant',
72+
'messages' => [] // Empty messages should generate an error
7273
]);
7374
}
7475

7576
/** @test */
7677
public function it_can_use_different_models()
7778
{
7879
$response = Groq::chat()->completions()->create([
79-
'model' => 'llama3-8b-8192', // Usando o modelo padrão ao invés do mixtral
80+
'model' => 'llama-3.1-8b-instant', // Using the default model instead of mixtral
8081
'messages' => [
81-
['role' => 'user', 'content' => 'Diga olá em português']
82+
['role' => 'user', 'content' => 'Say hello in Portuguese']
8283
]
8384
]);
8485

@@ -89,9 +90,9 @@ public function it_can_use_different_models()
8990
/** @test */
9091
public function it_can_handle_file_operations()
9192
{
92-
// Criar arquivo JSONL temporário
93+
// Create temporary JSONL file
9394
$tempFile = tempnam(sys_get_temp_dir(), 'groq_test_') . '.jsonl';
94-
file_put_contents($tempFile, json_encode(['prompt' => 'Olá']) . "\n" . json_encode(['prompt' => 'Mundo']) . "\n");
95+
file_put_contents($tempFile, json_encode(['prompt' => 'Hello']) . "\n" . json_encode(['prompt' => 'World']) . "\n");
9596

9697
// Upload
9798
$file = Groq::files()->upload($tempFile, 'batch');
@@ -115,23 +116,27 @@ public function it_can_handle_file_operations()
115116
/** @test */
116117
public function it_respects_configuration_options()
117118
{
118-
// Configurando para gerar respostas curtas, mas com limite de tokens maior
119+
// Configure to generate short responses with stricter token limit
119120
Groq::setConfig([
120121
'temperature' => 0.1,
121-
'max_tokens' => 30 // Usando um valor muito mais restritivo para o teste
122+
'max_tokens' => 30 // Using a much more restrictive value for testing
122123
]);
123124

124125
$response = Groq::chat()->completions()->create([
125-
'model' => 'llama3-8b-8192',
126+
'model' => 'llama-3.1-8b-instant',
126127
'messages' => [
127-
['role' => 'user', 'content' => 'Diga apenas "Olá, mundo!"']
128+
['role' => 'user', 'content' => 'Just say "Hello, world!"']
128129
],
129-
'max_tokens' => 30 // Garantindo que o valor seja passado na requisição também
130+
'max_tokens' => 30 // Ensuring the value is also passed in the request
130131
]);
131132

132133
$this->assertIsArray($response);
133134
$this->assertArrayHasKey('choices', $response);
134-
// Vamos apenas verificar se há resposta, sem verificar o tamanho
135+
// Let's just check if there's a response, without verifying the size
135136
$this->assertNotEmpty($response['choices'][0]['message']['content']);
137+
138+
// Verify the response is reasonably constrained by max_tokens
139+
$content = $response['choices'][0]['message']['content'];
140+
$this->assertLessThanOrEqual(100, strlen($content), 'Response should be constrained by max_tokens');
136141
}
137142
}

0 commit comments

Comments
 (0)