AI

A Guide to Using xAI, Grok, ChatGPT, DALL-E & Gemini AI APIs

In this digital age, the integration of AI into applications has become not just a trend but a necessity. This guide will walk you through how to leverage the power of AI through APIs from some of the leading providers: Grok, xAI, ChatGPT, DALL-E and Gemini.

xAI (AKA Grok)

Grok is an AI chatbot developed by xAI, a company founded by Elon Musk.

It was launched in November 2023 and is designed to be a conversational AI with a sense of humor and wit. The name “Grok” comes from a term in Robert A. Heinlein’s science fiction novel Stranger in a Strange Land meaning to understand something intuitively

Example in bash

curl https://api.x.ai/v1/chat/completions \\
  -H "Content-Type: application/json" \\
  -H "Authorization: Bearer $XAI_API_KEY" \\
  -d '{
        "messages": [
          {
            "role": "system",
            "content": "You are Grok, a chatbot inspired by the Hitchhikers Guide to the Galaxy."
          },
          {
            "role": "user",
            "content": "What is the meaning of life, the universe, and everything?"
          }
        ],
        "model": "grok-beta",
        "stream": false,
        "temperature": 0
      }'

ChatGPT API

ChatGPT is developed by OpenAI, known for its conversational AI capabilities. It has evolved through several iterations, with the latest updates focusing on integrating more advanced models like GPT-4o, which not only handles text but also images and sounds, reducing the need for separate models like DALL-E for images or Whisper for voice.

Example in python

import openai

openai.api_key = 'your-api-key'

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Tell me a joke."}
]

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=messages
)

reply = response['choices'][0]['message']['content']
print(reply)

DALL-E API

DALL-E, also from OpenAI, is renowned for its ability to generate images from textual descriptions. Its integration with ChatGPT, especially with the advent of DALL-E 3, has made it possible to generate images directly within text conversations.

Example in python

import openai

openai.api_key = 'your-api-key'

response = openai.Image.create(
    prompt="A robot wearing a hat and sunglasses",
    n=1,
    size="256x256",
    response_format="b64_json"
)

image_data = response['data'][0]['b64_json']

with open("robot.png", "wb") as f:
    f.write(base64.b64decode(image_data))

Gemini API

Developed by Google, Gemini represents Google’s push into advanced generative AI, aiming to compete directly with the likes of ChatGPT. Initially known as Bard, it has since been rebranded and enhanced significantly.

Example in python

import google.generativeai as genai

genai.configure(api_key='your-api-key')

model = genai.GenerativeModel('gemini-pro')

response = model.generate_content("Write a story about a magic backpack.")

print(response.text)

Closing thoughts

Accessing the API of major generative AIs is actually much more straightforward that one would think. Costs are incredibly cheap for the value you get and integration into your programs (especially python) is as simple as making a single call to the API.

The Free AI Exam Guide

If you’re interested in taking your first steps into AI certification, be sure to checkout my free guide on preparing for the Microsoft AI Fundamentals Exam (AI-900).

You can download the PDF here.

author avatar
Graham
Graham works closely with VMware & Microsoft solutions. He is a VMware Certified Implementation Expert, 8x VMware vExpert & a VMware User Moderator on the official VMware VMTN forums. Feel free to reach out via Twitter @VirtualG.uk or email: contact@virtualg.uk

Leave a Response

This site uses Akismet to reduce spam. Learn how your comment data is processed.