DEV Community

King coder
King coder

Posted on

๐ŸŒŸ Today I Learned: Basics of Generative AI (Gen AI) with Groq Cloud and Prompting

๐Ÿค– What is Generative AI?

Generative AI (Gen AI) is a type of artificial intelligence that can create new content like:

  • Text (e.g., writing emails, summaries, answers)
  • Images (e.g., AI-generated art)
  • Code (e.g., coding assistants like GitHub Copilot)
  • Music, videos, and more

๐ŸŽฏ In simple words: You give the AI a prompt (a message or question), and it gives you a new response or result.


๐Ÿ› ๏ธ How Does Gen AI Work?

Hereโ€™s a basic process:

Training:

The AI is trained using massive datasets (books, articles, code, images). It learns patterns, language, and structure.

Understanding Prompts:

You give it a prompt like โ€œExplain gravity,โ€ and it uses its knowledge to give a relevant answer.

Generating Response:

It predicts the most likely next word/token โ€” one at a time โ€” to form a complete response.


๐Ÿงฑ What Are Tokens?

A token is a small piece of text โ€” a word or part of a word.

AI doesnโ€™t understand full sentences; it understands tokens.

Example:

  • "Hello" might be one token.
  • "unbelievable" might be split into "un", "believ", "able".

๐Ÿ”ข Token limits affect how long your prompts and responses can be.


โš ๏ธ AI Has a Limitation: It's Trained on Old Data

Generative AI models are trained on existing data from the internet (books, websites, codebases), but they donโ€™t know whatโ€™s happening right now.

โŒ Ask: โ€œWhat happened in the 2024 elections?โ€

AI might say: โ€œI don't know,โ€ because it wasnโ€™t trained on that.


๐Ÿงฐ Why Do We Add Tools to AI?

Since the AI cannot access live data or the internet by default, we can add tools to give it more power.

โœ… What tools allow AI to do:

Use Case Without Tool With Tool
Live Weather Canโ€™t answer Fetches data from weather API
Stock Prices Might be outdated Gets real-time prices
Access Your Files Canโ€™t read local files Tool connects to file system
Use Google Search Doesnโ€™t know how Tool sends search query and returns result
Custom Code Execution Not possible Tool runs code and sends output


๐Ÿš€ Using Groq Cloud to Run Gen AI Models

Groq Cloud is a platform that allows you to run powerful open-source generative AI models โ€” instantly and at lightning-fast speeds โšก. It removes the need for expensive hardware or complex local setups, making AI more accessible to everyone.

Instead of running models on your own machine or renting expensive GPUs, you can use Groq's cloud infrastructure to send prompts and receive AI-generated responses in real time.

๐ŸŒŸ Models Available on Groq

Groq supports a range of cutting-edge open-source models, including:

  • LLaMA 3 โ€” Created by Meta, this model offers high-quality responses and strong reasoning capabilities, suitable for chat, summarization, and more.

  • Mixtral โ€” A high-performance, multilingual model capable of handling complex prompts in multiple languages with strong accuracy.

  • Gemma โ€” A lightweight and efficient model designed for fast performance with minimal resources, ideal for quick, low-latency tasks.

  • Falcon โ€” An open-source large language model known for speed and versatility in many NLP tasks.

  • Mistral โ€” A powerful, efficient model designed for state-of-the-art performance on various language understanding tasks.

  • OpenAssistant โ€” A community-driven model designed to assist with conversational AI needs.

โ€ฆand many more, constantly updated and expanded to provide the latest in generative AI capabilities.


๐Ÿ’ก Why Use Groq Cloud and APIs for AI?

Training and building large AI models from scratch is extremely resource-intensive. Models today often have millions or even billions of parameters, which require vast amounts of:

  • Computational power (high-end GPUs, TPUs, or specialized AI hardware)
  • Time (weeks or months of training)
  • Energy (a huge electricity footprint)
  • Expertise (data scientists, machine learning engineers)

