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/ */
// 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'],
],
'tion_response,
];
// Second call to the model with tool response:
$response = $groq->chat()->completions()->create([
'model' => 'openai/gpt-oss-120b',
'messages' => $messages
]);
echo $response['choices'][0]['message']['content'];
} else {
// Direct response, no tool_calls
echo $response['choices'][0]['message']['content'];
}
// When the model requests a tool, the first response
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);
// Output (formatted JSON):
// {
// "text": "Hello, how can I help you today",
// "language": "english",
// "duration": 3.2,
// "segments": [ { "start": 0.0, "end": 2.1, "text": "Hello, how can I help you today" } ]
// }
} 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('canopylabs/orpheus-v1-english') // 'canopylabs/orpheus-v1-english' for English, 'canopylabs/orpheus-arabic-saudi' for Arabic
->input('Hello, this text will be converted to speech')
->voice('troy') // 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('canopylabs/orpheus-v1-english')
->input('This is another example text')
->voice('troy')
->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();
}
// Method 1 prints: "Audio file saved successfully!"
// Method 2 streams raw WAV audio bytes (Content-Type: audio/wav).
use LucianoTonet\GroqPHP\Groq;
$groq = new Groq(getenv('GROQ_API_KEY'));
try {
// Analyze a local image
$response = $groq->vision()->analyze('path/to/image.jpg', 'What do you see in this image?');
// Analyze an image from URL
$response = $groq->vision()->analyze('https://example.com/image.jpg', 'Describe this image');
// Custom options
$response = $groq->vision()->analyze('path/to/image.jpg', 'What colors do you see?', [
'temperature' => 0.7,
'max_completion_tokens' => 100
]);
echo $response['choices'][0]['message']['content'];
// Expected response structure (formatted):
// {
// "choices": [
// { "message": { "role": "assistant", "content": "I see a sunset over the mountains..." }, "finish_reason": "stop" }
// ],
// "usage": { "prompt_tokens": 120, "completion_tokens": 40, "total_tokens": 160 }
// }
} catch (\LucianoTonet\GroqPHP\GroqException $e) {
echo 'Error: ' . $e->getMessage();
}
$response = $groq->reasoning()->analyze(
"What is the capital of France?",
[
'model' => 'qwen/qwen3.6-27b',
'reasoning_format' => 'hidden'
]
);
// Response
use LucianoTonet\GroqPHP\Groq;
use LucianoTonet\GroqPHP\BuiltInTools;
$groq = new Groq(getenv('GROQ_API_KEY'));
$response = $groq->chat()->completions()->create([
'model' => 'compound-beta',
'messages' => [
['role' => 'user', 'content' => 'What happened in AI last week?'],
],
'compound_custom' => BuiltInTools::compound([
BuiltInTools::WEB_SEARCH,
BuiltInTools::CODE_INTERPRETER,
]),
'search_settings' => ['exclude_domains' => ['wikipedia.org']],
]);
echo $response['choices'][0]['message']['content'];
// Expected response structure (formatted):
// {
// "choices": [
// { "message": { "role": "assistant", "content": "Last week's AI highlights
use LucianoTonet\GroqPHP\BuiltInTools;
$response = $groq->chat()->completions()->create([
'model' => 'llama-3.3-70b-versatile', // a model that supports documents
'messages' => [
['role' => 'user', 'content' => 'Summarize the provided document'],
],
'documents' => [
BuiltInTools::document('Groq is a fast inference platform...', 'doc-1'),
],
'citation_options' => 'enabled',
]);
echo $response['choices'][0]['message']['content'];
// Expected response structure (formatted):
// {
// "choices": [
// { "message": { "role": "assistant", "content": "Groq is a fast AI inference platform focused on low-latency LLM serving." }, "finish_reason": "stop" }
// ],
// "citations": [ { "document": "doc-1", "url": "...", "title": "..." } ] // present when citation_options=enabled
// }
use LucianoTonet\GroqPHP\Groq;
use LucianoTonet\GroqPHP\Responses;
$groq = new Groq(getenv('GROQ_API_KEY'));
$response = $groq->responses()->create([
'model' => 'openai/gpt-oss-120b',
'input' => 'Tell me a fun fact about the moon in one sentence.',
]);
echo Responses::outputText($response);
// Hello from the Responses API.
$stream = $groq->responses()->create([
'model' => 'openai/gpt-oss-120b',
'input' => 'Tell me a short story.',
'stream' => true,
]);
foreach ($stream->chunks() as $event) {
if (($event['type'] ?? null) === 'response.output_text.delta') {
echo $event['delta'];
}
}