Skip to content

Commit

Permalink
Fix authorization to the API.
Browse files Browse the repository at this point in the history
  • Loading branch information
Palm1r committed Aug 30, 2024
1 parent e3495e1 commit 46258a1
Show file tree
Hide file tree
Showing 12 changed files with 21 additions and 44 deletions.
28 changes: 16 additions & 12 deletions LLMClientInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,7 @@ LLMClientInterface::ContextPair LLMClientInterface::prepareContext(

void LLMClientInterface::updateProvider()
{
m_serverUrl = QUrl(QString("%1:%2%3")
.arg(settings().url.value())
.arg(settings().port.value())
.arg(settings().endPoint.value()));
m_serverUrl = QUrl(QString("%1%2").arg(settings().url(), settings().endPoint()));
}

void LLMClientInterface::sendCompletionToClient(const QString &completion,
Expand Down Expand Up @@ -324,22 +321,29 @@ void LLMClientInterface::sendCompletionToClient(const QString &completion,

void LLMClientInterface::sendLLMRequest(const QJsonObject &request, const ContextPair &prompt)
{
QJsonObject qodeRequest = {{"model", settings().modelName.value()}, {"stream", true}};
QJsonObject providerRequest = {{"model", settings().modelName.value()}, {"stream", true}};

auto currentTemplate = PromptTemplateManager::instance().getCurrentTemplate();
currentTemplate->prepareRequest(qodeRequest, prompt.prefix, prompt.suffix);
currentTemplate->prepareRequest(providerRequest, prompt.prefix, prompt.suffix);

auto &providerManager = LLMProvidersManager::instance();
providerManager.getCurrentProvider()->prepareRequest(qodeRequest);
providerManager.getCurrentProvider()->prepareRequest(providerRequest);

logMessage(
QString("Sending request to llm: \nurl: %1\nRequest body:\n%2")
.arg(m_serverUrl.toString())
.arg(QString::fromUtf8(QJsonDocument(qodeRequest).toJson(QJsonDocument::Indented))));
logMessage(QString("Sending request to llm: \nurl: %1\nRequest body:\n%2")
.arg(m_serverUrl.toString(),
QString::fromUtf8(
QJsonDocument(providerRequest).toJson(QJsonDocument::Indented))));

QNetworkRequest networkRequest(m_serverUrl);
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QNetworkReply *reply = m_manager->post(networkRequest, QJsonDocument(qodeRequest).toJson());

if (providerRequest.contains("api_key")) {
QString apiKey = providerRequest["api_key"].toString();
networkRequest.setRawHeader("Authorization", QString("Bearer %1").arg(apiKey).toUtf8());
providerRequest.remove("api_key");
}

QNetworkReply *reply = m_manager->post(networkRequest, QJsonDocument(providerRequest).toJson());
if (!reply) {
logMessage("Error: Failed to create network reply");
return;
Expand Down
2 changes: 1 addition & 1 deletion QodeAssist.json.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Name" : "QodeAssist",
"Version" : "0.0.5",
"Version" : "0.0.6",
"CompatVersion" : "${IDE_VERSION_COMPAT}",
"Vendor" : "Petr Mironychev",
"Copyright" : "(C) ${IDE_COPYRIGHT_YEAR} Petr Mironychev, (C) ${IDE_COPYRIGHT_YEAR} The Qt Company Ltd",
Expand Down
1 change: 0 additions & 1 deletion QodeAssistConstants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const char ENABLE_AUTO_COMPLETE[] = "QodeAssist.enableAutoComplete";
const char ENABLE_LOGGING[] = "QodeAssist.enableLogging";
const char LLM_PROVIDERS[] = "QodeAssist.llmProviders";
const char URL[] = "QodeAssist.url";
const char PORT[] = "QodeAssist.port";
const char END_POINT[] = "QodeAssist.endPoint";
const char MODEL_NAME[] = "QodeAssist.modelName";
const char SELECT_MODELS[] = "QodeAssist.selectModels";
Expand Down
8 changes: 1 addition & 7 deletions QodeAssistSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ QodeAssistSettings::QodeAssistSettings()
endPoint.setLabelText(Tr::tr("Endpoint:"));
endPoint.setDisplayStyle(Utils::StringAspect::LineEditDisplay);

port.setSettingsKey(Constants::PORT);
port.setLabelText(Tr::tr("Port:"));
port.setRange(1, 65535);

modelName.setSettingsKey(Constants::MODEL_NAME);
modelName.setLabelText(Tr::tr("LLM Name:"));
modelName.setDisplayStyle(Utils::StringAspect::LineEditDisplay);
Expand Down Expand Up @@ -215,7 +211,7 @@ QodeAssistSettings::QodeAssistSettings()
enableLogging,
Row{Stretch{1}, resetToDefaults}}}},
Group{title(Tr::tr("LLM Providers")),
Form{Column{llmProviders, Row{url, port, endPoint}, providerPaths}}},
Form{Column{llmProviders, Row{url, endPoint}, providerPaths}}},
Group{title(Tr::tr("LLM Model Settings")),
Form{Column{Row{selectModels, modelName}}}},
Group{title(Tr::tr("FIM Prompt Settings")),
Expand Down Expand Up @@ -288,7 +284,6 @@ void QodeAssistSettings::updateProviderSettings()
if (provider) {
logMessage(QString("currentProvider %1").arg(provider->name()));
url.setValue(provider->url());
port.setValue(provider->defaultPort());
endPoint.setValue(provider->completionEndpoint());
ollamaLivetime.setEnabled(provider->name() == "Ollama");
}
Expand Down Expand Up @@ -347,7 +342,6 @@ void QodeAssistSettings::resetSettingsToDefaults()
resetAspect(enableAutoComplete);
resetAspect(llmProviders);
resetAspect(url);
resetAspect(port);
resetAspect(endPoint);
resetAspect(modelName);
resetAspect(fimPrompts);
Expand Down
1 change: 0 additions & 1 deletion QodeAssistSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class QodeAssistSettings : public Utils::AspectContainer

Utils::SelectionAspect llmProviders{this};
Utils::StringAspect url{this};
Utils::IntegerAspect port{this};
Utils::StringAspect endPoint{this};

Utils::StringAspect modelName{this};
Expand Down
1 change: 0 additions & 1 deletion providers/LLMProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class LLMProvider

virtual QString name() const = 0;
virtual QString url() const = 0;
virtual int defaultPort() const = 0;
virtual QString completionEndpoint() const = 0;

virtual void prepareRequest(QJsonObject &request) = 0;
Expand Down
7 changes: 1 addition & 6 deletions providers/LMStudioProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ QString LMStudioProvider::name() const

QString LMStudioProvider::url() const
{
return "http://localhost";
}

int LMStudioProvider::defaultPort() const
{
return 1234;
return "http://localhost:1234";
}

QString LMStudioProvider::completionEndpoint() const
Expand Down
1 change: 0 additions & 1 deletion providers/LMStudioProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class LMStudioProvider : public LLMProvider

QString name() const override;
QString url() const override;
int defaultPort() const override;
QString completionEndpoint() const override;
void prepareRequest(QJsonObject &request) override;
bool handleResponse(QNetworkReply *reply, QString &accumulatedResponse) override;
Expand Down
7 changes: 1 addition & 6 deletions providers/OllamaProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ QString OllamaProvider::name() const

QString OllamaProvider::url() const
{
return "http://localhost";
}

int OllamaProvider::defaultPort() const
{
return 11434;
return "http://localhost:11434";
}

QString OllamaProvider::completionEndpoint() const
Expand Down
1 change: 0 additions & 1 deletion providers/OllamaProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class OllamaProvider : public LLMProvider

QString name() const override;
QString url() const override;
int defaultPort() const override;
QString completionEndpoint() const override;
void prepareRequest(QJsonObject &request) override;
bool handleResponse(QNetworkReply *reply, QString &accumulatedResponse) override;
Expand Down
7 changes: 1 addition & 6 deletions providers/OpenAICompatProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ QString OpenAICompatProvider::name() const

QString OpenAICompatProvider::url() const
{
return "http://localhost";
}

int OpenAICompatProvider::defaultPort() const
{
return 1234;
return "http://localhost:1234";
}

QString OpenAICompatProvider::completionEndpoint() const
Expand Down
1 change: 0 additions & 1 deletion providers/OpenAICompatProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class OpenAICompatProvider : public LLMProvider

QString name() const override;
QString url() const override;
int defaultPort() const override;
QString completionEndpoint() const override;
void prepareRequest(QJsonObject &request) override;
bool handleResponse(QNetworkReply *reply, QString &accumulatedResponse) override;
Expand Down

0 comments on commit 46258a1

Please sign in to comment.