Try out : Groq API Key

Objective

Create a Groq API Key for accessing to the open source generative AI models.

  1. Read about Groq
  • We are interested in using open source LLMs
  • Review the pricing plan
  1. Signup for developer access

groq-sign-up

  • Follow the steps to signup

  • Try out the models in the playground

  • Paste the following in the system prompt

You are a smart science teacher for grade 5. Politely answer the question. Your answers should be no more than 4 sentences & it must be easy to follow.
  1. Try out the model in terminal window

Scroll below for the commands to use on Windows.

  • Create API key

groq-api-key

On Linux/Mac

  • Open a terminal/shell window

  • Set the key in the environment variable

export GROQ_API_KEY="Paste your key here"
  • Access the model using HTTP/REST endpoint
curl "https://api.groq.com/openai/v1/chat/completions" \
  -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${GROQ_API_KEY}" \
  -d '{
         "messages": [
           {
             "role": "system",
             "content": "You are a smart science teacher for grade 5. Politely answer the question. Your answers should be no more than 4 sentences & it must be \neasy to follow."
           },
           {
             "role": "user",
             "content": "how does a car work?\n"
           }
         ],
         "model": "llama3-70b-8192",
         "temperature": 1,
         "max_tokens": 1024,
         "top_p": 1,
         "stream": false,
         "stop": null
       }'
  

On Windows

  • Open a PowerShell terminal

  • Set the key in the environment variable (quotes are needed)

$env:GROQ_API_KEY="Paste your key here"
  • Access the model using HTTP/REST endpoint
# Create the JSON payload
$payload = @{
    messages = @(
        @{
            role = "system"
            content = "You are a smart science teacher for grade 5. Politely answer the question. Your answers should be no more than 4 sentences & it must be easy to follow."
        },
        @{
            role = "user"
            content = "how does a car work?"
        }
    )
    model = "llama3-70b-8192"
    temperature = 1
    max_tokens = 1024
    top_p = 1
    stream = $false
    stop = $null
} | ConvertTo-Json -Depth 10 -Compress

$response = Invoke-RestMethod -Uri "https://api.groq.com/openai/v1/chat/completions" `
  -Method Post `
  -Headers @{
      "Content-Type" = "application/json";
      "Authorization" = "Bearer $env:GROQ_API_KEY"
  } `
  -Body $payload

if ($response.choices) {
    foreach ($choice in $response.choices) {
        $choice.message.content
    }
} else {
    Write-Output "No choices available in the response."
}