Skip to content

Commit 62af4e4

Browse files
committed
Atualiza integração do Groq com novas funcionalidades
Adiciona um exemplo de arquivo .env e expande a configuração com variáveis para ajuste de modelo e limites de taxa. Inclui novos métodos de validação em requisições, facilitando a integração da API, e implementa controle de taxa para evitar abusos. A documentação foi simplificada e exemplos práticos foram adicionados para melhorar a usabilidade. A versão do pacote foi incrementada para refletir as novas melhorias.
1 parent c078490 commit 62af4e4

16 files changed

+398
-1271
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
GROQ_API_KEY=sua_chave_api_aqui
2+
GROQ_API_BASE=https://api.groq.com/openai/v1

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ yarn-error.log
1717
/.fleet
1818
/.idea
1919
/.vscode
20+
composer.lock
21+
proj2md.py

README.md

Lines changed: 29 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,68 @@
11
# Groq Laravel
22

3-
The Groq Laravel package provides a service provider and facade to integrate the GroqPHP library into your Laravel application. This allows you to easily interact with the Groq API.
3+
Groq Laravel is a powerful package that provides seamless integration between Laravel applications and the Groq API, enabling you to leverage the capabilities of language models (LLMs) like LLaMa directly within your PHP projects.
4+
5+
## Features
6+
7+
- **Simple and Intuitive Interface:** Interact with the Groq API using the `Groq` facade, simplifying access to chat, audio, and model functionalities.
8+
- **Robust Error Handling:** Efficiently handle communication errors and Groq API responses by capturing specific exceptions and providing informative messages.
9+
- **Flexible Configuration:** Define multiple Groq API instances, customize request timeouts, configure cache options, and adjust the package behavior to your needs.
10+
- **Detailed Practical Examples:** Explore code examples that demonstrate how to use the Groq Laravel package in real scenarios, including chatbots, audio transcription, and more.
11+
- **Comprehensive Testing:** Ensure the package's quality and reliability with a suite of tests covering integration, unit testing, and configuration aspects.
412

513
## Installation
614

7-
Install the package with Composer:
15+
1. Install the package via Composer:
816

917
```bash
1018
composer require lucianotonet/groq-laravel
1119
```
1220

13-
## Configuration
14-
15-
After registering the service provider, publish the configuration file:
21+
2. Publish the configuration file:
1622

1723
```bash
18-
php artisan vendor:publish --provider="LucianoTonet\\GroqLaravel\\GroqServiceProvider"
19-
```
20-
21-
This will create a `config/groq.php` file where you can set your Groq API key and base URL:
22-
23-
```php
24-
return [
25-
'api_key' => env('GROQ_API_KEY'),
26-
'base_url' => env('GROQ_API_BASE', 'https://api.groq.com/openai/v1'),
27-
];
24+
php artisan vendor:publish --provider="LucianoTonet\GroqLaravel\GroqServiceProvider"
2825
```
2926

30-
You can also set these values in your `.env` file:
27+
3. Configure your Groq API credentials in the `.env` file:
3128

3229
```
33-
GROQ_API_KEY=your-api-key-here
30+
GROQ_API_KEY=your_api_key_here
3431
GROQ_API_BASE=https://api.groq.com/openai/v1
3532
```
3633

37-
## Usage
34+
4. (Optional) Configure caching by defining the following environment variables in the `.env` file:
3835

39-
### `Groq` Facade
40-
41-
This package provides a convenient `Groq` facade to interact with the Groq API.
36+
```
37+
GROQ_CACHE_DRIVER=file
38+
GROQ_CACHE_TTL=3600
39+
```
4240

43-
### Examples
41+
5. Import the `Groq` facade in your classes:
4442

45-
To use the Groq facade, first import it into your class:
4643
```php
4744
use LucianoTonet\GroqLaravel\Facades\Groq;
4845
```
4946

50-
#### Chat Completions
51-
52-
```php
53-
// Starting a Chat Session
54-
$response = Groq::chat()->completions()->create([
55-
'messages' => [
56-
['role' => 'user', 'content' => 'Hello, world!'],
57-
],
58-
]);
59-
60-
// Process the response
61-
echo $response['choices'][0]['message']['content'];
62-
```
47+
## Usage
6348

64-
#### Chat Streaming
49+
Here's a simple example of creating a chat completion:
6550

