██▒   █▓ ▒█████   ██▓▓█████▄ 
▓██░   █▒▒██▒  ██▒▓██▒▒██▀ ██▌
 ▓██  █▒░▒██░  ██▒▒██▒░██   █▌
  ▒██ █░░▒██   ██░░██░░▓█▄   ▌
   ▒▀█░  ░ ████▓▒░░██░░▒████▓ 
   ░ ▐░  ░ ▒░▒░▒░ ░▓   ▒▒▓  ▒ 
   ░ ░░    ░ ▒ ▒░  ▒ ░ ░ ▒  ▒ 
     ░░  ░ ░ ░ ▒   ▒ ░ ░ ░  ░ 
      ░      ░ ░   ░     ░    
     ░                 ░      
    

 ___      ___ ________  ___  ________     
|\  \    /  /|\   __  \|\  \|\   ___ \    
\ \  \  /  / | \  \|\  \ \  \ \  \_|\ \   
 \ \  \/  / / \ \  \\\  \ \  \ \  \ \\ \  
  \ \    / /   \ \  \\\  \ \  \ \  \_\\ \ 
   \ \__/ /     \ \_______\ \__\ \_______\
    \|__|/       \|_______|\|__|\|_______|
    

Yane ✖ Karov

  • Enhancing the PopOS Launcher with GPT Search Capabilities

    This guide explores the process of integrating a GPT search plugin with the PopOS Launcher for quick and easy access.


    At 4am, a thought crosses your mind:

    I really wish I had a GPT search just a click away...

    Well, guess what? You can!

    Seriously? But how?

    With a dash of digital wizardry and some hands-on tinkering


    Step 1: Obtain an API Key from OpenAI

    vist OpenAI and generate your unique API Key.

    Step 2: Install Python on Your System

    Run the following command to install Python 3:

    sudo apt install python3
    

    Step 3: Install revChatGPT Package Using pip

    Upgrade revChatGPT package using pip with the following command:

    python3 -m pip install --upgrade revChatGPT
    

    Alternatively, You can download the source code from the GitHub page

    Step 4: Verify Your Configuration

    python3 -m revChatGPT.V3 --api_key <your_new_api_key>
    

    Step 5: Craft a Chatbot

    Open a new file chat.py in your .local/bin directory and input the following script. Remember to replace the api_key variable with your API key:

    nano ~/.local/bin/chat.py
    

    Here's the Python script:

    #!/usr/bin/env python3
    
    import atexit
    import os
    import sys
    import traceback
    from revChatGPT.V3 import Chatbot
    
    PID_FILE = '/tmp/chat.pid'
    
    
    def delete_pid_file():
        if os.path.isfile(PID_FILE):
            os.remove(PID_FILE)
    
    
    if os.path.isfile(PID_FILE):
        print(f"{PID_FILE} already exists.")
        sys.exit()
    
    with open(PID_FILE, 'w') as f:
        f.write(str(os.getpid()))
    
    atexit.register(delete_pid_file)
    
    IN_FIFO = '/tmp/in_fifo'
    OUT_FIFO = '/tmp/out_fifo'
    
    # Ensure the IN_FIFO exists
    if not os.path.exists(IN_FIFO):
        os.mkfifo(IN_FIFO)
    
    # Ensure the OUT_FIFO exists
    if not os.path.exists(OUT_FIFO):
        os.mkfifo(OUT_FIFO)
    
    
    def chat_daemon():
        api_key = "<your_api_key>"
        chatbot = Chatbot(api_key=api_key, engine="gpt-3.5-turbo",
                          system_prompt="You're a Tea expert, respond accordingly.")
    
        with open(IN_FIFO, 'r') as in_fifo:
            with open(OUT_FIFO, 'w') as out_fifo:
                while True:
                    try:
                        query = in_fifo.readline().strip()
                        if query:  # Only ask the chatbot if query is not empty
                            response = chatbot.ask(query)
                            out_fifo.write(response + '\n')
                            out_fifo.flush()  # Ensure the response gets written immediately
                    except Exception as e:
                        out_fifo.write('ERROR: ' + str(e) + '\n')
                        out_fifo.write(traceback.format_exc())
                        out_fifo.flush()
    
    
    def main():
        chat_daemon()
    
    
    if __name__ == "__main__":
        main()
    

    Step 6: Give Your Script Executable Permissions

    Grant executable permissions to the script by using the following command:

    chmod +x ~/.local/bin/chat.py
    

    Step 7: Create a Fork of the WIP Search Plugin for the Launcher

    Either clone the existing fork of the plugin that I have created here, or create a new one from the original PopOS Launcher repository here:

    git clone https://github.com/canadaduane/launcher.git && cd launcher
    

    If you cloned my fork, skip to step 9

    Step 8: Switch to the 'search-plugin' Branch and Update:

    git checkout search-plugin && git pull origin search-plugin && git fetch
    

    Step 9: Build release version & install search feature

    Note this assumes you have a rust development environment already setup, if not here is a short guide on setting one up

    To build and install the search feature, use the following command:

    just build-release ; just plugins="search" install
    cd plugins/src/search
    

    Step 10: Implement Our New Search Plugin Functionality

    Define the configuration for your search plugin. Note: the top rule is mandatory.

    cat <<'EOF' > config.ron
    (
      rules: [
        (
          pattern: StartsWithKeyword(["f", "find"]),
          action: (
            query_command: "fdfind --ignore-case --full-path $KEYWORD1",
            output_captures: "^(.+)/([^/]+)$",
            result_name: "$CAPTURE2",
            result_desc: "$CAPTURE1",
            run_command: "xdg-open '$OUTPUT'",
          )
        ),
        (
          pattern: StartsWith(["="]),
          split: Regex("^="),
          action: (
            query_command: "qalc -u8 -set 'maxdeci 9' -t $KEYWORD1",
            result_name: "$KEYWORD1",
            result_desc: "$OUTPUT",
            run_command: "/bin/bash -c 'wl-copy \"$OUTPUT\" && notify-send \"Copied to clipboard\"'",
          )
        ),
        (
          pattern: StartsWithKeyword(["ls"]),
          action: (
            query_command: "ls -1 $KEYWORD1",
            result_name: "File",
            result_desc: "hi $OUTPUT",
            run_command: "xdg-open '$KEYWORD1/$OUTPUT'"
          )
        ),
        (
          pattern: StartsWithKeyword(["apt"]),
          action: (
            query_command: "apt list $KEYWORD1",
            output_captures: "^([^/]+)/(.+)$",
            result_name: "$CAPTURE1",
            result_desc: "$CAPTURE2",
            run_command: "notify-send '$OUTPUT'",
          )
        ),
        (
          pattern: StartsWithKeyword(["ps"]),
          action: (
            query_command: "/bin/bash -c 'ps --sort=-pcpu -axo pid,ucmd,pcpu | head -10'",
            output_captures: "^\\s+([0-9]+)\\s+(.*)$",
            result_name: "${CAPTURE2}",
            result_desc: "${CAPTURE1}",
            run_command: "notify-send '$OUTPUT'",
          )
        ),
        (
          pattern: StartsWithKeyword(["drives"]),
          action: (
            query_command: "lsblk -lno NAME,SIZE,MOUNTPOINTS",
            run_command: "notify-send '$OUTPUT'",
          )
        ),
      ]
    )
    EOF
    

    Step 11: Navigate Back to the Root Directory

    Use this command to go back to the root directory:

     cd ../../..
    

    Step 12: Rebuild the Release Version & Reinstall Search Feature

    Build the release version and reinstall the search feature using this command:

    just build-release ; just plugins="search" install
    

    Step 13: Usage

    Super Key & then type !gpt What is the meaning of life?

    If you want to watch the chatbot's responses in real-time, you can use the following command in a separate terminal window:

    watch -n 1 'cat /tmp/chat.log'
    

    Step 14: Enjoy!

    test

    Congratulations! You have successfully integrated the GPT search plugin with the PopOS Launcher. Enjoy the convenience and magic of GPT search at a press of a button.

    Git Links: python daemon && launcher fork

    © 2024 Yane ✖ Karov