Because of this, most developers, startups, and even many large companies do not train AI models themselves. Instead, they use APIs provided by platforms like Groq Cloud to access these powerful models on-demand.


๐Ÿ”Ž Other Examples of AI APIs and Platforms

  • Grok by Salesforce โ€” Offers AI models integrated with business applications and workflows.
  • OpenAI API โ€” Provides access to GPT models like ChatGPT for conversational AI and content generation.
  • Cohere โ€” Focuses on natural language processing APIs for tasks like classification and search.

These platforms allow users to integrate AI capabilities without needing to manage or train models themselves.


๐Ÿง‘โ€๐Ÿซ What About Training Your Own AI Model?

Some organizations and researchers do train and create their own AI models, especially when they need custom functionality or have unique data requirements. However, training large models requires:

  • Massive computational resources (clusters of GPUs or specialized hardware)
  • Access to huge datasets
  • Considerable time and expertise
  • Ongoing maintenance and fine-tuning

For many use cases, itโ€™s far more practical and cost-effective to use APIs to access pre-trained models hosted on the cloud, rather than building and maintaining models from scratch.


๐Ÿ”ง How Groq Cloud Works

  1. Sign Up for Groq Cloud: Create an account on Groq Cloud.
  2. Get API Access: Obtain an API key to authenticate your requests.
  3. Choose a Model: Select from models like LLaMA 3, Mixtral, or Gemma.
  4. Send a Prompt: Make an HTTP request to the Groq API with your prompt.
  5. Receive a Response: Instantly get back a generated response from the selected model.

๐Ÿงช Example Use Case

Summarize an article using LLaMA 3:

curl -X POST https://api.groq.com/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "llama3-70b",
    "messages": [
      {
        "role": "user",
        "content": "Summarize the key points of the article on climate change."
      }
    ]
  }'
Enter fullscreen mode Exit fullscreen mode

๐Ÿค” Why Do We Use the Groq SDK/Library?

When you want to use Groqโ€™s AI models, you can either:

  • Call their API (like sending a request on the internet) yourself, or
  • Use the Groq SDK โ€” a special tool (library) made to help you talk to Groq more easily.

Why use the Groq SDK?

  • Makes your work easier: You donโ€™t have to write all the complicated code to connect and talk to Groq.
  • Automatically handles login: The SDK takes care of sending your secret API key safely.
  • Helps with errors: If something goes wrong, it gives you clear messages.
  • Simple commands: You can ask the AI in just a few lines of code.
  • Keeps updated: The SDK is updated when Groq changes their API, so you donโ€™t have to worry.

๐Ÿš€ How Does the Groq SDK Work?

  • When you tell the SDK to โ€œcreate a chat completion,โ€ it sends your request behind the scenes to Groqโ€™s servers.
  • It sends your prompt (the message you give the AI) and the model name.
  • It waits for Groqโ€™s AI to answer.
  • Then it gives you the AIโ€™s response in a way thatโ€™s easy to use in your code.

You donโ€™t have to do all the hard work of sending and receiving data โ€” the SDK does it for you.


๐Ÿ“œ Simple Example Using Groq SDK (JavaScript)

import Groq from "groq-sdk";
import dotenv from "dotenv";

dotenv.config();

const groq = new Groq({ apiKey: process.env.GROQ_API_KEY });

async function main() {
  const response = await groq.chat.completions.create({
    model: "llama3-70b-8192",
    messages: [
      { role: "system", content: "You are a helpful assistant." },
      { role: "user", content: "Explain Generative AI simply." },
    ],
  });

  console.log("AI says:", response.choices[0]?.message?.content);
}

main();


Enter fullscreen mode Exit fullscreen mode

Building a Simple Web Search Tool with Groq AI

Imagine you want to find the current weather in Ranchi using AI. You write this code to ask Groqโ€™s AI:


import Groq from "groq-sdk";
import dotenv from "dotenv";
dotenv.config();

