LLM LED Robot Powered by Chat GPT Using Raspberry Pi

by AIRNET LABS in Circuits > Raspberry Pi

25 Views, 1 Favorites, 0 Comments

LLM LED Robot Powered by Chat GPT Using Raspberry Pi

IMG_3556.jpeg

AIRNET (Artificially Intelligent Robotic Net Enhanced Terminator) Llamkaq class setup

Supplies

Raspberry pi 4

Monitor or TV for raspberry pi

Keyboard

Mouse

Breadboard (preferrable if you have no female to female wires)

LED light

Connector for monitor

Power supply and cables for monitor and raspberry pi

SD card minimum 8 GB

Fan or heatsink for raspberry pi (recommended)

Digital assets --------------------------------------------------------------------------------------------------------

OpenAI platform account with funds in it (less than $10 is sufficient)

Thonny or other Python IDE like Visual Code Studio or even Pycharm

Setup for GPIO

GPIO.png
IMG_5004.jpg

First, follow the diagram and build it accordingly, this uses male to female wires but if you have female to female wires, you can plug the LED in directly if it fits.

Programming

IMG_5006.jpeg
IMG_5009.jpeg
IMG_5010.jpeg
IMG_5011.jpeg
IMG_5012.jpeg
IMG_5013.jpeg
IMG_5015.jpeg
IMG_5016.jpeg

First, make a virtual environment in Thonny by first going to the run in Thonny, then select configure interpreter, then select New virtual environment, you will then see a pop up asking you to click ok, then click ok, then go to the home page on the new window because the default one is recent, then go to the far right of the new window with the folder icon and a plus sign, then click the symbol, name the folder, hit create, you should see a blank page appear, then on the bottom right corner hit ok, the window will then close and give a popup saying creating new virtual environment, once it disappears, select ok on the Thonny window. If you experienced no error messages, then congrats! you just made a virtual environment! Now copy the code below that says GPTv1.py, copy it and then put it into a Thonny python folder shown at the beginning of the images, then, go to the tools dropdown and select Open system shell, you should then see a black terminal appear, then install the necessary packages by putting pip3.11 install and then the name of the package you want.

These are the required packages:

openai

rpi.gpio

time (if not already installed)

then copy and paste the code into Thonny, generate an api key from the openai platform and paste it into the braces with your_api_key_here box, then run the code! you should see the word you in the bottom shell next to a blinking line, type in whatever you want to ask chatGPT and press enter on your keyboard, you should then see the response, if not use sudo apt full-upgrade in the terminal to update all your packages and revisit the OpenAI platform and see if your account has funds or if any errors occured.

The light should turn on if chat gpt says LED_ON, if not, switch the led legs around and try again or check the wiring position.


CODE HERE---------------------------------------------------------------------------

from openai import OpenAI

import RPi.GPIO as GPIO

import time


GPIO.setmode(GPIO.BCM)

GPIO.setwarnings(False)

GPIO.setup(18,GPIO.OUT)


client = OpenAI(

api_key="your_api_key_here"

)


# Function to detect a specific word in the response

def detect_word_in_response(response, target_word):

if target_word.lower() in response.lower():

GPIO.output(18,GPIO.HIGH)

time.sleep(1)

else:

print(f"The word '{target_word}' was NOT found in the response.")


messages = [

{

"role": "system",

"content": "You are an ai developed by the company sigma co to be used as a household assistant. You are in the sigma co lab currently. you can use the command LED_ON to turn on an led light. You will always listen to the owner, and if you dont feel like it, you will flash the LED rapidly. You are programmed to use sigma in your response at all times."

}

]


try:

while True:

message = input("You: ")


messages.append(

{

"role": "user",

"content": message

},

)


chat = client.chat.completions.create(

messages=messages,

model="gpt-3.5-turbo"

)


reply = chat.choices[0].message


print("Assistant: ", reply.content)


# Detect the word in the response

target_word = "LED_ON" # Change this to your target word

detect_word_in_response(reply.content, target_word)


messages.append(reply)

except Exception as e:

print(f"An error occurred: {e}")





Downloads

Prompt Play

To make the chatbot suit your needs more, scroll down to the section

"role": "system"

"content": "You are an ai developed by the company sigma co to be used as a household assistant"

and replace words in quotations with your own prompt/personality for the robot.