6651
```php
67-
$message = 'Hello, world!';
68-
69-
// Make the call to the Groq API with streaming enabled
70-
$response = Groq::chat()->completions()->create([
71-
'model' => 'mixtral-8x7b-32768',
52+
$response = Groq::chat()->completion()->create([
53+
'model' => 'llama-3.1-8b-instant',
7254
'messages' => [
73-
[
74-
'role' => 'user',
75-
'content' => $message
76-
]
55+
['role' => 'user', 'content' => 'Hello, how are you?'],
7756
],
78-
'stream' => true
7957
]);
80-
81-
// Process the response chunks
82-
foreach ($response->chunks() as $chunk) {
83-
if (isset($chunk['choices'][0]['delta']['role'])) {
84-
echo "<strong>" . $chunk['choices'][0]['delta']['role'] . ":</strong> ";
85-
}
86-
87-
if (isset($chunk['choices'][0]['delta']['content'])) {
88-
echo $chunk['choices'][0]['delta']['content'];
89-
}
90-
}
9158
```
9259

93-
#### Tool Usage Example
60+
Refer to the [documentation](docs/index.md) for more detailed information on available methods, configuration options, and practical examples.
9461

95-
```php
96-
// Define the tools to be used
97-
$tools = [
98-
'search' => function ($args) {
99-
// Implement search tool logic
100-
return 'Search results for ' . $args['query'];
101-
},
102-
'calculator' => function ($args) {
103-
// Implement calculator tool logic
104-
return $args['a'] + $args['b'];
105-
},
106-
];
107-
108-
$messages = [
109-
['role' => 'user', 'content' => 'Calculate 3 + 5 using the calculator tool.']
110-
];
111-
112-
// Make the call to the Groq API
113-
$response = Groq::chat()->completions()->create([
114-
'model' => 'llama3-groq-70b-8192-tool-use-preview',
115-
'messages' => $messages,
116-
'temperature' => 0,
117-
'tool_choice' => 'auto',
118-
'tools' => $tools
119-
]);
120-
121-
// Process the API response
122-
if (!empty($response['choices'][0]['message']['tool_calls'])) {
123-
foreach ($response['choices'][0]['message']['tool_calls'] as $tool_call) {
124-
if ($tool_call['function']['name']) {
125-
$function_args = json_decode($tool_call['function']['arguments'], true);
126-
127-
// Call the function defined earlier
128-
$function_response = $tool_call['function']['name']($function_args);
129-
130-
// Add the function response to the message
131-
$messages[] = [
132-
'tool_call_id' => $tool_call['id'],
133-
'role' => 'tool',
134-
'name' => $tool_call['function']['name'],
135-
'content' => $function_response,
136-
];
137-
}
138-
}
139-
140-
// Make a new call to the Groq API with the function response
141-
$response = Groq::chat()->completions()->create([
142-
'model' => 'llama3-groq-70b-8192-tool-use-preview',
143-
'messages' => $messages
144-
]);
145-
}
146-
147-
// Display the final response
148-
echo $response['choices'][0]['message']['content'];
149-
```
150-
151-
#### Error Handling
62+
## Contributing
15263

153-
The Groq-Laravel package throws a `GroqException` for any errors encountered while communicating with the Groq API. You can catch this exception:
154-
155-
```php
156-
try {
157-
$response = Groq::chat()->completions()->create([
158-
// ... your request parameters
159-
]);
160-
} catch (GroqException $e) {
161-
// Handle the exception, for example, log the error or display a user-friendly message
162-
Log::error($e->getMessage());
163-
abort(500, 'An error occurred while processing your request.');
164-
}
165-
```
64+
Contributions are welcome! Please follow the guidelines outlined in the [CONTRIBUTING.md](CONTRIBUTING.md) file.
16665

16766
## License
16867

169-
This package is open-sourced software licensed under the [MIT license](LICENSE).
68+
This package is open-source software licensed under the [MIT license](LICENSE).

composer.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "lucianotonet/groq-laravel",
33
"description": "Laravel package to access Groq REST API",
44
"type": "library",
5-
"version": "0.0.7",
5+
"version": "0.0.8",
66
"license": "MIT",
77
"authors": [
88
{
@@ -11,9 +11,15 @@
1111
}
1212
],
1313
"require": {
14-
"php": ">=8.1",
15-
"lucianotonet/groq-php": "^0.0.7"
16-
},
14+
"php": "^8.1",
15+
"lucianotonet/groq-php": "^0.0.8",
16+
"illuminate/support": "*",
17+
"illuminate/contracts": "*"
18+
},
19+
"require-dev": {
20+
"orchestra/testbench": "^6.0|^7.0|^8.0",
21+
"phpunit/phpunit": "^9.0"
22+
},
1723
"autoload": {
1824
"psr-4": {
1925
"LucianoTonet\\GroqLaravel\\": "src/"

0 commit comments

Comments
 (0)