const groq = new Groq({ apiKey: process.env.GROQ_API_KEY });

async function main() {
  const completion = await groq.chat.completions.create({
    model: "llama3-70b-8192",
    messages: [
      {
        role: "system",
        content: "You are Rohit, a smart AI assistant. Give answers in paragraphs, easy to understand.",
      },
      {
        role: "user",
        content: "What is the current weather in Ranchi?",
      },
    ],
  });

  console.log(completion.choices[0]?.message?.content || "");
}

main();

Enter fullscreen mode Exit fullscreen mode
  • You send a prompt to the AI asking about the weather in Ranchi.
  • The AI responds based on its training data (which only includes information up to a certain date).
  • So, it gives you general weather info but cannot provide live, current weather data.

Example AI answer:

โ€œI donโ€™t have real-time access to the internet, but hereโ€™s what I know about Ranchiโ€™s climate... For real-time data, check websites like AccuWeather.โ€


โ“ Why Does This Happen?

  • AI models like this are trained on past data, so they donโ€™t know about things happening right now.
  • They cannot browse the internet or fetch live info by default.

๐Ÿงฐ What Are โ€œToolsโ€ in AI?

In the world of AI, Tools are special functions that an AI can use when it needs extra capabilities โ€” like searching the internet, doing calculations, reading uploaded files, generating images, or running code.

Think of Tools as the AI's superpowers. On their own, AI models like ChatGPT are trained on a lot of information, but they donโ€™t always know real-time data, canโ€™t access private files, or perform live web lookups unless you give them the ability to use tools.


๐Ÿง  Simple Analogy

Imagine the AI is like a really smart assistant. It knows a lot but sometimes needs to grab a calculator, browse the web, or look at a document.

You ask:

โ“ โ€œWhatโ€™s the current gold price in India?โ€

The AI responds:

๐Ÿค” โ€œHmm, I donโ€™t know the latest price. Let me use my โ€˜web searchโ€™ tool.โ€

โœ… It uses the tool โ†’ ๐Ÿ” gets live data โ†’ ๐Ÿงพ gives you the correct answer.

๐Ÿ› ๏ธ Common Types of Tools

Tool Name What It Does
๐ŸŒ Web Search Looks up real-time information from the internet
๐Ÿงฎ Python Runs code, does math, makes charts, or analyzes data
๐Ÿ“‚ File Reader Reads and processes uploaded files (PDFs, CSVs, etc.)
๐Ÿ–ผ๏ธ Image Generator Creates images from text prompts
๐Ÿ“Š Data Analysis Summarizes or visualizes structured data

๐Ÿš€ Why Are Tools Useful?

  • ๐Ÿ•’ Get Real-Time Answers (e.g., weather, stock prices, news)
  • ๐Ÿ“Š Analyze Files or Data (e.g., spreadsheets, reports)
  • โœจ Generate Visuals (e.g., mockups, charts, illustrations)
  • ๐Ÿ”„ Automate Tasks (e.g., code execution, format conversion)

โœ… What We're Building

We are building an AI Assistant that can:

๐Ÿง‘ Talk with the user in simple language

๐Ÿง  Think: โ€œDo I already know the answer?โ€

โŒ If not, use a tool (like search)

๐Ÿ”„ Use the result of the tool to answer fully


๐Ÿ” How It Works โ€” Real Life Example

๐Ÿ‘ค User: โ€œWhat is the latest news about Chandrayaan?โ€

๐Ÿค– AI: โ€œHmmโ€ฆ I donโ€™t have up-to-date info. Let me use my webSearch tool.โ€

๐ŸŒ AI uses Tavily to search the internet

โœ… AI gets live info and replies:

โ€œThe latest update is...โ€


๐Ÿงฑ How We Build This โ€” Step-by-Step Breakdown

Letโ€™s understand the code, tools, and flow in very clear steps.

๐Ÿ”น 1. Install Libraries (Tools We Use)

Open your terminal and type:


