-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples folder + some improvements:
- Add support for response streaming and JSON mode. - Improve error handling with more descriptive errors. - Add usage examples. - Fix bugs related to streaming and JSON mode. - Update minimum PHP version. - Add unit tests.
- Loading branch information
1 parent
aea491d
commit 3bfb937
Showing
15 changed files
with
511 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,6 @@ | |
*.swo | ||
playground/* | ||
.idea | ||
.env | ||
.env | ||
/examples/.env | ||
/examples/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
GROQ_API_KEY=gsk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # Get your API key on https://console.groq.com/keys | ||
GROQ_API_BASE_URL=https://api.groq.com/openai/v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/.phpunit.cache | ||
/.php-cs-fixer.cache | ||
/.php-cs-fixer.php | ||
/phpunit.xml | ||
/vendor/ | ||
*.swp | ||
*.swo | ||
playground/* | ||
.idea | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<form method="post" class="mb-10"> | ||
<input type="text" class="border border-black p-4 w-96" name="message" placeholder="What is the importance of low latency in LLMs?" required> | ||
<button type="submit" class="bg-black text-white p-4">Send</button> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
require __DIR__ . '/_input.php'; | ||
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | ||
$message = $_POST['message']; | ||
|
||
echo "<strong>user: </strong> $message <br>"; | ||
|
||
$response = $groq->chat()->completions()->create([ | ||
'model' => 'mixtral-8x7b-32768', | ||
'messages' => [ | ||
[ | ||
'role' => 'user', | ||
'content' => $message | ||
] | ||
], | ||
'stream' => true | ||
]); | ||
|
||
foreach ($response->chunks() as $chunk) { | ||
if (isset($chunk['choices'][0]['delta']['role'])) { | ||
echo "<strong>" . $chunk['choices'][0]['delta']['role'] . ":</strong> "; | ||
} | ||
|
||
if (isset($chunk['choices'][0]['delta']['content'])) { | ||
echo $chunk['choices'][0]['delta']['content']; | ||
} | ||
|
||
ob_flush(); | ||
flush(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
require __DIR__ . '/_input.php'; | ||
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | ||
$message = $_POST['message']; | ||
|
||
echo "<strong>user: </strong> $message <br>"; | ||
|
||
$response = $groq->chat()->completions()->create([ | ||
'model' => 'llama3-8b-8192', // llama3-8b-8192, llama2-70b-4096, mixtral-8x7b-32768, gemma-7b-it | ||
'messages' => [ | ||
[ | ||
'role' => 'user', | ||
'content' => $message | ||
] | ||
], | ||
]); | ||
|
||
echo "<strong>assistant: </strong> "; | ||
echo $response['choices'][0]['message']['content']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "lucianotonet/example-groq-php", | ||
"type": "project", | ||
"authors": [ | ||
{ | ||
"name": "Luciano Tonet", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"vlucas/phpdotenv": "^5.6", | ||
"guzzlehttp/guzzle": "^7.8", | ||
"lucianotonet/groq-php": "^0.0.3" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^11.0" | ||
}, | ||
"repositories": [ | ||
{ | ||
"type": "path", | ||
"url": "../../", | ||
"options": { | ||
"symlink": true | ||
} | ||
} | ||
] | ||
} |
Oops, something went wrong.