Skip to content

Commit

Permalink
feat: [codegeex] add new features
Browse files Browse the repository at this point in the history
1.reference files
2.connect to net work

Log: as title
  • Loading branch information
LiHua000 authored and deepin-mozart committed Jul 26, 2024
1 parent 5cd2a09 commit 1115c32
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 37 deletions.
7 changes: 7 additions & 0 deletions src/plugins/codegeex/builtin/texts/codegeex_files_16px.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions src/plugins/codegeex/builtin/texts/codegeex_internet_16px.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/plugins/codegeex/codegeex.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
<file>builtin/texts/codegeex_function_16px.svg</file>
<file>builtin/texts/codegeex_send_16px.svg</file>
<file>builtin/texts/codegeex_edit_32px.svg</file>
<file>builtin/texts/codegeex_internet_16px.svg</file>
<file>builtin/texts/codegeex_files_16px.svg</file>
</qresource>
</RCC>
90 changes: 85 additions & 5 deletions src/plugins/codegeex/codegeex/askapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "askapi.h"
#include "codegeexmanager.h"
#include "src/common/supportfile/language.h"

#include <QNetworkAccessManager>
#include <QNetworkReply>
Expand All @@ -10,6 +12,7 @@
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QFileInfo>

namespace CodeGeeX {

Expand Down Expand Up @@ -231,7 +234,6 @@ void AskApi::processResponse(QNetworkReply *reply)
} else {
QString replyMsg = QString::fromUtf8(reply->readAll());
QStringList lines = replyMsg.split('\n');
QString data;
QString event;
QString id;
for (const auto &line : lines) {
Expand All @@ -253,28 +255,87 @@ void AskApi::processResponse(QNetworkReply *reply)
}

QJsonObject jsonObject = jsonDocument.object();
data = jsonObject.value("text").toString();

emit response(id, data, event);
auto entry = processJsonObject(event, &jsonObject);
if (entry.type == "crawl")
emit crawledWebsite(id, entry.websites);
else
emit response(id, entry.text, event);
}
}
}
});
}

Entry AskApi::processJsonObject(const QString &event, QJsonObject *obj)
{
Entry entry;
if (!obj || obj->isEmpty())
return entry;

if (event == "add") {
entry.type = "text";
entry.text = obj->value("text").toString();
return entry;
}

if (event == "processing") {
auto type = obj->value("type").toString();
entry.type = type;
if (type == "keyword") {
auto keyWords = obj->value("data").toArray();
QString keys;
for (auto key : keyWords)
keys = keys + key.toString() + " ";
entry.text = keys.trimmed();
} else if (type == "crawl") {
auto crawlObj = obj->value("data").toObject();
for (auto it = crawlObj.begin(); it != crawlObj.end(); ++it) {
websiteReference website;
QString citationKey = it.key();
QJsonObject citationObj = it.value().toObject();

website.citation = citationKey;
website.status = citationObj["status"].toString();
website.url = citationObj["url"].toString();
website.title = citationObj["title"].toString();

entry.websites.append(website);
}
}
return entry;
}

if (event == "finish")
entry.type = event;

return entry;
}

QByteArray AskApi::assembleSSEChatBody(const QString &prompt,
const QString &machineId,
const QJsonArray &history,
const QString &talkId)
{
QJsonObject jsonObject;

jsonObject.insert("prompt", prompt);
jsonObject.insert("machineId", machineId);
jsonObject.insert("client", "deepin-unioncode");
jsonObject.insert("history", history);
//temp support choose to use later
jsonObject.insert("locale", locale);
jsonObject.insert("model", model);

if (!CodeGeeXManager::instance()->getReferenceFiles().isEmpty()) {
auto fileDatas = parseFile(CodeGeeXManager::instance()->getReferenceFiles());
jsonObject.insert("command", "file_augment");
QJsonObject files;
files["files"] = fileDatas;
jsonObject.insert("files", files);
} else if (CodeGeeXManager::instance()->isConnectToNetWork())
jsonObject.insert("command", "online_search");
else
jsonObject.insert("model", model);

if(!talkId.isEmpty())
jsonObject.insert("talkId", talkId);

Expand Down Expand Up @@ -316,4 +377,23 @@ QJsonObject AskApi::toJsonOBject(QNetworkReply *reply)
QJsonDocument document = QJsonDocument::fromJson(response.toUtf8());
return document.object();
}

QJsonArray AskApi::parseFile(QStringList files)
{
QJsonArray result;

for (auto file : files) {
QJsonObject obj;
obj["name"] = QFileInfo(file).fileName();
obj["language"] = support_file::Language::id(file);
QFile content(file);
if (content.open(QIODevice::ReadOnly)) {
obj["content"] = QString(content.readAll());
}
result.append(obj);
}

return result;
}

} // end namespace
16 changes: 16 additions & 0 deletions src/plugins/codegeex/codegeex/askapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ class QNetworkAccessManager;
class QNetworkReply;