npm init -y
npm install groq-sdk @tavily/core dotenv readline

Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ง What Are These?

Library What It Does
groq-sdk Connects to Groq AI (like ChatGPT)
@tavily/core Searches the internet (real-time)
dotenv Stores API keys safely
readline Lets you type in the terminal

๐Ÿ”น 2. Add API Keys in .env

Create a .env file and add your keys:


GROQ_API_KEY=your_groq_api_key
TAVILY_API_KEY=your_tavily_api_key

Enter fullscreen mode Exit fullscreen mode

This keeps your keys safe and private.

๐Ÿ”น 3. The Core Concept: Tools and Tool Use

A tool is just a function that the AI can call.

You define it like this:

{
  type: "function",
  function: {
    name: "webSearch",
    description: "Search real-time information from the internet.",
    parameters: {
      type: "object",
      properties: {
        query: {
          type: "string",
          description: "The search query to use on the internet.",
        },
      },
      required: ["query"],
    },
  },
}


Enter fullscreen mode Exit fullscreen mode

Then you tell Groq:

"AI, here's a tool called webSearch. Use it when you need live info."

๐Ÿ”น 4. Full Working Code (Explained Line-by-Line)

import Groq from "groq-sdk";
import { tavily } from "@tavily/core";
import dotenv from "dotenv";
import readline from "readline";
import { readableStreamAsyncIterable } from "groq-sdk/lib/streaming.mjs";
dotenv.config();
const groq = new Groq({ apiKey: process.env.GROK_API_KEY });
const tvly = tavily({ apiKey: process.env.TAVILY_API_KEY });

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const systemPrompt = {
  role: "system",
  content: `You are Rohit, a Smart AI assistant. Make sure to give answers in paragraphs and in a way that is easy to understand for the user. Provide answer in the Indian Context.



You have Access to the Following Tools:
1. webSearch({ query: string }): This tool allows you to search the latest and real-time data from the Internet. You can use this tool to find information that is not available in your training data or to get the latest updates on a topic.
`,
};



async function main() {
  let messages = [systemPrompt];

  while (true) {
    const userInput = await ask("Ask your question :- ");

    messages.push({ role: "user", content: userInput });

    while(true) {
      const completion = await groq.chat.completions.create({
        messages: messages,
        tools: [
          {
            type: "function",
            function: {
              name: "webSearch",
              description: "Search the latest and Real time data from the Internet.",
              parameters: {
                type: "object",
                properties: {
                  query: {
                    type: "string",
                    description: "The search query to Perform Search from the Internet.",
                  },
                },
                required: ["query"],
              },
            },
          },
        ],
        tool_choice: "auto",
        model: "llama3-70b-8192",
      });

      messages.push(completion.choices[0].message);

      if(completion.choices[0].message.content) {
        console.log("\nAI Answer:\n" + completion.choices[0].message.content);
      } else {
        console.log("/n Tool calls made by the AI.");
      }

      const toolCalls = completion.choices[0].message.tool_calls;

      if (!toolCalls || toolCalls.length === 0) {
        console.log("No tool calls made by the AI.");
        break;
      }

      for (const call of toolCalls) {
        if (call.function.name === "webSearch") {
          const args = JSON.parse(call.function.arguments);
          const toolResult = await webSearch({ query: args.query });
          messages.push({
            role: "tool",
            tool_call_id: call.id, // REQUIRED for Groq API!
            name: "webSearch",
            content: toolResult,
          });

          // Call Groq again with updated messages to get the final answer
          const followUp = await groq.chat.completions.create({
            messages: messages,
            model: "llama3-70b-8192",
          });

          // Log the final AI answer after tool result
          const finalContent = followUp.choices[0].message.content;
          if (finalContent) {
            console.log("\nAI Final Answer:\n" + finalContent);
          } else {
            console.log("\nAI Final Answer: (No content returned)");
          }
        } else {
          console.log("Unknown tool call:", call);
        }
      }
    }

  }
}

