diff --git a/boards/default/micropython/build/lib/map.json b/boards/default/micropython/build/lib/map.json index bad68088..8cfa2639 100644 --- a/boards/default/micropython/build/lib/map.json +++ b/boards/default/micropython/build/lib/map.json @@ -589,6 +589,16 @@ "__size__": 481, "__name__": "oled128x64.py" }, + "ollama": { + "__require__": [ + "urequests", + "time", + "json" + ], + "__file__": true, + "__size__": 3329, + "__name__": "ollama.py" + }, "onenet": { "__require__": [ "ujson", diff --git a/boards/default/micropython/build/lib/ollama.py b/boards/default/micropython/build/lib/ollama.py new file mode 100644 index 00000000..5f777a1b --- /dev/null +++ b/boards/default/micropython/build/lib/ollama.py @@ -0,0 +1,109 @@ +import urequests +import time +import json + + +class Ollama(): + def __init__(self, url="", model=""): + self._heads = { + "Accept": "text/event-stream", + # "Cache-Control": "no-cache", + # "Connection": "keep-alive", + "Content-Type": "application/json" + } + self._url = url + self._max_retries = 1 + self._max_history_num = 0 + self._timeout = 10000 + self._messages = [] + self._data = { + "stream": True, + "model": model, + "messages": self._messages + } + + def set_timeout(self, timeout): + self._timeout = timeout + + def set_max_retries(self, max_retries): + self._max_retries = max_retries + + def set_custom_url(self, url): + self._url = url + + def select_model(self, model_name): + self._data["model"] = model_name + + def set_max_history_num(self, num): + self._max_history_num = num + + def empty_history(self): + self._messages = [] + + def add_history(self, role, content): + self._messages.append({ + "role": role, + "content": content + }) + + def clear_user_history(self): + if len(self._messages) < 2: + return + for i in range(1, len(self._messages)): + del self._messages[i] + + 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._url, headers=self._heads, data=data) + if response.status_code == 200: + break + time.slee(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)["message"]["content"] + response.close() + return output + + try: + while True: + line = response.raw.readline() + if line: + line = line.decode('utf-8').strip() + data = json.loads(line) + if data["done"]: + break + content = data["message"]["content"] + content_callback(content) + output += content + else: + break + finally: + response.close() + return output + + def chat(self, message, content_callback=None): + self.add_history("user", message) + self._data["stream"] = bool(content_callback) + self._heads["Accept"] = "text/event-stream" if content_callback else "application/json" + content = self._post(content_callback) + if self._max_history_num: + self.add_history("assistant", content) + messages_len = len(self._messages) + history_num = 2 * self._max_history_num + while history_num < len(self._messages): + del self._messages[0] + else: + self.clear_user_history() + + return content diff --git a/boards/default/micropython_esp32c2/build/Mixgo_Mini_lib-v1.23.0.bin b/boards/default/micropython_esp32c2/build/Mixgo_Mini_lib-v1.23.0.bin index 27ce2e44..a1a7fc15 100644 Binary files a/boards/default/micropython_esp32c2/build/Mixgo_Mini_lib-v1.23.0.bin and b/boards/default/micropython_esp32c2/build/Mixgo_Mini_lib-v1.23.0.bin differ diff --git a/boards/default/micropython_esp32c2/build/Mixgo_Mini_lib_ble-v1.23.0.bin b/boards/default/micropython_esp32c2/build/Mixgo_Mini_lib_ble-v1.23.0.bin index 6af728fb..6e091e81 100644 Binary files a/boards/default/micropython_esp32c2/build/Mixgo_Mini_lib_ble-v1.23.0.bin and b/boards/default/micropython_esp32c2/build/Mixgo_Mini_lib_ble-v1.23.0.bin differ diff --git a/boards/default/micropython_esp32c2/build/lib/map.json b/boards/default/micropython_esp32c2/build/lib/map.json index 04fe9961..eaa22e56 100644 --- a/boards/default/micropython_esp32c2/build/lib/map.json +++ b/boards/default/micropython_esp32c2/build/lib/map.json @@ -21,7 +21,7 @@ "framebuf" ], "__file__": true, - "__size__": 12696, + "__size__": 13061, "__name__": "mini_bot.py" }, "mini_g2": { diff --git a/boards/default/micropython_esp32c2/build/lib/mini_bot.py b/boards/default/micropython_esp32c2/build/lib/mini_bot.py index 1eceecd5..ad8a4b6a 100644 --- a/boards/default/micropython_esp32c2/build/lib/mini_bot.py +++ b/boards/default/micropython_esp32c2/build/lib/mini_bot.py @@ -35,7 +35,7 @@ class BOT035(FrameBuffer): self._buffer = bytearray(12) self._brightness = brightness self._touchs = [self.touch(0), self.touch(1)] - self._version = True if self._rreg(0x00) == 0x27 else False + self._version = True if self._rreg(0x00) >= 0x27 else False super().__init__(self._buffer, _LEDS_W, _LEDS_H, MONO_VLSB) self.reset() self.show() @@ -231,6 +231,17 @@ class BOT035(FrameBuffer): self._i2c.writeto(_BOT035_ADDRESS, reg.to_bytes(1, 'little')) return self._i2c.readfrom(_BOT035_ADDRESS, nbytes)[0] + def version(self): + _ver = self._rreg(0x00) + if _ver == 0x26: + return "v1.7", "Only supports CDC serial port" + elif _ver == 0x27: + return "v2.5", "Composite devices (CDC, keyboard and mouse)" + elif _ver == 0x28: + return "v2.9", "Composite devices (CDC, HID, WEBUSB, Keyboard and mouse)" + else: + return "vx.x", "Unknown, awaiting update" + def reset(self): """Reset SPK, PWM, HID registers to default state""" self._i2c.writeto_mem(_BOT035_ADDRESS, _BOT035_SPK, b'\x0A\x00\x00\x00\x20\x4E\x64\x64')