How to use Groq API with PHP the easy way (function)

Perry

Administrator
Staff member
Groq has some interesting things going on.
  1. It's API is free to use, and have the lowest cost if you're a high usage customer.
  2. It's the fastest AI API on the Market.
  3. Built on open-source models like Meta AI Llama.
Why is it so fast? Groq is fast because of its customized hardware. Single-core streamlined architecture, static execution model, and massive parallelism. It eliminates runtime scheduling, uses thousands of arithmetic units in parallel, and optimizes data flow with on-chip memory. These features enable deterministic latency and high efficiency, perfect for AI and ML workloads.

Here's how to use their API with a PHP function

PHP:
$groqApiKey = "you_key";

// models https://console.groq.com/docs/models
// llama3-8b-8192  - llama3-70b-8192  -  gemma-7b-it (8k)
// llama-3.3-70b-versatile - llama-3.3-70b-specdec

function queryGroqAPI($message, $systemMessage, $tokens, $model, $temp = 0) {
  
    global $groqApiKey;
    $payload = [
        'messages' => [
            ['role' => 'system', 'content' => $systemMessage],
            ['role' => 'user', 'content' => $message]
        ],
        'model' => $model,
        'temperature' => $temp,
    
        'top_p' => 1,
        'stream' => false
      //  'stop' => ["\n\n"] // Updated stopping condition
    ];
    if (!empty($tokens)) {
        $payload['max_tokens'] = $tokens;
    }
    $ch = curl_init('https://api.groq.com/openai/v1/chat/completions');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: Bearer ' . $groqApiKey
    ]);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($httpCode != 200) {
        echo "Groq HTTP Error: Received status code {$httpCode} <br> Prompt $message";
        print_r($response);
        exit();
    }
    $decodedResponse = json_decode($response, true);
    if (isset($decodedResponse['error'])) {
        echo  'Groq API Error: ' . $decodedResponse['error']['message'].'<br> Prompt: '.$message.'<br>';
        print_r($decodedResponse);
        exit();
    }
  
    // Extract the content from the response array
    $content = $decodedResponse['choices'][0]['message']['content'];
    return $content;
}

Call the function:

PHP:
// Example inputs
$message = "What is the capital of France?";
$systemMessage = "You are a helpful assistant.";
$tokens = 100; // Set the token limit
$model = "llama-3.3-70b-versatile"; // Specify the model
$temp = 0.7; // Set temperature for variability

// Call the function
$response = queryGroqAPI($message, $systemMessage, $tokens, $model, $temp);

// Output the response
echo "Response from Groq API: " . $response;
 
To obtain a Groq API key, follow these steps:
  • Sign Up or Log In: Visit the Groq Console and create an account or log in if you already have one.
  • Navigate to API Keys: After logging in, go to the API Keys section, typically found in the dashboard or under account settings.
  • Create a New API Key: Click on "Create API Key," provide a descriptive name, and submit.
  • Secure Your API Key: Once generated, copy and store the API key securely, as it may not be displayed again.
 
Groq and Grok? I'm confused😴
Yes me too, I don't understand why Elon Musk choose Grok when Groq was already a brand.

But to answer the question: No, Groq and Grok are distinct entities in the AI landscape:
  • Groq: An American AI company founded in 2016, specializing in high-performance hardware accelerators for AI workloads. Their flagship product, the Language Processing Unit (LPU), is designed to enhance the speed and efficiency of AI inference tasks.
  • Grok: A generative AI chatbot developed by xAI, a company led by Elon Musk. Launched in 2023, Grok is integrated with the social media platform X (formerly Twitter) and is designed to provide users with conversational AI capabilities, including real-time information access and a sense of humor.
While both are involved in AI, Groq focuses on hardware solutions to accelerate AI computations, whereas Grok offers a software-based conversational AI experience.
 
I see why the names can be confusing! Here's a way to keep them straight: think of Groq as the speedster of AI hardware, zooming through computations with its LPU, making AI models run faster and more efficiently. On the flip side, Grok is like your witty friend on social media, always ready with a clever response or helpful insight, thanks to xAI's tech.

If you're diving into AI development, Groq's hardware could be the secret sauce for your projects, speeding up everything from training to inference. And if you're looking to add a conversational AI to your app or website, Grok might be the charming assistant you need. Both are fascinating in their own right, but they serve different purposes in the AI world. Curious about how you might use either in your projects? Let's chat more!
 
Back
Top