function ask(query) {
  return new Promise((resolve) => rl.question(query, resolve));
}

async function webSearch({ query }) {
  const response = await tvly.search(query, {
    searchDepth: "advanced", // Must be a string 
    searchType: "web",
    searchLanguage: "en",
  });

  // Join all results into a single string
  const finalResult = response.results
    .map((result) => Array.isArray(result.content) ? result.content.join("\n") : result.content)
    .join("\n\n");

  return finalResult;
}



main();
Enter fullscreen mode Exit fullscreen mode

โœ… 1. Importing Required Packages

You start by importing libraries:

  • groq-sdk: This lets your app talk to Groqโ€™s AI models (like LLaMA 3).
  • @tavily/core: This connects your app to Tavily, which does real-time web searches.
  • dotenv: Allows your app to load secret keys (like API keys) from a .env file.
  • readline: Lets your app take input from the user in the terminal (like a chatbot).
  • readableStreamAsyncIterable: Used for reading streamed responses, though itโ€™s not actively used here.

๐Ÿ” 2. Load Environment Variables

You load the .env file using dotenv. This gives access to your API keys securely instead of writing them in the code.


๐Ÿ”Œ 3. Create Groq and Tavily Clients

You create instances of both Groq and Tavily using the API keys. These instances allow you to send requests to both platforms.


๐Ÿงพ 4. Set Up Terminal Interface

You use readline to make a simple question-and-answer interface in the terminal where the user can type their questions.


๐Ÿค– 5. Define System Prompt for the AI

You tell the AI how to behave:

  • Act like "Rohit", a smart assistant.
  • Answer in paragraph form, in simple, Indian-friendly language.
  • Also mention the tools available to it โ€” in this case, the webSearch tool.

๐Ÿ” 6. Start the Chat Loop

You start an infinite loop where the user can keep asking questions.

  • First, the user types a question.
  • The question gets added to the message history (a chat-like structure).

๐Ÿ’ฌ 7. AI Processes the Question

Now the Groq AI is called with the current message history and given access to tools (like webSearch).

You set tool_choice: "auto", which means:

  • The AI can decide for itself whether to answer directly or use a tool if it doesnโ€™t know something.

๐Ÿง  8. Store AIโ€™s First Response

You take the AIโ€™s response and add it to the message history.

  • If the AI replied with text, you print it to the user.
  • If the AI didn't give an answer, it probably tried to call a tool instead.

๐Ÿงฐ 9. Check If Any Tool Was Used

You check whether the AI tried to call any tools (like webSearch).

  • If no tool was called, the loop ends and waits for the next user question.
  • If a tool was called, you move to the next step.

๐ŸŒ 10. Handle the webSearch Tool Call

For each tool call:

  • You check if the AI called the webSearch tool.
  • If yes, you take the search query given by the AI.
  • You use Tavily to run the search using that query.
  • Tavily sends back search results from the real internet.

๐Ÿ”„ 11. Send Tool Result Back to AI

Once the tool returns the data:

  • You send the search result back to Groq as a tool response.
  • Then, you ask Groq again to complete the answer โ€” now that it has access to real-time data.
  • Groq replies with the final, full answer using both its training and the tool's result.
  • You print this final answer to the user.

๐Ÿ“ฆ 12. If Tool Call is Unknown (Fallback)

You also have a safety check in case the AI tries to call a tool that your app doesnโ€™t support. In this case, it logs that an unknown tool was requested.


โŒจ๏ธ 13. Ask Function

You created a helper function that waits for user input in the terminal and returns it as a Promise. This makes the chatbot interaction smooth.


๐Ÿ” 14. webSearch Function

You define a function that:

  • Uses Tavily to perform a live internet search.
  • Combines all the search results into one big string.
  • Returns that string back to the AI so it can use it in its response.

โ–ถ๏ธ 15. Start the App

Finally, you call the main() function to launch your chatbot.

Top comments (0)