From 08714af5ed8438c27c121526e58764dd1b1d4537 Mon Sep 17 00:00:00 2001 From: zhaokexin Date: Mon, 22 Apr 2024 09:39:17 +0800 Subject: [PATCH] =?UTF-8?q?nlp=E6=94=AF=E6=8C=81=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E9=80=9A=E4=BF=A1=E8=BF=9E=E6=8E=A5,?= =?UTF-8?q?=E6=AF=8F=E6=AC=A1=E9=9A=8F=E6=9C=BA=E4=BA=A7=E7=94=9F=E4=B8=80?= =?UTF-8?q?=E4=B8=AAsessionId=E7=94=A8=E4=BA=8E=E5=8C=BA=E5=88=86=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E7=9A=84=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/textprocessor.cpp | 123 +++++++++++----- src/services/textprocessor.h | 38 +++-- src/services/textprocessor.xml | 13 +- src/services/textprocessorglue.c | 218 +++++++++++++++++++++++----- src/services/textprocessorglue.h | 49 ++++--- src/services/textprocessorproxy.cpp | 106 ++++++++++---- src/services/textprocessorproxy.h | 26 +++- 7 files changed, 426 insertions(+), 147 deletions(-) diff --git a/src/services/textprocessor.cpp b/src/services/textprocessor.cpp index 5abf13e..3f43f49 100644 --- a/src/services/textprocessor.cpp +++ b/src/services/textprocessor.cpp @@ -24,6 +24,8 @@ const std::string TextProcessor::objectPath_ = "/org/openkylin/aisdk/textprocessor"; +std::set TextProcessor::sessionIdSet; + TextProcessor::TextProcessor(GDBusConnection &connection, ai_engine::AiEnginePluginManager &aiEnginePluginManager) : connection_(connection) @@ -37,16 +39,20 @@ TextProcessor::~TextProcessor() { void TextProcessor::stopProcess() { - if (nlpEngine_) { - nlpEngine_->stopChat(); + gintAndNlpEngineMap::iterator iter = nlpEngineMap_.begin(); + while (iter != nlpEngineMap_.end()) { + iter->second->stopChat(); + ++iter; } std::lock_guard locker(mutex_); stopped = true; } -void TextProcessor::setChatCallback() { - nlpEngine_->setChatResultCallback([this](const std::string &result) { +void TextProcessor::setSessionChatCallback(gint sessionId) { + nlpEngineMap_.at(sessionId)->setChatResultCallback( + [this, sessionId](const std::string &result) { + std::cout << "setChatCallback sessionId " << sessionId << std::endl; std::cout << "aisdk_text_processor_complete_chat: \n " << result << std::endl; { std::lock_guard locker(mutex_); @@ -55,12 +61,15 @@ void TextProcessor::setChatCallback() { } } + std::string interfaceName = "org.openkylin.aisdk.textprocessor" + + std::to_string((int)sessionId); + std::shared_ptr textProcessor = static_cast(this)->getSharedObject(); g_dbus_connection_emit_signal(&(textProcessor->connection_), "org.openkylin.aisdk.textprocessor", "/org/openkylin/aisdk/textprocessor", - "org.openkylin.aisdk.textprocessor", + interfaceName.c_str(), "ChatResult", g_variant_new("(s)", result.c_str()), nullptr); }); @@ -72,6 +81,7 @@ void TextProcessor::onHandleInitEngine(AisdkTextProcessor *delegate, { std::cout << "On init engine..." << engineName << appId << apiKey << secretKey << invocation << std::endl; + TextProcessor *textProcessor = static_cast(userData); if (!textProcessor) { return; @@ -80,22 +90,33 @@ void TextProcessor::onHandleInitEngine(AisdkTextProcessor *delegate, new config::PromptTemplate(config::promptTemplateConfigFile(engineName), config::promptFilePath(engineName)) ); - textProcessor->initEngine(engineName, appId, apiKey, secretKey); - textProcessor->setChatCallback(); + + int sessionId = textProcessor->generateRandomSessionId(); + bool initResult = textProcessor->initEngine( + engineName, appId, apiKey, secretKey, sessionId); + + if (initResult) { + textProcessor->setSessionChatCallback(sessionId); + } + + aisdk_text_processor_complete_init(delegate, invocation, sessionId, initResult); + std::cout << "On init engine..." << sessionId << std::endl; } -bool TextProcessor::onHandleChat(AisdkTextProcessor *delegate, - GDBusMethodInvocation *invocation, - gchar *question, gpointer userData) { +bool TextProcessor::onHandleChat( + AisdkTextProcessor *delegate, GDBusMethodInvocation *invocation, + gchar *question, gint sessionId, gpointer userData) { std::cout << "On handle chat..." << invocation << std::endl; + std::shared_ptr textProcessor = static_cast(userData)->getSharedObject(); - if (!textProcessor) { + if (!textProcessor || + textProcessor->nlpEngineMap_.find(sessionId) == textProcessor->nlpEngineMap_.end()) { return false; } std::string userQuestion = question; - cpr::async([delegate, userQuestion, textProcessor]() { + cpr::async([delegate, userQuestion, textProcessor, sessionId]() { std::string message; { std::lock_guard locker(textProcessor->mutex_); @@ -104,15 +125,15 @@ bool TextProcessor::onHandleChat(AisdkTextProcessor *delegate, textProcessor->actorChanged = false; } - textProcessor->nlpEngine_->chat(message); + textProcessor->nlpEngineMap_.at(sessionId)->chat(message); }); return true; } -void TextProcessor::onSetActor(AisdkTextProcessor *delegate, - GDBusMethodInvocation *invocation, gint actor, - gpointer userData) { +void TextProcessor::onSetActor( + AisdkTextProcessor *delegate, GDBusMethodInvocation *invocation, + gint actor, gint sessionId, gpointer userData) { std::cout << "On Set Actor..." << std::endl; TextProcessor *textProcessor = static_cast(userData); if (!textProcessor) { @@ -132,41 +153,44 @@ void TextProcessor::onSetActor(AisdkTextProcessor *delegate, } } -void TextProcessor::onStopChat(AisdkTextProcessor *delegate, - GDBusMethodInvocation *invocation, - gpointer userData) { +void TextProcessor::onStopChat( + AisdkTextProcessor *delegate, GDBusMethodInvocation *invocation, + gint sessionId, gpointer userData) { std::cout << "On Stop Chat..." << std::endl; TextProcessor *textProcessor = static_cast(userData); - if (!textProcessor) { + if (!textProcessor || + textProcessor->nlpEngineMap_.find(sessionId) == textProcessor->nlpEngineMap_.end()) { return; } - textProcessor->nlpEngine_->stopChat(); + textProcessor->nlpEngineMap_.at(sessionId)->stopChat(); } -void TextProcessor::onSetContextSize(AisdkTextProcessor *delegate, - GDBusMethodInvocation *invocation, gint size, - gpointer userData) { - std::cout << "On Set Context Size..." << std::endl; +void TextProcessor::onSetContextSize( + AisdkTextProcessor *delegate, GDBusMethodInvocation *invocation, + gint size, gint sessionId, gpointer userData) { + std::cout << "On Set Context Size...sessionId " << sessionId << std::endl; TextProcessor *textProcessor = static_cast(userData); - if (!textProcessor) { + if (!textProcessor || + textProcessor->nlpEngineMap_.find(sessionId) == textProcessor->nlpEngineMap_.end()) { return; } - textProcessor->nlpEngine_->setContextSize(size); + textProcessor->nlpEngineMap_.at(sessionId)->setContextSize(size); } -void TextProcessor::onClearContext(AisdkTextProcessor *delegate, - GDBusMethodInvocation *invocation, - gpointer userData) { +void TextProcessor::onClearContext( + AisdkTextProcessor *delegate, GDBusMethodInvocation *invocation, + gint sessionId, gpointer userData) { std::cout << "On Clear Context..." << std::endl; TextProcessor *textProcessor = static_cast(userData); - if (!textProcessor) { + if (!textProcessor || + textProcessor->nlpEngineMap_.find(sessionId) == textProcessor->nlpEngineMap_.end()) { return; } - textProcessor->nlpEngine_->clearContext(); + textProcessor->nlpEngineMap_.at(sessionId)->clearContext(); } void TextProcessor::exportSkeleton() { @@ -211,17 +235,36 @@ void TextProcessor::unexportSkeleton() { g_dbus_interface_skeleton_unexport_from_connection( G_DBUS_INTERFACE_SKELETON(delegate_), &connection_); } - + sessionIdSet.clear(); g_object_unref(delegate_); } -void TextProcessor::initEngine(const std::string &engineName, - const std::string &appId, const std::string &apiKey, const std::string &secretKey) +bool TextProcessor::initEngine(const std::string &engineName, + const std::string &appId, const std::string &apiKey, const std::string &secretKey, gint sessionId) { - nlpEngine_ = aiEnginePluginManager_.createNlpEngine(engineName); - if (nlpEngine_->isCloud()) { - static_cast(nlpEngine_.get())->setAppId(appId); - static_cast(nlpEngine_.get())->setApiKey(apiKey); - static_cast(nlpEngine_.get())->setSecretKey(secretKey); + auto nlpEngine = aiEnginePluginManager_.createNlpEngine(engineName); + if (!nlpEngine) { + std::cout << "initEngine nlpEngine is nullptr !" << std::endl; + return false; + } + nlpEngineMap_[sessionId] = std::move(nlpEngine); + + if (nlpEngineMap_.at(sessionId)->isCloud()) { + static_cast( + nlpEngineMap_.at(sessionId).get())->setAppId(appId); + static_cast( + nlpEngineMap_.at(sessionId).get())->setApiKey(apiKey); + static_cast( + nlpEngineMap_.at(sessionId).get())->setSecretKey(secretKey); + } + return true; +} + +int TextProcessor::generateRandomSessionId() { + int sessionId = std::rand(); + while (sessionIdSet.find(sessionId) != sessionIdSet.end()) { + sessionId = std::rand(); } + sessionIdSet.insert(sessionId); + return sessionId; } diff --git a/src/services/textprocessor.h b/src/services/textprocessor.h index 5fc6738..031a586 100644 --- a/src/services/textprocessor.h +++ b/src/services/textprocessor.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include "engine/aienginepluginmanager.h" #include "config/prompttemplate.h" @@ -39,29 +40,35 @@ public: private: void exportSkeleton(); void unexportSkeleton(); + int generateRandomSessionId(); - void initEngine(const std::string &engineName, const std::string &appId, - const std::string &apiKey, const std::string &secretKey); - void setChatCallback(); + bool initEngine(const std::string &engineName, const std::string &appId, + const std::string &apiKey, const std::string &secretKey, gint sessionId); + void setSessionChatCallback(gint sessionId); static void onHandleInitEngine(AisdkTextProcessor *delegate, GDBusMethodInvocation *invocation, gchar *engineName, gchar *appId, gchar *apiKey, gchar *secretKey, gpointer userData); - static bool onHandleChat(AisdkTextProcessor *delegate, - GDBusMethodInvocation *invocation, gchar *question, gpointer userData); + static bool onHandleChat( + AisdkTextProcessor *delegate, GDBusMethodInvocation *invocation, + gchar *question, gint sessionId, gpointer userData); - static void onSetActor(AisdkTextProcessor *delegate, - GDBusMethodInvocation *invocation, gint actor, gpointer userData); + static void onSetActor( + AisdkTextProcessor *delegate, GDBusMethodInvocation *invocation, + gint actor, gint sessionId, gpointer userData); - static void onStopChat(AisdkTextProcessor *delegate, - GDBusMethodInvocation *invocation, gpointer userData); + static void onStopChat( + AisdkTextProcessor *delegate, GDBusMethodInvocation *invocation, + gint sessionId, gpointer userData); - static void onSetContextSize(AisdkTextProcessor *delegate, - GDBusMethodInvocation *invocation, gint size, gpointer userData); + static void onSetContextSize( + AisdkTextProcessor *delegate, GDBusMethodInvocation *invocation, + gint size, gint sessionId, gpointer userData); - static void onClearContext(AisdkTextProcessor *delegate, - GDBusMethodInvocation *invocation, gpointer userData); + static void onClearContext( + AisdkTextProcessor *delegate, GDBusMethodInvocation *invocation, + gint sessionId, gpointer userData); private: std::shared_ptr getSharedObject() { @@ -73,7 +80,6 @@ private: AisdkTextProcessor *delegate_ = nullptr; GDBusConnection &connection_; ai_engine::AiEnginePluginManager &aiEnginePluginManager_; - std::unique_ptr nlpEngine_ { nullptr }; std::unique_ptr promptTemplate_; std::string currentPrompt_; int actorId_ { -1 }; @@ -82,6 +88,10 @@ private: std::mutex mutex_; static const std::string objectPath_; + + static std::set sessionIdSet; + typedef std::map> gintAndNlpEngineMap; + gintAndNlpEngineMap nlpEngineMap_; }; #endif diff --git a/src/services/textprocessor.xml b/src/services/textprocessor.xml index ce3f1d3..6f80d58 100644 --- a/src/services/textprocessor.xml +++ b/src/services/textprocessor.xml @@ -2,12 +2,12 @@