|
1 |
| -# Groq Laravel package |
2 |
| - |
3 |
| -Laravel package to provide access to the [Groq REST API](https://console.groq.com/docs) using the [Groq-PHP library](https://github.com/lucianotonet/groq-php). |
| 1 | +# Groq Laravel |
4 | 2 |
|
| 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. |
5 | 4 |
|
6 | 5 | ## Installation
|
7 | 6 |
|
8 |
| -You can install the package via composer: |
| 7 | +Install the package with Composer: |
9 | 8 |
|
10 | 9 | ```bash
|
11 | 10 | composer require lucianotonet/groq-laravel
|
12 | 11 | ```
|
13 | 12 |
|
14 |
| -### Set up your keys |
| 13 | +## Configuration |
15 | 14 |
|
16 |
| -Set your [Groq API key](https://console.groq.com/keys) on the `.env` file: |
| 15 | +After registering the service provider, publish the configuration file: |
17 | 16 |
|
18 |
| -```.env |
19 |
| -GROQ_API_KEY=gsk_... |
| 17 | +```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 | +]; |
| 28 | +``` |
| 29 | + |
| 30 | +You can also set these values in your `.env` file: |
| 31 | + |
| 32 | +``` |
| 33 | +GROQ_API_KEY=your-api-key-here |
| 34 | +GROQ_API_BASE=https://api.groq.com/openai/v1 |
20 | 35 | ```
|
21 | 36 |
|
22 | 37 | ## Usage
|
23 | 38 |
|
24 |
| -### Groq Facade |
| 39 | +### `Groq` Facade |
25 | 40 |
|
26 |
| -You can use the `Groq` facade to interact with the Groq API like this: |
| 41 | +This package provides a convenient `Groq` facade to interact with the Groq API. |
27 | 42 |
|
| 43 | +### Examples |
| 44 | + |
| 45 | +To use the Groq facade, first import it into your class: |
28 | 46 | ```php
|
29 |
| -use Illuminate\Support\Facades\Route; |
30 | 47 | use LucianoTonet\GroqLaravel\Facades\Groq;
|
| 48 | +``` |
| 49 | + |
| 50 | +#### Chat Completions |
31 | 51 |
|
32 |
| -Route::get('/', function () { |
33 |
| - $groq = new Groq(); |
34 |
| - |
35 |
| - $chatCompletion = $groq->chat()->completions()->create([ |
36 |
| - 'model' => 'llama3-8b-8192', // llama3-8b-8192, llama3-70b-8192, llama2-70b-4096, mixtral-8x7b-32768, gemma-7b-it |
37 |
| - 'messages' => [ |
38 |
| - [ |
39 |
| - 'role' => 'user', |
40 |
| - 'content' => 'Explain the importance of low latency LLMs' |
41 |
| - ] |
42 |
| - ], |
| 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 | +``` |
| 63 | + |
| 64 | +#### Chat Streaming |
| 65 | + |
| 66 | +```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', |
| 72 | + 'messages' => [ |
| 73 | + [ |
| 74 | + 'role' => 'user', |
| 75 | + 'content' => $message |
| 76 | + ] |
| 77 | + ], |
| 78 | + 'stream' => true |
| 79 | +]); |
| 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 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +#### Tool Usage Example |
| 94 | + |
| 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 |
43 | 144 | ]);
|
| 145 | +} |
44 | 146 |
|
45 |
| - return $chatCompletion['choices'][0]['message']['content']; |
46 |
| -}); |
| 147 | +// Display the final response |
| 148 | +echo $response['choices'][0]['message']['content']; |
47 | 149 | ```
|
48 | 150 |
|
49 |
| -*Groq Laravel Package* is just a wrapper to the [Grok PHP library](https://github.com/lucianotonet/groq-php), so you can use all the methods and classes from that library through the facade. |
| 151 | +#### Error Handling |
| 152 | + |
| 153 | +The Groq-Laravel package throws a `GroqException` for any errors encountered while communicating with the Groq API. You can catch this exception: |
50 | 154 |
|
51 |
| -All examples found on the [examples directory](https://github.com/lucianotonet/groq-php/tree/main/examples) can be used with the Groq facade. |
| 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 | +``` |
52 | 166 |
|
53 | 167 | ## License
|
54 | 168 |
|
55 |
| -This package is open-sourced software licensed under the MIT license. See the [LICENSE](LICENSE) file for more information. |
| 169 | +This package is open-sourced software licensed under the [MIT license](LICENSE). |
0 commit comments