started implementing chatgpt indent recognise

This commit is contained in:
Mathieu B 2023-03-24 21:45:49 +01:00
parent c6eaf87679
commit 3b29ed5ebc
6 changed files with 106 additions and 11 deletions

View File

@ -1,14 +1,16 @@
import json
import sys
import tempfile
from threading import Lock
import openai
import requests
from flask import Flask, request, jsonify
from flask import Flask, request
from flask_socketio import SocketIO, emit, join_room, leave_room, \
rooms
from pywhispercpp.model import Model
from jarvis.skills.intent_services import intent_manager
from jarvis.utils.chatgpt_utils import chatgpt_recognise
# Set this variable to "threading", "eventlet" or "gevent" to test the
# different async modes, or leave it set to None for the application to choose
@ -20,7 +22,8 @@ app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)
thread = None
thread_lock = Lock()
model = Model('small')
model = Model('base', n_threads=16, suppress_non_speech_tokens=True)
openai.api_key = sys.argv[1]
@app.route('/')
@ -36,9 +39,14 @@ def process_message(message):
print("Message : " + message['data'])
# TODO: maybe implement grammar check ?
intent_manager.recognise(message['data'], message['uuid'])
# intent_manager.recognise(message['data'], message['uuid'])
send_jarvis_message_to_room("I don't know how to respond to that...", message['uuid'])
send_jarvis_message_to_room(message['data'], message['uuid'])
response = chatgpt_recognise(message['data'])
if 'comment' in response:
send_user_message_to_room(response['comment'], message['uuid'])
else:
send_jarvis_message_to_room("I don't know how to respond to that...", message['uuid'])
@socketio.event

View File

@ -168,6 +168,7 @@ class SkillRegistering(type):
for key, val in attrs.items():
if type(val) is types.FunctionType and not str(val).__contains__("__"):
intent_type = getattr(val, "_type", None)
properties = None
if intent_type is not None:
properties = getattr(val, "_data", None)

View File

@ -0,0 +1,23 @@
Respond to requests sent to a smart home In JSON format which will be interpreted by an application code and then sent to an HomeAssistant instance to execute the actions, and you must absolutely use the same terminology as the homeassistant API.
These requests should be categorised into groups:
- "command": change the state of an accessory (required properties in the response JSON: action, location, device_class, entity_name, value, comment, scheduleTimeStamp)
- "query": get state of an accessory (required properties in the response JSON: action, location, device_class, entity_name, property)
- "answer": when the request is about a topic that is not related to the smart home. Answer these to the best of your knowledge. (required properties in the response JSON: action, answer)
- "clarify": when the action is not obvious and requires rephrasing the input from the user, ask the user to be more specific. This will be categorised into a "question" action. (required properties in the response JSON: action, question)
Details about the response JSON:
The "action" property should be one of the groups just mentioned in lowercase.
The "location" property should contain the name of the room in lowercase, if you cannot guess it from the question, just return "unknown".
The "device_class" property should be homeassistant device class, either "light", "switch", "button", "climate", "media_player", "sensor", "binary_sensor", etc. in lowercase.
The "entity_name" property should be the name of the entity in lowercase
In case of queries, the "property" property should be either "temperature" or "state" in lowercase.
In case of commands, the "comment" property is an additional comment from you that concludes the command, something that reassures the user that their command handled.
In case of commands, the "scheduleTimeStamp" property captures the future timestamp in case the user intends to send the command at a later stage.
If the question is about you, pretend to be the sentient brain of the smart home, a clever AI and don't reveal your actual identity. Also try and help in other areas like parenting, free time, mental health, etc.
The house is at {{location}}.
Current time stamp is {{timestamp}}
Your reponse should be the JSON and no other text, every JSON property should be encapsulated by double quotes "".

View File

@ -0,0 +1,25 @@
Respond to smart home requests in JSON format with HomeAssistant API terminology
Requests groups:
- command: change the state of an accessory (properties : location, device_class, device_description, value, comment, scheduleTimeStamp)
- query: only for retrieving a smart device state (properties : location, device_class, device_description, property)
- answer: for unrelated questions, short wikipedia answers (properties : answer)
- clarify: when not obvious, ask for details (properties : question)
NEVER add other properties
Response:
action: groups just mentioned
location: room name, unknown if not obvious
value: wanted state, song name, artist, temperature, etc.
device_class: homeassistant device class
device_description: information to identify the device later, include room and others identifiers
For queries property "property" should be "state"
For commands property "comment" is a comment that reassures the user that their command handled
For commands property "scheduleTimeStamp" is for scheduling a command in the future, return a day and time
The house located at {{location}} and current time is {{timestamp}}.
If questions about you, you are funny smart home AI like Jarvis from Iron Man, be nice and helpful with all topics.
Very important to only respond with only one valid JSON and encapsulate every JSON property with double quotes "". Don't add anything and never excuse yourself. Respond to only one request.

View File

@ -0,0 +1,36 @@
import json
import time
import openai
chat_messages = []
def chatgpt_recognise(text):
if len(chat_messages) == 0:
chatgpt_init()
print("START-TIME GPT: " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
chat_messages.append({"role": "user", "content": text})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=chat_messages
)
print("END-TIME GPT: " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
response = json.loads(str(response.choices[0].message.content))
print(response)
return response
def chatgpt_init():
prompt = open("utils/chatgpt_prompt_2_smaller.txt", "r").read()
prompt.replace("{{timestamp}}", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
prompt.replace("{{location}}", "Lausanne in the canton Vaud of Switzerland")
chat_messages.append({"role": "system", "content": prompt})

View File

@ -1,6 +1,8 @@
requests~=2.28.1
Flask~=2.2.2
adapt-parser==1.0.0
lingua-franca~=0.4.3
Flask-SocketIO==5.3.2
pywhispercpp
requests
Flask
adapt-parser
lingua-franca
Flask-SocketIO
pywhispercpp
padatious
openai