-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestJson.py
83 lines (72 loc) · 2.43 KB
/
TestJson.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from langchain.prompts import (
ChatPromptTemplate,
MessagesPlaceholder,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate
)
from langchain.chains import ConversationChain
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferWindowMemory
from dotenv import load_dotenv
import json
import paho.mqtt.client as mqtt
MQTT_SERVER = "broker.mqttdashboard.com"
CLIENTID = "solomon_client_00098"
PASSWORD = ""
SUBTOPIC_LED = "esp32-dht22/LED"
SUBTOPIC_DOOR = "esp32-dht22/DOOR"
# 連線設定
# 初始化地端程式
client = mqtt.Client()
# 設定登入帳號密碼
client.username_pw_set(CLIENTID)
# 設定連線資訊(IP, Port, 連線時間)
client.connect(MQTT_SERVER, 1883, 60)
llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo")
k = 0 # keep the last {k} interactions in memory
memory = ConversationBufferWindowMemory(k = k, return_messages = True)
def create_chat(state):
system_message = f"""
`no explanations`
`no prompt`
You are a wise steward, The current environment is represented in JSON:
{{{json.dumps(state)}}}
Please check the environment to update JSON.
Format your response as a JSON object with "msg", "light", "door" keys.
"""
prompt = ChatPromptTemplate.from_messages([
SystemMessagePromptTemplate.from_template(system_message),
MessagesPlaceholder(variable_name = "history"),
HumanMessagePromptTemplate.from_template("{input}")
])
conversation = ConversationChain(
memory = memory, prompt = prompt, llm = llm, verbose = True
)
return conversation
def get_state(response):
state = json.loads(response[response.find("{"):response.find("}") + 1])
client.publish(SUBTOPIC_LED, "on" if state["light"] == 1 else "off")
client.publish(SUBTOPIC_DOOR, "on" if state["door"] == 1 else "off")
# state = json.loads(response)
return state
def main():
load_dotenv()
state = {
"light": 0,
"door": 0,
"msg": "The light is off, the door is closed"
}
while True:
print(json.dumps(state))
user_input = input("> ")
if len(user_input) == 0:
continue
conversation = create_chat(state)
response = conversation.predict(input=user_input)
print(f"Assistant: {response}\n")
try:
state = get_state(response)
except Exception as e:
print(e)
if __name__ == '__main__':
main()