Skip to content

Commit 6be65f9

Browse files
Forairaaaaalbuque
authored andcommitted
libs/module: Add support for llm module.
Signed-off-by: Forairaaaaa <[email protected]>
1 parent e2219e5 commit 6be65f9

File tree

7 files changed

+1115
-0
lines changed

7 files changed

+1115
-0
lines changed

docs/en/module/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Module
1010
dualkmeter.rst
1111
grbl.rst
1212
hmi.rst
13+
llm.rst
1314
lora.rst
1415
lorawan868.rst
1516
nbiot.rst

docs/en/module/llm.rst

Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
1+
2+
LlmModule
3+
=========
4+
5+
.. include:: ../refs/module.llm.ref
6+
7+
Micropython Example::
8+
9+
from module import LlmModule
10+
11+
def on_keyword_detected():
12+
print("[Keyword] detected")
13+
14+
def on_asr_data_input(data: str, finish: bool, index: int):
15+
print(f"[ASR data] {data}")
16+
17+
def on_llm_data_input(data: str, finish: bool, index: int):
18+
print(f"[LLM data] {data}")
19+
20+
module_llm = LlmModule()
21+
module_llm.begin_voice_assistant()
22+
23+
module_llm.set_voice_assistant_on_keyword_detected_callback(on_keyword_detected)
24+
module_llm.set_voice_assistant_on_asr_data_input_callback(on_asr_data_input)
25+
module_llm.set_voice_assistant_on_llm_data_input_callback(on_llm_data_input)
26+
27+
while True:
28+
module_llm.update()
29+
30+
UIFLOW2 Example:
31+
32+
|example.png|
33+
34+
.. only:: builder_html
35+
36+
|llm_voice_assistant.m5f2|
37+
38+
39+
class LlmModule
40+
---------------
41+
42+
Constructors
43+
------------
44+
45+
.. class:: LlmModule()
46+
47+
Initialize LlmModule and set up UART communication based on board type.
48+
49+
UIFLOW2:
50+
51+
|init.png|
52+
53+
Methods
54+
-------
55+
56+
.. method:: LlmModule.update() -> None
57+
58+
Update ModuleLLM, receive response message.
59+
60+
UIFLOW2:
61+
62+
|update.png|
63+
64+
.. method:: LlmModule.check_connection() -> bool
65+
66+
Check if the module connection is working properly.
67+
68+
:return: True if module connection is OK, False otherwise.
69+
:rtype: bool
70+
71+
UIFLOW2:
72+
73+
|check_connection.png|
74+
75+
.. method:: LlmModule.get_response_msg_list() -> list
76+
77+
Get the list of module's response messages.
78+
79+
:return: List of response messages as dictionaries.
80+
:rtype: list
81+
82+
UIFLOW2:
83+
84+
|get_response_msg_list.png|
85+
86+
.. method:: LlmModule.clear_response_msg_list() -> None
87+
88+
Clear the module's response message list.
89+
90+
UIFLOW2:
91+
92+
|clear_response_msg_list.png|
93+
94+
.. method:: LlmModule.sys_ping() -> int
95+
96+
Send a ping to the system and get the response code.
97+
98+
UIFLOW2:
99+
100+
|sys_ping.png|
101+
102+
.. method:: LlmModule.sys_reset(wait_reset_finish=True) -> int
103+
104+
Reset the system.
105+
106+
:param bool wait_reset_finish: Whether to wait for reset completion.
107+
:return: Result of the reset command.
108+
:rtype: int
109+
110+
UIFLOW2:
111+
112+
|sys_reset.png|
113+
114+
.. method:: LlmModule.sys_reboot() -> int
115+
116+
Reboot the system.
117+
118+
:return: Result of the reboot command.
119+
:rtype: int
120+
121+
UIFLOW2:
122+
123+
|sys_reboot.png|
124+
125+
.. method:: LlmModule.llm_setup(prompt="", model="qwen2.5-0.5b", response_format="llm.utf-8.stream", input="llm.utf-8.stream", enoutput=True, enkws=True, max_token_len=127, request_id="llm_setup") -> str
126+
127+
Set up the LLM module.
128+
129+
:param str prompt: The prompt text.
130+
:param str model: The model name.
131+
:param str response_format: The response format.
132+
:param str input: The input format.
133+
:param bool enoutput: Enable output.
134+
:param bool enkws: Enable keyword spotting.
135+
:param int max_token_len: Maximum token length.
136+
:param str request_id: Request ID.
137+
:return: Result of the setup command.
138+
:rtype: str
139+
140+
UIFLOW2:
141+
142+
|llm_setup.png|
143+
144+
.. method:: LlmModule.llm_inference(work_id, input_data, request_id="llm_inference") -> str
145+
146+
Perform inference with the LLM module.
147+
148+
:param work_id: The work ID.
149+
:param input_data: The input data.
150+
:param str request_id: Request ID.
151+
:return: Result of the inference command.
152+
:rtype: str
153+
154+
UIFLOW2:
155+
156+
|llm_inference.png|
157+
158+
.. method:: LlmModule.audio_setup(capcard=0, capdevice=0, cap_volume=0.5, playcard=0, playdevice=1, play_volume=0.15, request_id="audio_setup") -> str
159+
160+
Set up the audio module.
161+
162+
:param int capcard: Capture card index.
163+
:param int capdevice: Capture device index.
164+
:param float cap_volume: Capture volume.
165+
:param int playcard: Playback card index.
166+
:param int playdevice: Playback device index.
167+
:param float play_volume: Playback volume.
168+
:param str request_id: Request ID.
169+
:return: Result of the setup command.
170+
:rtype: str
171+
172+
UIFLOW2:
173+
174+
|audio_setup.png|
175+
176+
.. method:: LlmModule.tts_setup(model="single_speaker_english_fast", response_format="tts.base64.wav", input="tts.utf-8.stream", enoutput=True, enkws=True, request_id="tts_setup") -> str
177+
178+
Set up the TTS module.
179+
180+
:param str model: TTS model name.
181+
:param str response_format: The response format.
182+
:param str input: The input format.
183+
:param bool enoutput: Enable output.
184+
:param bool enkws: Enable keyword spotting.
185+
:param str request_id: Request ID.
186+
:return: Result of the setup command.
187+
:rtype: str
188+
189+
UIFLOW2:
190+
191+
|tts_setup.png|
192+
193+
.. method:: LlmModule.kws_setup(kws="HELLO", model="sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01", response_format="kws.bool", input="sys.pcm", enoutput=True, request_id="kws_setup") -> str
194+
195+
Set up the KWS module.
196+
197+
:param str kws: Keyword to detect.
198+
:param str model: KWS model name.
199+
:param str response_format: The response format.
200+
:param str input: The input format.
201+
:param bool enoutput: Enable output.
202+
:param str request_id: Request ID.
203+
:return: Result of the setup command.
204+
:rtype: str
205+
206+
UIFLOW2:
207+
208+
|kws_setup.png|
209+
210+
.. method:: LlmModule.asr_setup(model="sherpa-ncnn-streaming-zipformer-20M-2023-02-17", response_format="asr.utf-8.stream", input="sys.pcm", enoutput=True, enkws=True, rule1=2.4, rule2=1.2, rule3=30.0, request_id="asr_setup") -> str
211+
212+
Set up the ASR module.
213+
214+
:param str model: ASR model name.
215+
:param str response_format: The response format.
216+
:param str input: The input format.
217+
:param bool enoutput: Enable output.
218+
:param bool enkws: Enable keyword spotting.
219+
:param float rule1: Rule 1 value.
220+
:param float rule2: Rule 2 value.
221+
:param float rule3: Rule 3 value.
222+
:param str request_id: Request ID.
223+
:return: Result of the setup command.
224+
:rtype: str
225+
226+
UIFLOW2:
227+
228+
|asr_setup.png|
229+
230+
.. method:: LlmModule.get_latest_llm_work_id() -> str
231+
232+
Get latest LLM module work id.
233+
234+
:return: Latest LLM module work id.
235+
:rtype: str
236+
237+
UIFLOW2:
238+
239+
|get_latest_llm_work_id.png|
240+
241+
.. method:: LlmModule.get_latest_audio_work_id() -> str
242+
243+
Get latest Audio module work id.
244+
245+
:return: Latest Audio module work id.
246+
:rtype: str
247+
248+
UIFLOW2:
249+
250+
|get_latest_audio_work_id.png|
251+
252+
.. method:: LlmModule.get_latest_tts_work_id() -> str
253+
254+
Get latest TTS module work id.
255+
256+
:return: Latest TTS module work id.
257+
:rtype: str
258+
259+
UIFLOW2:
260+
261+
|get_latest_tts_work_id.png|
262+
263+
.. method:: LlmModule.get_latest_kws_work_id() -> str
264+
265+
Get latest KWS module work id.
266+
267+
:return: Latest KWS module work id.
268+
:rtype: str
269+
270+
UIFLOW2:
271+
272+
|get_latest_kws_work_id.png|
273+
274+
.. method:: LlmModule.get_latest_asr_work_id() -> str
275+
276+
Get latest ASR module work id.
277+
278+
:return: Latest ASR module work id.
279+
:rtype: str
280+
281+
UIFLOW2:
282+
283+
|get_latest_asr_work_id.png|
284+
285+
.. method:: LlmModule.get_latest_error_code() -> int
286+
287+
Get latest ModuleLLM response error code.
288+
289+
:return: Latest ModuleLLM response error code.
290+
:rtype: int
291+
292+
UIFLOW2:
293+
294+
|get_latest_error_code.png|
295+
296+
.. method:: LlmModule.begin_voice_assistant(wake_up_keyword="HELLO", prompt="") -> bool
297+
298+
Begin the voice assistant.
299+
300+
:param str wake_up_keyword: The wake-up keyword.
301+
:param str prompt: The assistant prompt.
302+
:return: True if the voice assistant began successfully, False otherwise.
303+
:rtype: bool
304+
305+
UIFLOW2:
306+
307+
|begin_voice_assistant.png|
308+
309+
.. method:: LlmModule.set_voice_assistant_on_keyword_detected_callback(on_keyword_detected) -> None
310+
311+
Set the callback for when the wake-up keyword is detected.
312+
313+
:param on_keyword_detected: Callback function to be executed on keyword detection.
314+
315+
UIFLOW2:
316+
317+
|set_voice_assistant_on_keyword_detected_callback.png|
318+
319+
.. method:: LlmModule.set_voice_assistant_on_asr_data_input_callback(on_asr_data_input) -> None
320+
321+
Set the callback for when ASR data is input.
322+
323+
:param on_asr_data_input: Callback function to handle ASR data input.
324+
325+
UIFLOW2:
326+
327+
|set_voice_assistant_on_asr_data_input_callback.png|
328+
329+
.. method:: LlmModule.set_voice_assistant_on_llm_data_input_callback(on_llm_data_input) -> None
330+
331+
Set the callback for when LLM data is input.
332+
333+
:param on_llm_data_input: Callback function to handle LLM data input.
334+
335+
UIFLOW2:
336+
337+
|set_voice_assistant_on_llm_data_input_callback.png|

