Skip to content

Commit

Permalink
Merge pull request #9 from Abandon-ht/dev
Browse files Browse the repository at this point in the history
update compatible with the old version of LLM firmware.
  • Loading branch information
Forairaaaaa authored Dec 9, 2024
2 parents 688df93 + 8a3601e commit 0cff2c1
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/M5ModuleLLM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ bool M5ModuleLLM::begin(Stream* serialPort)

bool M5ModuleLLM::checkConnection()
{
llm_version = (sys.version() == MODULE_LLM_OK);
return sys.ping() == MODULE_LLM_OK;
}

Expand Down
1 change: 1 addition & 0 deletions src/M5ModuleLLM.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "api/api_kws.h"
#include "api/api_asr.h"
#include "api/api_yolo.h"
#include "api/api_version.h"

class M5ModuleLLM {
public:
Expand Down
20 changes: 13 additions & 7 deletions src/api/api_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: MIT
*/
#include "api_asr.h"
#include "api_version.h"

using namespace m5_module_llm;

Expand All @@ -23,14 +24,19 @@ String ApiAsr::setup(ApiAsrSetupConfig_t config, String request_id)
doc["object"] = "asr.setup";
doc["data"]["model"] = config.model;
doc["data"]["response_format"] = config.response_format;
JsonArray inputArray = doc["data"]["input"].to<JsonArray>();
for (const String& str : config.input) {
inputArray.add(str);
doc["data"]["enoutput"] = config.enoutput;
doc["data"]["enkws"] = config.enkws;
doc["data"]["rule1"] = config.rule1;
doc["data"]["rule2"] = config.rule2;
doc["data"]["rule3"] = config.rule3;
if (!llm_version) {
doc["data"]["input"] = config.input[0];
} else {
JsonArray inputArray = doc["data"]["input"].to<JsonArray>();
for (const String& str : config.input) {
inputArray.add(str);
}
}
doc["data"]["enoutput"] = config.enoutput;
doc["data"]["rule1"] = config.rule1;
doc["data"]["rule2"] = config.rule2;
doc["data"]["rule3"] = config.rule3;
serializeJson(doc, cmd);
}

Expand Down
1 change: 1 addition & 0 deletions src/api/api_asr.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct ApiAsrSetupConfig_t {
String response_format = "asr.utf-8.stream";
std::vector<String> input = {"sys.pcm"};
bool enoutput = true;
bool enkws = true;
float rule1 = 2.4;
float rule2 = 1.2;
float rule3 = 30.0;
Expand Down
15 changes: 10 additions & 5 deletions src/api/api_kws.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: MIT
*/
#include "api_kws.h"
#include "api_version.h"

using namespace m5_module_llm;

Expand All @@ -23,12 +24,16 @@ String ApiKws::setup(ApiKwsSetupConfig_t config, String request_id)
doc["object"] = "kws.setup";
doc["data"]["model"] = config.model;
doc["data"]["response_format"] = config.response_format;
JsonArray inputArray = doc["data"]["input"].to<JsonArray>();
for (const String& str : config.input) {
inputArray.add(str);
doc["data"]["enoutput"] = config.enoutput;
doc["data"]["kws"] = config.kws;
if (!llm_version) {
doc["data"]["input"] = config.input[0];
} else {
JsonArray inputArray = doc["data"]["input"].to<JsonArray>();
for (const String& str : config.input) {
inputArray.add(str);
}
}
doc["data"]["enoutput"] = config.enoutput;
doc["data"]["kws"] = config.kws;
serializeJson(doc, cmd);
}

Expand Down
19 changes: 13 additions & 6 deletions src/api/api_llm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: MIT
*/
#include "api_llm.h"
#include "api_version.h"

using namespace m5_module_llm;

Expand All @@ -23,13 +24,19 @@ String ApiLlm::setup(ApiLlmSetupConfig_t config, String request_id)
doc["object"] = "llm.setup";
doc["data"]["model"] = config.model;
doc["data"]["response_format"] = config.response_format;
JsonArray inputArray = doc["data"]["input"].to<JsonArray>();
for (const String& str : config.input) {
inputArray.add(str);
doc["data"]["enoutput"] = config.enoutput;
doc["data"]["enkws"] = config.enkws;
doc["data"]["max_token_len"] = config.max_token_len;
doc["data"]["prompt"] = config.prompt;
if (!llm_version) {
doc["data"]["model"] = "qwen2.5-0.5b";
doc["data"]["input"] = config.input[0];
} else {
JsonArray inputArray = doc["data"]["input"].to<JsonArray>();
for (const String& str : config.input) {
inputArray.add(str);
}
}
doc["data"]["enoutput"] = config.enoutput;
doc["data"]["max_token_len"] = config.max_token_len;
doc["data"]["prompt"] = config.prompt;
serializeJson(doc, cmd);
}

Expand Down
1 change: 1 addition & 0 deletions src/api/api_llm.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct ApiLlmSetupConfig_t {
String response_format = "llm.utf-8.stream";
std::vector<String> input = {"llm.utf-8.stream"};
bool enoutput = true;
bool enkws = true;
int max_token_len = 127;
// int max_token_len = 512;
};
Expand Down
2 changes: 1 addition & 1 deletion src/api/api_melotts.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace m5_module_llm {
struct ApiMelottsSetupConfig_t {
String model = "melotts_zh-cn";
String response_format = "sys.pcm";
std::vector<String> input = {"tts.utf-8,stream"};
std::vector<String> input = {"tts.utf-8.stream"};
bool enoutput = false;
bool enaudio = true;
};
Expand Down
12 changes: 10 additions & 2 deletions src/api/api_sys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ static const char* _cmd_reset =
"{\"request_id\":\"sys_reset\",\"work_id\":\"sys\",\"action\":\"reset\",\"object\":\"None\",\"data\":\"None\"}";
static const char* _cmd_reboot =
"{\"request_id\":\"sys_reboot\",\"work_id\":\"sys\",\"action\":\"reboot\",\"object\":\"None\",\"data\":\"None\"}";
// static const char* _cmd_ls_mode =
// "{\"request_id\":\"sys_lsmode\",\"work_id\":\"sys\",\"action\":\"lsmode\",\"object\":\"None\",\"data\":\"None\"}";
static const char* _cmd_version =
"{\"request_id\":\"sys_version\",\"work_id\":\"sys\",\"action\":\"version\",\"object\":\"None\",\"data\":\"None\"}";

void ApiSys::init(ModuleMsg* moduleMsg)
{
Expand All @@ -29,6 +29,14 @@ int ApiSys::ping()
return ret;
}

int ApiSys::version()
{
int ret = MODULE_LLM_WAIT_RESPONSE_TIMEOUT;
_module_msg->sendCmdAndWaitToTakeMsg(
_cmd_version, "sys_version", [&ret](ResponseMsg_t& msg) { ret = msg.error.code; }, 2000);
return ret;
}

int ApiSys::reset(bool waitResetFinish)
{
int ret = MODULE_LLM_WAIT_RESPONSE_TIMEOUT;
Expand Down
10 changes: 10 additions & 0 deletions src/api/api_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class ApiSys {
* @param waitResetFinish
* @return int
*/

int version();

/**
* @brief Check version
*
* @param waitCheckFinish
* @return int
*/

int reset(bool waitResetFinish = true);

/**
Expand Down
18 changes: 13 additions & 5 deletions src/api/api_tts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: MIT
*/
#include "api_tts.h"
#include "api_version.h"

using namespace m5_module_llm;

Expand All @@ -23,12 +24,19 @@ String ApiTts::setup(ApiTtsSetupConfig_t config, String request_id)
doc["object"] = "tts.setup";
doc["data"]["model"] = config.model;
doc["data"]["response_format"] = config.response_format;
JsonArray inputArray = doc["data"]["input"].to<JsonArray>();
for (const String& str : config.input) {
inputArray.add(str);
doc["data"]["enoutput"] = config.enoutput;
doc["data"]["enkws"] = config.enkws;
doc["data"]["enaudio"] = config.enaudio;
if (!llm_version) {
doc["data"]["response_format"] = "tts.base64.wav";
doc["data"]["input"] = config.input[0];
doc["data"]["enoutput"] = true;
} else {
JsonArray inputArray = doc["data"]["input"].to<JsonArray>();
for (const String& str : config.input) {
inputArray.add(str);
}
}
doc["data"]["enoutput"] = config.enoutput;
doc["data"]["enaudio"] = config.enaudio;
serializeJson(doc, cmd);
}

Expand Down
3 changes: 2 additions & 1 deletion src/api/api_tts.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ namespace m5_module_llm {
struct ApiTtsSetupConfig_t {
String model = "single_speaker_english_fast";
String response_format = "sys.pcm";
std::vector<String> input = {"tts.utf-8,stream"};
std::vector<String> input = {"tts.utf-8.stream"};
bool enoutput = false;
bool enaudio = true;
bool enkws = true;
};

class ApiTts {
Expand Down
8 changes: 8 additions & 0 deletions src/api/api_version.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
#include "api_version.h"

int llm_version = 0;
8 changes: 8 additions & 0 deletions src/api/api_version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
#pragma once

extern int llm_version;
15 changes: 11 additions & 4 deletions src/presets/voice_assistant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: MIT
*/
#include "../M5ModuleLLM.h"
#include "../src/api/api_version.h"

using namespace m5_module_llm;

Expand Down Expand Up @@ -59,11 +60,17 @@ int M5ModuleLLM_VoiceAssistant::begin(String wakeUpKeyword, String prompt)

_debug("setup module tts..");
{
ApiTtsSetupConfig_t config;
config.input = {_work_id.llm, _work_id.kws};
_work_id.tts = _m5_module_llm->tts.setup(config);
if (!llm_version) {
ApiTtsSetupConfig_t config;
config.input = {_work_id.llm, _work_id.kws};
_work_id.tts = _m5_module_llm->tts.setup(config);
} else {
ApiMelottsSetupConfig_t config;
config.input = {_work_id.llm, _work_id.kws};
_work_id.melotts = _m5_module_llm->melotts.setup(config);
}
}
if (_work_id.tts.isEmpty()) {
if (_work_id.tts.isEmpty() && _work_id.melotts.isEmpty()) {
return MODULE_LLM_ERROR_NONE;
}

Expand Down

0 comments on commit 0cff2c1

Please sign in to comment.