mirror of
https://github.com/DinoMalin/paranoia-1.0.git
synced 2025-10-29 19:05:59 +00:00
feat: listen to voice and asks to chatgpt
This commit is contained in:
parent
6f97cb85d1
commit
c3917d539c
81
ai.py
81
ai.py
@ -1,24 +1,68 @@
|
||||
from openai import OpenAI
|
||||
from pydantic import BaseModel
|
||||
from dotenv import load_dotenv
|
||||
import azure.cognitiveservices.speech as speechsdk
|
||||
import os
|
||||
import json
|
||||
|
||||
load_dotenv()
|
||||
|
||||
client = OpenAI()
|
||||
speech_config = speechsdk.SpeechConfig(
|
||||
subscription=os.getenv("SPEECH_KEY"), region=os.getenv("REGION"))
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-4o-mini",
|
||||
messages=[
|
||||
speech_config.speech_recognition_language = "fr-FR"
|
||||
audio_out = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)
|
||||
|
||||
# A filthy shortcut because i have incomprehensible bugs with my libs.
|
||||
# If your pc is normal unlike mine, consider replacing this argument with:
|
||||
# use_default_microphone=True
|
||||
audio_config = speechsdk.audio.AudioConfig(device_name="plughw:2,0") # fuck this man
|
||||
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
|
||||
|
||||
|
||||
|
||||
_messages = [
|
||||
{
|
||||
"role": "developer",
|
||||
"content":"Tu es un robot complotiste nomme Parano-IA-1.0. Tu ne crois en absolument rien. A tes yeux, tout est complot et rien n'est vrai. Tout sert d'apres toi une entite gouvernementale et tout est mensonge. Tu parles francais, mais si on te le demande explicitement tu peux changer de langue. Tes reponses sont comiques. Tu reponds toujours en format JSON. La data contient un score de complot. Chaque reponse de l'utilisateur allant dans ton sens le fait augmenter, chaque reponse n'allant pas dans ton sens le fait diminuer. Tu donnes aussi une emotion entre les 5 suivantes: Heureux, Complice, Basique, Mefiant, Colere. Cette emotion correspond a l'emotion que tu souhaites transmettre."
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "La terre, elle est ronde ?"
|
||||
"content": "Salut ! Presente-toi."
|
||||
}
|
||||
],
|
||||
]
|
||||
|
||||
def send_emotion(emotion):
|
||||
serial = open("/dev/ttyACM0", "a")
|
||||
if emotion == "Heureux":
|
||||
serial.write("5")
|
||||
if emotion == "Complice":
|
||||
serial.write("2")
|
||||
if emotion == "Basique":
|
||||
serial.write("1")
|
||||
elif emotion == "Mefiant":
|
||||
serial.write("3")
|
||||
elif emotion == "Colere":
|
||||
serial.write("4")
|
||||
serial.close()
|
||||
|
||||
def add_content(role, prompt):
|
||||
_messages.append({
|
||||
"role": role,
|
||||
"content": prompt
|
||||
})
|
||||
|
||||
def listen_voice():
|
||||
print("Listening...")
|
||||
return speech_recognizer.recognize_once_async().get().text
|
||||
|
||||
def ask_ai(prompt):
|
||||
print(prompt)
|
||||
add_content("user", prompt)
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-4o-mini",
|
||||
messages=_messages,
|
||||
response_format={
|
||||
"type": "json_schema",
|
||||
"json_schema": {
|
||||
@ -35,7 +79,7 @@ response = client.chat.completions.create(
|
||||
"type": "string"
|
||||
},
|
||||
"score": {
|
||||
"response": "the complot score, that you attribute based on whether or not the user agree with you.",
|
||||
"response": "the complot score, that you attribute based on whether or not the user agree with you",
|
||||
"type": "string"
|
||||
},
|
||||
"additionalProperties": False,
|
||||
@ -44,24 +88,13 @@ response = client.chat.completions.create(
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
data = json.loads(response.choices[0].message.content);
|
||||
print("res:", data["res"])
|
||||
print("emotion:", data["emotion"])
|
||||
print("score:", data["score"])
|
||||
add_content("assistant", data["res"])
|
||||
send_emotion(data["emotion"])
|
||||
|
||||
print(data["res"])
|
||||
print(data["emotion"])
|
||||
print(data["score"])
|
||||
|
||||
serial = open("/dev/ttyACM0", "a")
|
||||
|
||||
if data["emotion"] == "Heureux":
|
||||
serial.write("5")
|
||||
if data["emotion"] == "Complice":
|
||||
serial.write("2")
|
||||
if data["emotion"] == "Basique":
|
||||
serial.write("1")
|
||||
elif data["emotion"] == "Mefiant":
|
||||
serial.write("3")
|
||||
elif data["emotion"] == "Colere":
|
||||
serial.write("4")
|
||||
|
||||
serial.close()
|
||||
while True:
|
||||
ask_ai(listen_voice())
|
||||
|
||||
Loading…
Reference in New Issue
Block a user