docs/en/refs/module.llm.ref

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/init.png
3+
.. |update.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/update.png
4+
.. |check_connection.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/check_connection.png
5+
.. |get_response_msg_list.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/get_response_msg_list.png
6+
.. |clear_response_msg_list.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/clear_response_msg_list.png
7+
.. |sys_ping.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/sys_ping.png
8+
.. |sys_reset.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/sys_reset.png
9+
.. |sys_reboot.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/sys_reboot.png
10+
.. |llm_setup.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/llm_setup.png
11+
.. |llm_inference.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/llm_inference.png
12+
.. |audio_setup.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/audio_setup.png
13+
.. |tts_setup.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/tts_setup.png
14+
.. |kws_setup.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/kws_setup.png
15+
.. |asr_setup.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/asr_setup.png
16+
.. |get_latest_llm_work_id.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/get_latest_llm_work_id.png
17+
.. |get_latest_audio_work_id.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/get_latest_audio_work_id.png
18+
.. |get_latest_tts_work_id.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/get_latest_tts_work_id.png
19+
.. |get_latest_kws_work_id.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/get_latest_kws_work_id.png
20+
.. |get_latest_asr_work_id.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/get_latest_asr_work_id.png
21+
.. |get_latest_error_code.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/get_latest_error_code.png
22+
.. |begin_voice_assistant.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/begin_voice_assistant.png
23+
.. |set_voice_assistant_on_keyword_detected_callback.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/set_voice_assistant_on_keyword_detected_callback.png
24+
.. |set_voice_assistant_on_asr_data_input_callback.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/set_voice_assistant_on_asr_data_input_callback.png
25+
.. |set_voice_assistant_on_llm_data_input_callback.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/set_voice_assistant_on_llm_data_input_callback.png
26+
27+
28+
.. |example.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/llm/example.png
29+
30+
.. |llm_voice_assistant.m5f2| raw:: html
31+
32+
<a
33+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/module/llm/llm_voice_assistant.m5f2"
34+
target="_blank"
35+
>
36+
llm_voice_assistant.m5f2
37+
</a>

0 commit comments

Comments
 (0)