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.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use LucianoTonet\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' => 'llama3-70b-8192',
'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)
function getNbaScore($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_calls
echo $response['choices'][0]['message']['content'];
}
use LucianoTonet\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'
]);
use LucianoTonet\GroqPHP\Groq;
$groq = new Groq(getenv('GROQ_API_KEY'));
try {
// Method 1: Save to file
$result = $groq->audio()->speech()
->model('playai-tts') // 'playai-tts' for English, 'playai-tts-arabic' for Arabic
->input('Hello, this text will be converted to speech')
->voice('Bryan-PlayAI') // Voice identifier
->responseFormat('wav') // Output format
->save('output.wav');
if ($result) {
echo "Audio file saved successfully!";
}
// Method 2: Get as stream
$audioStream = $groq->audio()->speech()
->model('playai-tts')
->input('This is another example text')
->voice('Bryan-PlayAI')
->create();
// Use the stream (e.g., send to browser)
header('Content-Type: audio/wav');
header('Content-Disposition: inline; filename="speech.wav"');
echo $audioStream;
} catch (\LucianoTonet\GroqPHP\GroqException $e) {
echo "Error: " . $e->getMessage();
}
use LucianoTonet\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) ...
use LucianoTonet\GroqPHP\Groq;
$groq = new Groq(getenv('GROQ_API_KEY'));
try {
$response = $groq->reasoning()->analyze(
'Explain the process of photosynthesis.',
[
'model' => 'deepseek-r1-distill-llama-70b',
'reasoning_format' => 'raw', // 'raw' (default), 'parsed', 'hidden'
'temperature' => 0.6,
'max_completion_tokens' => 10240
]
);
echo $response['choices'][0]['message']['content'];
} catch (\LucianoTonet\GroqPHP\GroqException $e) {
echo "Error: " . $e->getMessage();
}
$response = $groq->reasoning()->analyze(
"What is the capital of France?",
[
'model' => 'deepseek-r1-distill-llama-70b',
'reasoning_format' => 'hidden'
]
);
// Response