1. Go to this page and download the library: Download lucianotonet/groq-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
useLucianoTonet\GroqPHP\Groq;
$groq = new Groq(getenv('GROQ_API_KEY'));
try {
$response = $groq->chat()->completions()->create([
'model' => 'llama3-8b-8192', // Or another supported model'messages' => [
['role' => 'user', 'content' => 'Explain the importance of low latency in LLMs'],
],
]);
echo $response['choices'][0]['message']['content'];
} catch (\LucianoTonet\GroqPHP\GroqException $e) {
echo'Error: ' . $e->getMessage();
}
$response = $groq->chat()->completions()->create([
'model' => 'llama3-8b-8192',
'messages' => [
['role' => 'user', 'content' => 'Tell me a short story'],
],
'stream' => true
]);
foreach ($response->chunks() as $chunk) {
if (isset($chunk['choices'][0]['delta']['content'])) {
echo $chunk['choices'][0]['delta']['content'];
ob_flush(); // Important for real streaming
flush();
}
}
$response = $groq->chat()->completions()->create([
'model' => 'mixtral-8x7b-32768',
'messages' => [
['role' => 'system', 'content' => 'You are an API and must respond only with valid JSON.'],
['role' => 'user', 'content' => 'Give me information about the current weather in London'],
],
'response_format' => ['type' => 'json_object']
]);
$content = $response['choices'][0]['message']['content'];
echo json_encode(json_decode($content), JSON_PRETTY_PRINT); // Display formatted JSON
// Example function (simulated)functiongetNbaScore($teamName){
// ... (simulated logic to return score) ...return json_encode(['team' => $teamName, 'score' => 100]); // Example
}
$messages = [
['role' => 'system', 'content' => "You must call the 'getNbaScore' function to answer questions about NBA game scores."],
['role' => 'user', 'content' => 'What is the Lakers score?']
];
$tools = [
[
'type' => 'function',
'function' => [
'name' => 'getNbaScore',
'description' => 'Get the score for an NBA game',
'parameters' => [
'type' => 'object',
'properties' => [
'team_name' => ['type' => 'string', 'description' => 'NBA team name'],
],
' => $function_response,
];
// Second call to the model with tool response:
$response = $groq->chat()->completions()->create([
'model' => 'llama3-groq-70b-8192-tool-use-preview',
'messages' => $messages
]);
echo $response['choices'][0]['message']['content'];
} else {
// Direct response, no tool_callsecho $response['choices'][0]['message']['content'];
}
useLucianoTonet\GroqPHP\Groq;
$groq = new Groq(getenv('GROQ_API_KEY'));
try {
$transcription = $groq->audio()->transcriptions()->create([
'file' => 'audio.mp3', /* Your audio file */'model' => 'whisper-large-v3',
'response_format' => 'verbose_json', /* Or 'text', 'json' */'language' => 'en', /* ISO 639-1 code (optional but recommended) */'prompt' => 'Audio transcription...'/* (optional) */
]);
echo json_encode($transcription, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
} catch (\LucianoTonet\GroqPHP\GroqException $e) {
echo"Error: " . $e->getMessage();
}
// (Similar to transcription, but uses ->translations()->create() and always translates to English)// Target language for translation is always English
$translation = $groq->audio()->translations()->create([
'file' => 'audio_in_spanish.mp3',
'model' => 'whisper-large-v3'
]);
useLucianoTonet\GroqPHP\Groq;
$groq = new Groq(getenv('GROQ_API_KEY'));
try {
$response = $groq->vision()->analyze('image.jpg', 'Describe this image');
echo $response['choices'][0]['message']['content'];
} catch (\LucianoTonet\GroqPHP\GroqException $e) {
echo"Error: " . $e->getMessage();
}
$response = $groq->vision()->analyze('https://example.com/image.jpg', 'Describe this image');
// ... (See example for details) ...
$response = $groq->reasoning()->analyze(
"Solve this math problem: 3x + 7 = 22",
[
'model' => 'deepseek-r1-distill-llama-70b',
'reasoning_format' => 'parsed'
]
);
// Response structure:// {// "reasoning": "Step 1: Subtract 7 from both sides...",// "content": "x = 5"// }
$response = $groq->reasoning()->analyze(
"What is the capital of France?",
[
'model' => 'deepseek-r1-distill-llama-70b',
'reasoning_format' => 'hidden'
]
);
// Response