Category: Python

  • Automatic AI Author (AAA) for WordPress

    Automatic AI Author (AAA) for WordPress

    Create and post content without human intervention

    Say you had a blog on any topic and wanted AI (OpenAi, xAi) to automatically write or translate existing content for you and post it directly to your WordPress website.

    1. Add user to WordPress with Application Password
      After adding a new User (or use an existing one) set an application password in WordPress (Users -> Edit User)
    # RSS_AI_Wordpress
    
    import requests
    import json
    import base64 
    from _AI_Writer import get_news_response
    response = get_news_response("What are the main headlines today?")
    
    # WordPress API endpoint
    url = "https://YOURWEBSITE.com/wp-json/wp/v2/posts"
    
    # Authentication credentials
    user = "BOT"
    password = "YOUR_APPLICATION_PASSWORT_MATE"
    credentials = user + ':' + password
    token = base64.b64encode(credentials.encode())
    header = {
        'Authorization': 'Basic ' + token.decode('utf-8'),
        'Content-Type': 'application/json; charset=utf-8',
        'Accept': 'application/json, */*',
        'User-Agent': 'Python/RequestsClient'
    }
    
    # Post content to WordPress
    post = {
        'title': 'AI BOT - Daily News',
        'content': response,
        'status': 'publish',
    }
    
    # Send POST request with verify=False to debug SSL issues
    response = requests.post(url, headers=header, json=post, verify=True)
    
    # Check if the request was successful
    if response.status_code == 201:  # 201 is the success code for creation
        print("Post created successfully!")
        #print(response.json())
    else:
        print(f"Error: {response.status_code}")
        print(response.text)

    This code posts automatically to your WordPress blog. The actual content (stored in “response”) we retrieve from a module called _AI_Writer.

    2. Writing Content with Your AI Writer Bot

    Our AI writer module fetches an RSS Feed (Google News in our case; bur could be any website or feed) and writes a short blog post in his own words on today’s news. This gets posted directly to our blog (see code above).

    # _AI_Writer.py
    
    import os
    from openai import OpenAI
    import feedparser
    
    XAI_API_KEY = "YOUR_XAI_KEY_HERE"
    client = OpenAI(
        api_key=XAI_API_KEY,
        base_url="https://api.x.ai/v1",
    )
    
    def chat_with_gpt(prompt):
        response = client.chat.completions.create(
            model = "grok-beta",
            messages=[{"role": "user", "content": prompt}],
            #temperature = 0.8,
        )
        return response.choices[0].message.content.strip()
    
    def get_rss_feed(url):
        """Fetch and parse RSS feed from given URL"""
        feed = feedparser.parse(url)
        return feed
    
    def get_feed_entries(feed, limit=10):
        """Extract entries from feed, with optional limit"""
        entries = []
        for entry in feed.entries[:limit]:
            entries.append({
                'title': entry.get('title', ''),
                'link': entry.get('link', ''),
                'published': entry.get('published', ''),
                'summary': entry.get('summary', '')
            })
        return entries
    
    def get_news_response(user_input):
        """Get AI response based on RSS feed news and user input"""
        rss_url = "https://news.google.com/news/rss"
        feed = get_rss_feed(rss_url)
        entries = get_feed_entries(feed)
        
        prompt = f"""Here are the latest news entries. {user_input}
    
    {[entry['title'] + ': ' + entry['summary'] for entry in entries]}"""
        
        return chat_with_gpt(prompt)
    
    # Modified main block for testing
    if __name__ == "__main__":
        # Test the module
        response = get_news_response("Please provide a brief summary")
        print("Test response:", response)
            

    Like all AI workflows this offers a plethora of use cases

    You could have it fill a website with articles without ever touching said website. Or maybe translate content of one website and repost content on another.

    Or maybe – if you are evil – scale this x 1000 and fill hundreds of websites with your propaganda. Unfortunately this is all too easy.

  • Python xAI Chatbot Tutorial

    Python xAI Chatbot Tutorial

    Make your own xAI chatbot in 3 minutes

    All the information I found was incomplete or simply did not work. ChatGPT was not much help either. All AI overlord code did not work.

    So here it is. How to make your own xAI Chatbot?

    1. Install Python and OpenAi library
    pip install openai
    1. Create your xAI account and get your API key
      Visit the API Keys page within the console. Here, you’ll create your very own API key, the magical token that grants you access to the Grok API.
    2. Run Python in Visual Studio Code (or wherever)
    import os
    from openai import OpenAI
    
    XAI_API_KEY = "xai-YOUR_KEY_COMES_HERE_MATE"
    client = OpenAI(
        api_key=XAI_API_KEY,
        base_url="https://api.x.ai/v1",
    )
    
    def chat_with_gpt(prompt):
        response = client.chat.completions.create(
            model = "grok-beta",
            messages=[{"role": "user", "content": prompt}],
            #temperature = 0.8,
        )
        return response.choices[0].message.content.strip()
    
    if __name__ == "__main__":
        while True:
            user_input = input("Tom: ")
            if user_input.lower() in ["quit", "exit", "bye"]:
                break
            
            response = chat_with_gpt(user_input)
            print("AIgnostic: ", response)

    That’s all. You got your own little chatbot my friend.

  • Python ChatGPT Chatbot Tutorial

    Python ChatGPT Chatbot Tutorial

    How to set up your own Chatbot interface with Python in 3 minutes?

    1. Get an OpenAI account and get your API key

      https://platform.openai.com
    2. Install Python and the OpenAI Python library
    pip install openai

    3. Run Python in Visual Studio Code (or wherever)

    import openai
    openai.api_key = "sk-YOUR_KEY_COMES_HERE_MATE"
    
    def chat_with_gpt(prompt):
        response = openai.chat.completions.create(
            model = "o1-mini",
            messages=[
                {"role": "assistant", "content": "You are a helpful assistant." },
                {"role": "user", "content": prompt}
                ],
            #temperature = 0.7,
        )
        return response.choices[0].message.content.strip()
    
    if __name__ == "__main__":
        while True:
            user_input = input("Tom: ")
            if user_input.lower() in ["quit", "exit", "bye"]:
                break
            
            response = chat_with_gpt(user_input)
            print("AIgnostic: ", response)

    You can specify any model you would like to test.