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}")



