Search code examples
javascriptnode.jsfetch-api

How do I avoid SDK and use raw fetch with Groq API?


I get a 404 on the api call. It appears to be using the deprecated API. I want to know how do a fetch/curl call with latest groq.com API. Anyone know how to fix? The official SDK is overly complex and I just want to make fetch requests.

const response = await fetch('https://api.groq.com/v1/engines/llama3/completions', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        Authorization: `Bearer ${env.GROQ_API_KEY}`
    },
    body: JSON.stringify({
        prompt,
        // maxTokens: 150000, // Customize as needed
        temperature: 0.5, // Customize as needed
        topP: 1.0, // Customize as needed
        n: 1, // Number of completions to generate
        stop: null // Optional stopping sequence
    })
});

Solution

  • Curl solution:

    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 helpful assistant"
               },
               {
                 "role": "user",
                 "content": "Hello"
               }
             ],
             "model": "llama3-8b-8192",
             "temperature": 1,
             "max_tokens": 1024,
             "top_p": 1,
             "stream": true,
             "stop": null
           }'
    

    Fetch solution:

            const response = await fetch('https://api.groq.com/openai/v1/chat/completions', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    Authorization: `Bearer ${env.GROQ_API_KEY}`
                },
                body: JSON.stringify({
                    messages: [
                        {
                            role: 'system',
                            content:
                                'You are a helpful assistant in the areas of global threat intelligence and osint. Please only return valid JSON and no other text.'
                        },
                        {
                            role: 'user',
                            content: prompt
                        }
                    ],
                    model: 'llama3-8b-8192',
                    temperature: 1,
                    // max_tokens: 1024,
                    top_p: 1,
                    stream: false,
                    stop: null
                })
            });
    
            if (response.ok) {
                const data = await response.json();
                console.log(data.choices[0].message?.content, '<---- groq.com api');
    
                return JSON.parse(data.choices[0].message?.content);
            } else {
                console.error(await response.json());
            }