groq / groq-typescript

The official Node.js / Typescript library for the Groq API
Apache License 2.0
145 stars 17 forks source link

Can I get a simple example using fetch or curl? #21

Closed ralyodio closed 4 months ago

ralyodio commented 4 months ago

I find this library to be overly complex and I'd like to just hit groq's api with fetch.

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
    })
});
gradenr commented 4 months ago

You can generate examples in curl (or for an SDK) with the following steps:

  1. Go to the playgound
  2. (optional) Set any query parameters in the playground such as temperature, system prompt, or user messages
  3. Click View Code in the lower right corner
  4. Select curl as your language in the dropdown at the top of the view code box

Here is an example curl from view code

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
       }'
ralyodio commented 4 months ago

what is the max tokens I can put?

gradenr commented 4 months ago

It varies for each model. The context window parameter in the models page is the max tokens the model can handle.

If you pass don't provide a value for max_tokens in your query, your query will use the max context available for the model.