|
1 | 1 | # Groq Laravel
|
2 | 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. |
| 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. |
4 | 12 |
|
5 | 13 | ## Installation
|
6 | 14 |
|
7 |
| -Install the package with Composer: |
| 15 | +1. Install the package via Composer: |
8 | 16 |
|
9 | 17 | ```bash
|
10 | 18 | composer require lucianotonet/groq-laravel
|
11 | 19 | ```
|
12 | 20 |
|
13 |
| -## Configuration |
14 |
| - |
15 |
| -After registering the service provider, publish the configuration file: |
| 21 | +2. Publish the configuration file: |
16 | 22 |
|
17 | 23 | ```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" |
28 | 25 | ```
|
29 | 26 |
|
30 |
| -You can also set these values in your `.env` file: |
| 27 | +3. Configure your Groq API credentials in the `.env` file: |
31 | 28 |
|
32 | 29 | ```
|
33 |
| -GROQ_API_KEY=your-api-key-here |
| 30 | +GROQ_API_KEY=your_api_key_here |
34 | 31 | GROQ_API_BASE=https://api.groq.com/openai/v1
|
35 | 32 | ```
|
36 | 33 |
|
37 |
| -## Usage |
| 34 | +4. (Optional) Configure caching by defining the following environment variables in the `.env` file: |
38 | 35 |
|
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 | +``` |
42 | 40 |
|
43 |
| -### Examples |
| 41 | +5. Import the `Groq` facade in your classes: |
44 | 42 |
|
45 |
| -To use the Groq facade, first import it into your class: |
46 | 43 | ```php
|
47 | 44 | use LucianoTonet\GroqLaravel\Facades\Groq;
|
48 | 45 | ```
|
49 | 46 |
|
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 |
63 | 48 |
|
64 |
| -#### Chat Streaming |
| 49 | +Here's a simple example of creating a chat completion: |
65 | 50 |
|
66 | 51 | ```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', |
72 | 54 | 'messages' => [
|
73 |
| - [ |
74 |
| - 'role' => 'user', |
75 |
| - 'content' => $message |
76 |
| - ] |
| 55 | + ['role' => 'user', 'content' => 'Hello, how are you?'], |
77 | 56 | ],
|
78 |
| - 'stream' => true |
79 | 57 | ]);
|
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 | 58 | ```
|
92 | 59 |
|
93 |
| -#### Tool Usage Example |
| 60 | +Refer to the [documentation](docs/index.md) for more detailed information on available methods, configuration options, and practical examples. |
94 | 61 |
|
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 |
152 | 63 |
|
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. |
166 | 65 |
|
167 | 66 | ## License
|
168 | 67 |
|
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). |
0 commit comments