Algorithms, Blockchain and Cloud

ChatGPT writes a Python Script to Interact with Grok LLM from x.ai (Free $25 Credit)


ChatGPT writes a Python Script to Interact with Grok LLM from x.ai (Free $25 Credit) | 调用x.ai的Grok

Free $25 Credit from X.AI

Earlier this year, Elon Musk’s X.AI company open-sourced the Grok large language model and provided $25 in free credit to use it. You can register for an account on the official website x.ai and apply for an API key. The examples to invoke the Grok are given as tutorials.

curl https://api.x.ai/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer xai-......" -d '{
  "messages": [
    {
      "role": "system",
      "content": "You are a test assistant."
    },
    {
      "role": "user",
      "content": "Testing. Just say hi and hello world and nothing else."
    }
  ],
  "model": "grok-beta",
  "stream": false,
  "temperature": 0
}'

Children, being underage, cannot sign up for accounts for large language models like ChatGPT or X.AI. They typically rely on the free Copilot integrated into Microsoft’s Bing search engine. Ryan mentioned today that ChatGPT no longer requires login to use, though this version is somewhat restricted.

You can create multiple API keys to use the Grok AI in different applications.

Longer prompts cost more. The cost is usage/token based. For simplicity you can think of a token is a word.

The API usage example to Grok AI – one prompt costs about $0.0012

Every account is granted $25 Free Credit to be used in 2024.

The X AI Dashboard

The X AI API Models: grok-beta
and grok-vision-beta.

ChatGPT Wrote a Python Interaction Program for X.AI’s Grok Model

Since the $25 credit is free, I decided to create a simple Python program for my child to interact with the Grok large language model and let them learn about real-world programming applications. I asked ChatGPT to write the program, and its generated code worked without issues.

import requests
import json

api_key = "x_ai ..."

# Define the API endpoint and headers
url = "https://api.x.ai/v1/chat/completions"
headers = {
    "Content-Type": "application/json",
    f"Authorization": "Bearer {api_key}",
}

# Define a system message for context
system_message = {"role": "system", "content": "You are a test assistant."}

print("Welcome to the Grok, an AI chatbot. Type 'bye' to exit.\n")

while True:
    # Prompt the user for input
    user_input = input("You: ").strip()

    # Check if the user wants to exit
    if user_input.lower() == "bye":
        print("Goodbye!")
        break

    if user_input == "":
        continue

    # Define the payload
    payload = {
        "messages": [
            system_message,
            {"role": "user", "content": user_input}
        ],
        "model": "grok-beta",
        "stream": False,
        "temperature": 0
    }

    try:
        # Make the request
        response = requests.post(url, headers=headers, json=payload)

        # Check the response status
        if response.status_code == 200:
            data = response.json()
            assistant_response = data["choices"][0]["message"]["content"]
            print(f"Grok: {assistant_response}\n")
        else:
            print(f"Error: {response.status_code} - {response.text}")
    except Exception as e:
        print(f"An error occurred: {e}")

I made some small modifications, such as avoiding empty prompts and using the strip function to remove leading and trailing spaces. Since the child uses a Mac, they needed to install Homebrew in Terminal, install Python, and then install the requests package with pip3 install requests to use the program.

Ryan is playing around with the Grok AI using the Python script that is developed by ChatGPT.

The simple Prompt to Grok on Mac Terminal

Large Language Model

ChatGPT Use Cases

ChatGPT - AGI (Artificial General Intelligence)

Grok 3 AI

ChatGPT Fails to

–EOF (The Ultimate Computing & Technology Blog) —

982 words
Last Post: Leetcode DCC Winner T-shirt
Next Post: Scammers Are Exploiting Multi-Signature Wallets (Tron): Stay Alert!

The Permanent URL is: ChatGPT writes a Python Script to Interact with Grok LLM from x.ai (Free $25 Credit) (AMP Version)

Exit mobile version