Skip to content

Commit

Permalink
fix: [codegeex] optimized code completion logic
Browse files Browse the repository at this point in the history
Log: as title
  • Loading branch information
LiHua000 authored and deepin-mozart committed Aug 2, 2024
1 parent 17bfbc5 commit 4d2d6ba
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
10 changes: 9 additions & 1 deletion src/plugins/codegeex/codegeex/copilotapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ CopilotApi::CopilotApi(QObject *parent)

void CopilotApi::postGenerate(const QString &url, const QString &code, const QString &suffix)
{
if (completionReply)
completionReply->close();

QByteArray body = assembleGenerateBody(code, suffix);
QNetworkReply *reply = postMessage(url, CodeGeeXManager::instance()->getSessionId(), body);
completionReply = reply;
reply->setProperty("responseType", CopilotApi::inline_completions);
processResponse(reply);
}
Expand Down Expand Up @@ -116,7 +120,7 @@ QByteArray CopilotApi::assembleGenerateBody(const QString &prefix, const QString
json.insert("context", context);
json.insert("model", completionModel);
json.insert("lang", file.second);
json.insert("max_new_tokens", 64);
json.insert("max_new_tokens", 128);

QJsonDocument doc(json);
return doc.toJson();
Expand Down Expand Up @@ -185,6 +189,10 @@ void CopilotApi::slotReadReply(QNetworkReply *reply)
if (type == CopilotApi::inline_completions) {
auto content = jsonObject.value("inline_completions").toArray().at(0).toObject();
code = content.value("text").toString();
// Cut the first code segment
auto codeLines = code.split('\n');
code = codeLines.mid(0, codeLines.indexOf("", 1)).join('\n') + '\n';
completionReply = nullptr;
emit response(CopilotApi::inline_completions, code, "");
} else if (type == CopilotApi::multilingual_code_translate) {
auto codeLines = jsonObject.value("text").toString().split('\n');
Expand Down
19 changes: 10 additions & 9 deletions src/plugins/codegeex/codegeex/copilotapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ class CopilotApi : public QObject
const QString &locale,
const QString &command);

enum ResponseType
{
enum ResponseType {
inline_completions,
multilingual_code_comment,
multilingual_code_translate,
Expand All @@ -158,25 +157,27 @@ public slots:
QNetworkReply *postMessage(const QString &url, const QString &token, const QByteArray &body);

QByteArray assembleGenerateBody(const QString &prefix,
const QString &suffix);
const QString &suffix);

QByteArray assembleTranslateBody(const QString &code,
const QString &dst_lang,
const QString &locale);
const QString &dst_lang,
const QString &locale);

QByteArray assembleCommandBody(const QString &code,
const QString &locale,
const QString &command);
const QString &locale,
const QString &command);

void processResponse(QNetworkReply *reply);
QPair<QString, QString> getCurrentFileInfo();

QNetworkAccessManager *manager = nullptr;
QString chatModel = chatModelLite;
QString completionModel = completionModelLite;

QNetworkReply *completionReply = nullptr;
};

}// end namespace
} // end namespace
Q_DECLARE_METATYPE(CodeGeeX::CopilotApi::ResponseType)

#endif // COPILOT_H
#endif // COPILOT_H
14 changes: 4 additions & 10 deletions src/plugins/codegeex/copilot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ Copilot::Copilot(QObject *parent)

connect(&copilotApi, &CopilotApi::responseByStream, this, &Copilot::response);
connect(&copilotApi, &CopilotApi::messageSended, this, &Copilot::messageSended);

timer.setSingleShot(true);

connect(&timer, &QTimer::timeout, [this]() {
generateCode();
});
}

QString Copilot::selectedText() const
Expand Down Expand Up @@ -119,7 +113,7 @@ QMenu *Copilot::getMenu()

void Copilot::translateCode(const QString &code, const QString &dstLanguage)
{
QString url = QString(kUrlSSEChat) + "?stream=false"; //receive all msg at once
QString url = QString(kUrlSSEChat) + "?stream=false"; //receive all msg at once
copilotApi.postTranslate(url, code, dstLanguage, locale);
}

Expand Down Expand Up @@ -159,13 +153,13 @@ void Copilot::handleTextChanged()
{
// start generate code.
QMetaObject::invokeMethod(this, [this]() {
timer.start(200);
generateCode();
});
}

void Copilot::addComment()
{
QString url = QString(kUrlSSEChat) + "?stream=false"; //receive all msg at once
QString url = QString(kUrlSSEChat) + "?stream=false"; //receive all msg at once
copilotApi.postComment(url,
selectedText(),
locale);
Expand Down Expand Up @@ -237,7 +231,7 @@ void Copilot::commits()
auto workingDirectory = prjInfo.workspaceFolder();
process.setWorkingDirectory(workingDirectory);

connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [this, &process](int exitCode, QProcess::ExitStatus exitStatus){
connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [this, &process](int exitCode, QProcess::ExitStatus exitStatus) {
Q_UNUSED(exitStatus)

if (exitCode != 0)
Expand Down
1 change: 0 additions & 1 deletion src/plugins/codegeex/copilot.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public slots:
CodeGeeX::CopilotApi copilotApi;
dpfservice::EditorService *editorService = nullptr;
QString generateResponse;
QTimer timer;
QMutex mutexResponse;
bool generateCodeEnabled = true;
};
Expand Down

0 comments on commit 4d2d6ba

Please sign in to comment.