How do you set up and use the Groq API to query multiple LLMs, based on the example code?
To set up the Groq API, first install the groq library with `pip3 install groq`, create an account at console.groq.com, generate an API key at console.groq.com/keys, and save that key as GROQ_API_KEY in a `.env` file. To query multiple LLMs, define a list of model names, loop over that list, and for each model call `client.chat.completions.create(model=model, messages=prompts)` using a Groq client initialized with the API key. Print each response with `result.choices[0].message.content`.
The book's example (`example_groq_api.ipynb`) shows the full setup and usage. First, install the Groq Python library by running `pip3 install groq`. Then, go to console.groq.com, create an account, and at console.groq.com/keys follow the instructions to create an API key. Add the key to a `.env` file with the name GROQ_API_KEY. After loading environment variables with `load_dotenv()`, retrieve the key using `os.getenv("GROQ_API_KEY")` and raise an error if it is not found. Next, initialize the client with `client = Groq(api_key=api_key)`. To query several models, the code first builds a list of model identifiers, such as `models_in_groq = ["gemma2-9b-it", "openai/gpt-oss-20b", "llama-3.3-70b-versatile", "qwen/qwen3-32b"]`. This list should be updated to reflect models currently hosted on the platform, as shown at console.groq.com/docs/models. The example also defines a `generate_llm_prompts()` function that returns a list of messages with a system prompt and a user prompt, for example `[{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "How does AI work?"}]`. Then, for each model in the list, the code calls `client.chat.completions.create(model=model, messages=prompts)`, which sends the same prompts to that model and returns a result. It prints the model name and then extracts the text with `result.choices[0].message.content`. The loop runs once per model, allowing direct comparison of outputs from multiple LLMs. Optional parameters such as `temperature` or `top_p` may be added to the API call, with the same meaning as in the OpenAI API. Because LLMs are probabilistic, the exact text returned may vary between runs.
Key points
- Install the Groq library with `pip3 install groq` and create an account at console.groq.com to obtain an API key.
- Store the API key as GROQ_API_KEY in a `.env` file, then load it with `load_dotenv()` and retrieve it using `os.getenv("GROQ_API_KEY")`.
- Initialize the Groq client with `Groq(api_key=api_key)`.
- Create a list of model names, such as `models_in_groq`, and update it according to the models hosted at console.groq.com/docs/models.
- Define prompts as a list of message dictionaries with system and user roles.
- Loop through the model list and call `client.chat.completions.create(model=model, messages=prompts)` for each model.
- Print each response using `result.choices[0].message.content`, and optionally add parameters like `temperature` or `top_p`.
AI for Qualitative Research: A Hands-On Guide for Management Scholars
Diana Garcia Quevedo
Palgrave Macmillan