Skip to content

Commit c078490

Browse files
committed
docs: Update README to reflect changes
1 parent 959a5cf commit c078490

File tree

1 file changed

+141
-27
lines changed

1 file changed

+141
-27
lines changed

README.md

Lines changed: 141 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,169 @@
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
42

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.
54

65
## Installation
76

8-
You can install the package via composer:
7+
Install the package with Composer:
98

109
```bash
1110
composer require lucianotonet/groq-laravel
1211
```
1312

14-
### Set up your keys
13+
## Configuration
1514

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:
1716

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
2035
```
2136

2237
## Usage
2338

24-
### Groq Facade
39+
### `Groq` Facade
2540

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.
2742

43+
### Examples
44+
45+
To use the Groq facade, first import it into your class:
2846
```php
29-
use Illuminate\Support\Facades\Route;
3047
use LucianoTonet\GroqLaravel\Facades\Groq;
48+
```
49+
50+
#### Chat Completions
3151

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
43144
]);
145+
}
44146

45-
return $chatCompletion['choices'][0]['message']['content'];
46-
});
147+
// Display the final response
148+
echo $response['choices'][0]['message']['content'];
47149
```
48150

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:
50154

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+
```
52166

53167
## License
54168

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

Comments
 (0)