From a1bfd806fa1a2d63a62b620eb024ac3465bba2a8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=8E=8B=E7=AB=8B=E5=B8=AE?= <3294713004@qq.com>
Date: Tue, 22 Apr 2025 01:53:11 +0800
Subject: [PATCH] =?UTF-8?q?feat(boards):=20micropython=E4=B8=8B=E6=B7=BB?=
=?UTF-8?q?=E5=8A=A0=E5=AF=B9openai=20chat=E6=8E=A5=E5=8F=A3=E7=9A=84?=
=?UTF-8?q?=E6=94=AF=E6=8C=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
boards/default_src/micropython/blocks/iot.js | 26 ++-
.../default_src/micropython/generators/iot.js | 31 +--
.../micropython/origin/build/lib/ollama.py | 4 +-
.../micropython/origin/build/lib/openai.py | 53 +++++
.../micropython_educore/template.xml | 79 ++++++++
.../micropython_esp32/template.xml | 113 +++++++++--
.../micropython_esp32c2/template.xml | 186 ++++++++++--------
.../micropython_esp32c3/template.xml | 116 +++++++++--
.../micropython_esp32s2/template.xml | 93 ++++++++-
.../micropython_esp32s3/template.xml | 115 +++++++++--
.../micropython_robot/template.xml | 81 +++++++-
11 files changed, 737 insertions(+), 160 deletions(-)
create mode 100644 boards/default_src/micropython/origin/build/lib/openai.py
diff --git a/boards/default_src/micropython/blocks/iot.js b/boards/default_src/micropython/blocks/iot.js
index e821ca79..69931a46 100644
--- a/boards/default_src/micropython/blocks/iot.js
+++ b/boards/default_src/micropython/blocks/iot.js
@@ -619,7 +619,7 @@ export const iot_onenetdisconnect = iot_onenet_disconnect;
export const iot_checkonenet = iot_onenet_check;
export const iot_publish = iot_onenet_publish;
-export const IOT_CONNECT_OLLAMA = {
+export const iot_connect_ollama = {
init: function () {
this.setColour(IOT_HUE);
this.appendDummyInput()
@@ -638,6 +638,30 @@ export const IOT_CONNECT_OLLAMA = {
}
};
+export const IOT_CONNECT_OLLAMA = iot_connect_ollama;
+
+export const iot_connect_openai = {
+ init: function () {
+ this.setColour(IOT_HUE);
+ this.appendDummyInput()
+ .appendField(Blockly.Msg.MIXLY_CONNECT_OPENAI);
+ this.appendValueInput('SERVER')
+ .appendField(Blockly.Msg.MIXLY_EMQX_SERVER)
+ .setAlign(Blockly.inputs.Align.RIGHT);
+ this.appendValueInput('KEY')
+ .appendField(Blockly.Msg.MIXLY_API_PRIVATE_KEY)
+ .setAlign(Blockly.inputs.Align.RIGHT);
+ this.appendValueInput('NAME')
+ .appendField(Blockly.Msg.MODEL_NAME)
+ .setAlign(Blockly.inputs.Align.RIGHT);
+ this.appendValueInput('NUMBER')
+ .appendField(Blockly.Msg.MIXLY_SET_MAXIMUM_HISTORICAL_SESSIONS_NUM);
+ this.setInputsInline(false);
+ this.setPreviousStatement(true);
+ this.setNextStatement(true);
+ }
+};
+
export const use_ollama_llm_to_chat = {
init: function () {
this.setColour(IOT_HUE);
diff --git a/boards/default_src/micropython/generators/iot.js b/boards/default_src/micropython/generators/iot.js
index 406b5368..9405df52 100644
--- a/boards/default_src/micropython/generators/iot.js
+++ b/boards/default_src/micropython/generators/iot.js
@@ -158,7 +158,7 @@ export const IOT_EMQX_INIT_AND_CONNECT_BY_SHARE_CODE = function (_, generator) {
// var mac_address = 'str(binascii.hexlify(wifi.radio.mac_address))[2:14]'
// var socket_pool = 'socketpool.SocketPool(wifi.radio)'
// var ssl_context = 'ssl.create_default_context()'
- var code = 'sk = analyse_sharekey(\'http://'+server.substring(1, server.length-1)+'/mixio-php/sharekey.php?sk=' + share_code + '\')\n'+
+ var code = 'sk = analyse_sharekey(\'http://' + server.substring(1, server.length - 1) + '/mixio-php/sharekey.php?sk=' + share_code + '\')\n' +
'MQTT_USR_PRJ = sk[0]+\'/\'+sk[1]+\'/\'\n' +
'mqtt_client = mixiot.init_MQTT_client(' + server + ', sk[0], sk[2]' + ', MQTT_USR_PRJ)\n';
return code;
@@ -235,18 +235,28 @@ export const iot_mqtt_data = function (_, generator) {
return [code, generator.ORDER_ATOMIC];
}
-export const IOT_CONNECT_OLLAMA = function(_,generator) {
- generator.definitions_['import_Ollama'] = "from ollama import Ollama";
+export const iot_connect_ollama = function (_, generator) {
+ generator.definitions_['import_ollama'] = "from ollama import Ollama";
var ser = generator.valueToCode(this, 'SERVER', generator.ORDER_ATOMIC);
var name = generator.valueToCode(this, 'NAME', generator.ORDER_ATOMIC);
var num = generator.valueToCode(this, 'NUMBER', generator.ORDER_ATOMIC);
- var code = 'llm = Ollama(' + ser + ', ' + name + ', '+ num +')\n';
+ var code = 'llm = Ollama(' + ser + ', ' + name + ', ' + num + ')\n';
return code;
+}
+export const IOT_CONNECT_OLLAMA = iot_connect_ollama;
+
+export const iot_connect_openai = function (_, generator) {
+ generator.definitions_['import_openai'] = "from openai import OpenAI";
+ var ser = generator.valueToCode(this, 'SERVER', generator.ORDER_ATOMIC);
+ var key = generator.valueToCode(this, 'KEY', generator.ORDER_ATOMIC);
+ var name = generator.valueToCode(this, 'NAME', generator.ORDER_ATOMIC);
+ var num = generator.valueToCode(this, 'NUMBER', generator.ORDER_ATOMIC);
+ var code = `llm = OpenAI(${ser}, ${key}, ${name}, ${num})\n`;
+ return code;
}
export const use_ollama_llm_to_chat = function (_, generator) {
- generator.definitions_['import_Ollama'] = "from ollama import Ollama";
var topic = generator.valueToCode(this, 'TOPIC', generator.ORDER_ATOMIC);
var method = generator.valueToCode(this, 'METHOD', generator.ORDER_ATOMIC);
var code = 'llm.chat(' + topic + ', ' + method + ')\n';
@@ -254,48 +264,41 @@ export const use_ollama_llm_to_chat = function (_, generator) {
}
export const use_ollama_llm_to_chat_return = function (_, generator) {
- generator.definitions_['import_Ollama'] = "from ollama import Ollama";
var topic = generator.valueToCode(this, 'TOPIC', generator.ORDER_ATOMIC);
var code = 'llm.chat(' + topic + ')';
- return [code,generator.ORDER_ATOMIC];
+ return [code, generator.ORDER_ATOMIC];
}
// export const ollama_set_timeout = function (_,generator) {
-// generator.definitions_['import_Ollama'] = "from ollama import Ollama";
// var t = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
// var code = 'llm.set_timeout(' + t + ')\n';
// return code;
// }
// export const ollama_set_max_retries = function (_,generator) {
-// generator.definitions_['import_Ollama'] = "from ollama import Ollama";
// var t = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
// var code = 'llm.set_max_retries(' + t + ')\n';
// return code;
// }
// export const ollama_set_custom_url = function (_,generator) {
-// generator.definitions_['import_Ollama'] = "from ollama import Ollama";
// var t = generator.valueToCode(this, 'TEXT', generator.ORDER_ATOMIC);
// var code = 'llm.set_custom_url(' + t + ')\n';
// return code;
// }
// export const ollama_select_model = function (_,generator) {
-// generator.definitions_['import_Ollama'] = "from ollama import Ollama";
// var t = generator.valueToCode(this, 'TEXT', generator.ORDER_ATOMIC);
// var code = 'llm.select_model(' + t + ')\n';
// return code;
// }
// export const ollama_clear_user_history = function (_,generator) {
-// generator.definitions_['import_Ollama'] = "from ollama import Ollama";
// var code = 'llm.clear_user_history()\n';
// return code;
// }
-export const ollama_empty_history = function (_,generator) {
- generator.definitions_['import_Ollama'] = "from ollama import Ollama";
+export const ollama_empty_history = function () {
var code = 'llm.empty_history()\n';
return code;
}
\ No newline at end of file
diff --git a/boards/default_src/micropython/origin/build/lib/ollama.py b/boards/default_src/micropython/origin/build/lib/ollama.py
index f8f7147b..e13c1bfa 100644
--- a/boards/default_src/micropython/origin/build/lib/ollama.py
+++ b/boards/default_src/micropython/origin/build/lib/ollama.py
@@ -60,7 +60,7 @@ class Ollama():
self._url, headers=self._heads, data=data)
if response.status_code == 200:
break
- time.slee(1)
+ time.sleep(1)
output = ""
@@ -101,7 +101,7 @@ class Ollama():
self.add_history("assistant", content)
messages_len = len(self._messages)
history_num = 2 * self._max_history_num
- while history_num < len(self._messages):
+ while history_num < messages_len:
del self._messages[0]
else:
self.clear_user_history()
diff --git a/boards/default_src/micropython/origin/build/lib/openai.py b/boards/default_src/micropython/origin/build/lib/openai.py
new file mode 100644
index 00000000..b4808ffb
--- /dev/null
+++ b/boards/default_src/micropython/origin/build/lib/openai.py
@@ -0,0 +1,53 @@
+import urequests
+import time
+import json
+import ollama
+
+
+class OpenAI(ollama.Ollama):
+ def __init__(self, url="", api_key="", model="", max_history_num=0, max_tokens=1024):
+ super().__init__(url, model, max_history_num)
+ self._heads["Authorization"] = "Bearer {}".format(api_key)
+ self._data["max_tokens"] = max_tokens
+ self._chat_url = "{}/chat/completions".format(self._url)
+
+ def _post(self, content_callback=None):
+ response = None
+ data = json.dumps(self._data).encode('utf-8')
+ for i in range(0, self._max_retries):
+ response = urequests.post(
+ self._chat_url, headers=self._heads, data=data)
+ if response.status_code == 200:
+ break
+ time.sleep(1)
+
+ output = ""
+
+ if response.status_code != 200:
+ output = response.text
+ if content_callback:
+ content_callback(output)
+ return output
+
+ if not content_callback:
+ output = json.loads(response.text)[
+ "choices"][0]["message"]["content"]
+ response.close()
+ return output
+
+ try:
+ while True:
+ line = response.raw.readline()
+ if line[:5] != b"data:":
+ continue
+ if line[-7:-1] == b"[DONE]":
+ break
+ line = line[6:-1]
+ line = line.decode('utf-8').strip()
+ data = json.loads(line)
+ content = data["choices"][0]["delta"]["content"]
+ content_callback(content)
+ output += content
+ finally:
+ response.close()
+ return output
diff --git a/boards/default_src/micropython_educore/template.xml b/boards/default_src/micropython_educore/template.xml
index cde6bf26..7d1fe7e7 100644
--- a/boards/default_src/micropython_educore/template.xml
+++ b/boards/default_src/micropython_educore/template.xml
@@ -7241,6 +7241,85 @@
+
+
+
+ 192.168.1.1
+
+
+
+
+ qwen2.5-coder:0.5b
+
+
+
+
+ 5
+
+
+
+
+
+
+ https://api.deepseek.com
+
+
+
+
+ API Key
+
+
+
+
+ deepseek-chat
+
+
+
+
+ 5
+
+
+
+
+
+
+ 请介绍一下米思齐?
+
+
+
+
+ content_callback
+
+
+
+
+
+
+
+ content_callback
+
+
+
+
+ content
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 请介绍一下米思齐?
+
+
+
+
diff --git a/boards/default_src/micropython_esp32/template.xml b/boards/default_src/micropython_esp32/template.xml
index cdce5e32..55a0a9dd 100644
--- a/boards/default_src/micropython_esp32/template.xml
+++ b/boards/default_src/micropython_esp32/template.xml
@@ -481,7 +481,7 @@
-
+
1
@@ -2929,7 +2929,7 @@
-
+
@@ -3051,11 +3051,11 @@
-->
-
-
- ble_keyboard_mouse
-
-
+
+
+ ble_keyboard_mouse
+
+
@@ -3066,10 +3066,10 @@
-
+
-
-
+
+
@@ -3088,7 +3088,7 @@
-
+
@@ -3972,12 +3972,12 @@
-
-
+
+
-
- xsensor
-
+
+ xsensor
+
@@ -4312,7 +4312,7 @@
-
+
500
@@ -7070,6 +7070,85 @@
+
+
+
+ 192.168.1.1
+
+
+
+
+ qwen2.5-coder:0.5b
+
+
+
+
+ 5
+
+
+
+
+
+
+ https://api.deepseek.com
+
+
+
+
+ API Key
+
+
+
+
+ deepseek-chat
+
+
+
+
+ 5
+
+
+
+
+
+
+ 请介绍一下米思齐?
+
+
+
+
+ content_callback
+
+
+
+
+
+
+
+ content_callback
+
+
+
+
+ content
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 请介绍一下米思齐?
+
+
+
+
diff --git a/boards/default_src/micropython_esp32c2/template.xml b/boards/default_src/micropython_esp32c2/template.xml
index 0e162111..21f0c8a0 100644
--- a/boards/default_src/micropython_esp32c2/template.xml
+++ b/boards/default_src/micropython_esp32c2/template.xml
@@ -455,7 +455,7 @@
-
+
1
@@ -1714,7 +1714,7 @@
-
+
@@ -1751,7 +1751,7 @@
-
+
Hello, Mixly!
@@ -1830,7 +1830,7 @@
-
+
@@ -2061,8 +2061,8 @@
-
-
+
+
@@ -2817,7 +2817,7 @@
-
+
@@ -2939,11 +2939,11 @@
-->
-
-
- ble_keyboard_mouse
-
-
+
+
+ ble_keyboard_mouse
+
+
@@ -2954,10 +2954,10 @@
-
+
-
-
+
+
@@ -2976,7 +2976,7 @@
-
+
@@ -3657,7 +3657,7 @@
-
+
xsensor
@@ -3863,13 +3863,13 @@
-
-
+
+
-
- xsensor
-
-
+
+ xsensor
+
+
@@ -4206,7 +4206,7 @@
-
+
500
@@ -4217,7 +4217,7 @@
weight
-
+
@@ -6961,63 +6961,85 @@
-
-
-
- 192.168.1.1
-
-
-
-
- qwen2.5-coder:0.5b
-
-
-
-
- 5
-
-
-
-
-
-
- 请介绍一下米思齐?
-
-
-
-
- content_callback
-
-
-
-
-
-
-
- content_callback
-
-
-
-
- content
-
-
-
-
-
-
-
-
-
-
-
-
-
- 请介绍一下米思齐?
-
-
-
-
+
+
+
+ 192.168.1.1
+
+
+
+
+ qwen2.5-coder:0.5b
+
+
+
+
+ 5
+
+
+
+
+
+
+ https://api.deepseek.com
+
+
+
+
+ API Key
+
+
+
+
+ deepseek-chat
+
+
+
+
+ 5
+
+
+
+
+
+
+ 请介绍一下米思齐?
+
+
+
+
+ content_callback
+
+
+
+
+
+
+
+ content_callback
+
+
+
+
+ content
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 请介绍一下米思齐?
+
+
+
+
diff --git a/boards/default_src/micropython_esp32c3/template.xml b/boards/default_src/micropython_esp32c3/template.xml
index 1d25086b..420157e7 100644
--- a/boards/default_src/micropython_esp32c3/template.xml
+++ b/boards/default_src/micropython_esp32c3/template.xml
@@ -455,7 +455,7 @@
-
+
1
@@ -1771,7 +1771,8 @@
-
+
+
@@ -2718,7 +2719,7 @@
-
+
@@ -2840,11 +2841,11 @@
-->
-
-
- ble_keyboard_mouse
-
-
+
+
+ ble_keyboard_mouse
+
+
@@ -2855,10 +2856,10 @@
-
+
-
-
+
+
@@ -2877,7 +2878,7 @@
-
+
@@ -3764,12 +3765,12 @@
-
-
+
+
-
- xsensor
-
+
+ xsensor
+
@@ -4107,7 +4108,7 @@
-
+
500
@@ -6874,6 +6875,85 @@
+
+
+
+ 192.168.1.1
+
+
+
+
+ qwen2.5-coder:0.5b
+
+
+
+
+ 5
+
+
+
+
+
+
+ https://api.deepseek.com
+
+
+
+
+ API Key
+
+
+
+
+ deepseek-chat
+
+
+
+
+ 5
+
+
+
+
+
+
+ 请介绍一下米思齐?
+
+
+
+
+ content_callback
+
+
+
+
+
+
+
+ content_callback
+
+
+
+
+ content
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 请介绍一下米思齐?
+
+
+
+
diff --git a/boards/default_src/micropython_esp32s2/template.xml b/boards/default_src/micropython_esp32s2/template.xml
index 10fc4c36..3168c883 100644
--- a/boards/default_src/micropython_esp32s2/template.xml
+++ b/boards/default_src/micropython_esp32s2/template.xml
@@ -481,7 +481,7 @@
-
+
1
@@ -3507,12 +3507,12 @@
-
-
+
+
-
- xsensor
-
+
+ xsensor
+
@@ -3849,7 +3849,7 @@
-
+
500
@@ -6605,6 +6605,85 @@
+
+
+
+ 192.168.1.1
+
+
+
+
+ qwen2.5-coder:0.5b
+
+
+
+
+ 5
+
+
+
+
+
+
+ https://api.deepseek.com
+
+
+
+
+ API Key
+
+
+
+
+ deepseek-chat
+
+
+
+
+ 5
+
+
+
+
+
+
+ 请介绍一下米思齐?
+
+
+
+
+ content_callback
+
+
+
+
+
+
+
+ content_callback
+
+
+
+
+ content
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 请介绍一下米思齐?
+
+
+
+
diff --git a/boards/default_src/micropython_esp32s3/template.xml b/boards/default_src/micropython_esp32s3/template.xml
index 0aac49fb..a78379ff 100644
--- a/boards/default_src/micropython_esp32s3/template.xml
+++ b/boards/default_src/micropython_esp32s3/template.xml
@@ -481,7 +481,7 @@
-
+
1
@@ -1486,8 +1486,8 @@
-
-
+
+
@@ -1789,7 +1789,7 @@
-
+
@@ -3086,7 +3086,7 @@
-
+
@@ -3134,7 +3134,7 @@
-
+
-
-
- ble_keyboard_mouse
-
-
+
+
+ ble_keyboard_mouse
+
+
@@ -3223,10 +3223,10 @@
-
+
-
-
+
+
@@ -3245,7 +3245,7 @@
-
+
@@ -4133,8 +4133,8 @@
-
-
+
+
xsensor
@@ -4476,7 +4476,7 @@
-
+
500
@@ -7290,6 +7290,85 @@
+
+
+
+ 192.168.1.1
+
+
+
+
+ qwen2.5-coder:0.5b
+
+
+
+
+ 5
+
+
+
+
+
+
+ https://api.deepseek.com
+
+
+
+
+ API Key
+
+
+
+
+ deepseek-chat
+
+
+
+
+ 5
+
+
+
+
+
+
+ 请介绍一下米思齐?
+
+
+
+
+ content_callback
+
+
+
+
+
+
+
+ content_callback
+
+
+
+
+ content
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 请介绍一下米思齐?
+
+
+
+
diff --git a/boards/default_src/micropython_robot/template.xml b/boards/default_src/micropython_robot/template.xml
index 4dc5af1b..2b49c794 100644
--- a/boards/default_src/micropython_robot/template.xml
+++ b/boards/default_src/micropython_robot/template.xml
@@ -468,7 +468,7 @@
-
+
1
@@ -5675,6 +5675,85 @@
+
+
+
+ 192.168.1.1
+
+
+
+
+ qwen2.5-coder:0.5b
+
+
+
+
+ 5
+
+
+
+
+
+
+ https://api.deepseek.com
+
+
+
+
+ API Key
+
+
+
+
+ deepseek-chat
+
+
+
+
+ 5
+
+
+
+
+
+
+ 请介绍一下米思齐?
+
+
+
+
+ content_callback
+
+
+
+
+
+
+
+ content_callback
+
+
+
+
+ content
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 请介绍一下米思齐?
+
+
+
+