namespace CodeGeeX {
struct websiteReference {
QString citation;
QString url;
QString title;
QString status;
};

struct Entry {
QString type;
QString text;
QList<websiteReference> websites;
};

class AskApi : public QObject
{
Q_OBJECT
Expand Down Expand Up @@ -75,6 +88,7 @@ class AskApi : public QObject
signals:
void loginState(LoginState loginState);
void response(const QString &msgID, const QString &response, const QString &event);
void crawledWebsite(const QString &msgID, const QList<websiteReference> &websites);
void getSessionListResult(const QVector<SessionRecord> &records);
void getMessageListResult(const QVector<MessageRecord> &records);
void sessionDeleted(const QStringList &talkId, bool isSuccessful);
Expand All @@ -87,6 +101,7 @@ public slots:
QNetworkReply *postMessage(const QString &url, const QString &token, const QByteArray &body);
QNetworkReply *getMessage(const QString &url, const QString &token);
void processResponse(QNetworkReply *reply);
Entry processJsonObject(const QString &event, QJsonObject *obj);

QByteArray assembleSSEChatBody(const QString &prompt,
const QString &machineId,
Expand All @@ -100,6 +115,7 @@ public slots:

QByteArray jsonToByteArray(const QJsonObject &jsonObject);
QJsonObject toJsonOBject(QNetworkReply *reply);
QJsonArray parseFile(QStringList files);

QNetworkAccessManager *manager = nullptr;
QString model = "codegeex-chat-lite";
Expand Down
12 changes: 10 additions & 2 deletions src/plugins/codegeex/codegeexmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ void CodeGeeXManager::setLocale(CodeGeeX::locale locale)
askApi.setLocale("zh");
Copilot::instance()->setLocale("zh");
} else if (locale == CodeGeeX::En) {
askApi.setLocale("cn");
Copilot::instance()->setLocale("cn");
askApi.setLocale("en");
Copilot::instance()->setLocale("en");
}

}
Expand All @@ -102,6 +102,11 @@ void CodeGeeXManager::setCurrentModel(languageModel model)
askApi.setModel(chatModelPro);
}

void CodeGeeXManager::connectToNetWork(bool connecting)
{
isConnecting = connecting;
}

void CodeGeeXManager::createNewSession()
{
QString currentMSecsStr = QString::number(QDateTime::currentMSecsSinceEpoch());
Expand Down Expand Up @@ -185,6 +190,8 @@ void CodeGeeXManager::onResponse(const QString &msgID, const QString &data, cons
curSessionMsg[msgID].updateData(responseData);
Q_EMIT requestMessageUpdate(curSessionMsg[msgID]);
}
} else if (event == "processing") {
emit searching(data);
}
}

Expand Down Expand Up @@ -279,6 +286,7 @@ CodeGeeXManager::CodeGeeXManager(QObject *parent)
void CodeGeeXManager::initConnections()
{
connect(&askApi, &AskApi::response, this, &CodeGeeXManager::onResponse);
connect(&askApi, &AskApi::crawledWebsite, this, &CodeGeeXManager::crawledWebsite);
connect(&askApi, &AskApi::loginState, this, &CodeGeeXManager::recevieLoginState);
connect(&askApi, &AskApi::sessionCreated, this, &CodeGeeXManager::onSessionCreated);
connect(&askApi, &AskApi::getSessionListResult, this, &CodeGeeXManager::recevieSessionRecords);
Expand Down
34 changes: 22 additions & 12 deletions src/plugins/codegeex/codegeexmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ struct RecordData
};

namespace CodeGeeX {
static const char* chatModelLite = "codegeex-chat-lite";
static const char* chatModelPro = "codegeex-chat-pro";
static const char *chatModelLite = "codegeex-chat-lite";
static const char *chatModelPro = "codegeex-chat-pro";

static const char* completionModelLite = "codegeex-lite";
static const char* completionModelPro = "codegeex-pro";
static const char *completionModelLite = "codegeex-lite";
static const char *completionModelPro = "codegeex-pro";

enum languageModel{
Lite,
Pro
};
enum languageModel {
Lite,
Pro
};

enum locale{
Zh,
En
};
enum locale {
Zh,
En
};
}
Q_DECLARE_METATYPE(CodeGeeX::languageModel)
Q_DECLARE_METATYPE(CodeGeeX::locale)
Expand Down Expand Up @@ -78,13 +78,21 @@ class CodeGeeXManager : public QObject
QString getTalkId() const;
QList<RecordData> sessionRecords() const;

void connectToNetWork(bool connecting);
bool isConnectToNetWork() { return isConnecting; };
QStringList getReferenceFiles() { return referenceFiles; };
void setRefereceFiles(QStringList files) { referenceFiles = files; };

Q_SIGNALS:
void loginSuccessed();
void logoutSuccessed();
void createdNewSession();
void requestMessageUpdate(const MessageData &msg);
void requestToTransCode(const QString &code);
void chatStarted();
void crawledWebsite(const QString &msgID, const QList<CodeGeeX::websiteReference> &websites);
void searching(const QString &searchText);
void terminated();
void chatFinished();
void sessionRecordsUpdated();
void setTextToSend(const QString &prompt);
Expand Down Expand Up @@ -123,6 +131,8 @@ public Q_SLOTS:
QTimer *queryTimer { nullptr };
bool isLogin { false };
bool isRunning { false };
bool isConnecting { false };
QStringList referenceFiles;
};

#endif // CODEGEEXMANAGER_H
2 changes: 1 addition & 1 deletion src/plugins/codegeex/option/detailwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ bool DetailWidget::getControlValue(QMap<QString, QVariant> &map)
dataToMap(config, map);

Copilot::instance()->setGenerateCodeEnabled(config.codeCompletionEnabled);
Copilot::instance()->setCommitsLocale(config.commitsLanguage == CodeGeeX::Zh ? "zh" : "cn");
Copilot::instance()->setCommitsLocale(config.commitsLanguage == CodeGeeX::Zh ? "zh" : "en");
CodeGeeXManager::instance()->setLocale(config.globalLanguage);
CodeGeeXManager::instance()->setCurrentModel(config.model);
return true;
Expand Down
Loading

0 comments on commit 1115c32

Please sign in to comment.