From fd005be844d91f821eb4ecd69c40f08afba7ae28 Mon Sep 17 00:00:00 2001 From: cosmic-snow <134004613+cosmic-snow@users.noreply.github.com> Date: Tue, 9 Jul 2024 19:08:20 +0200 Subject: [PATCH] GPT4All Chat server API: add errors 405 Method Not Allowed (#2615) Add a HTTP status code 405 Method Not Allowed for each of the four handlers when the wrong method (GET/POST) is used. This mimics the OpenAI API response. Signed-off-by: cosmic-snow <134004613+cosmic-snow@users.noreply.github.com> --- gpt4all-chat/server.cpp | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/gpt4all-chat/server.cpp b/gpt4all-chat/server.cpp index 9b5819a4..af11663a 100644 --- a/gpt4all-chat/server.cpp +++ b/gpt4all-chat/server.cpp @@ -147,6 +147,53 @@ void Server::start() } ); + // Respond with code 405 to wrong HTTP methods: + m_server->route("/v1/models", QHttpServerRequest::Method::Post, + [](const QHttpServerRequest &request) { + if (!MySettings::globalInstance()->serverChat()) + return QHttpServerResponse(QHttpServerResponder::StatusCode::Unauthorized); + return QHttpServerResponse( + QJsonDocument::fromJson("{\"error\": {\"message\": \"Not allowed to POST on /v1/models." + " (HINT: Perhaps you meant to use a different HTTP method?)\"," + " \"type\": \"invalid_request_error\", \"param\": null, \"code\": null}}").object(), + QHttpServerResponder::StatusCode::MethodNotAllowed); + } + ); + + m_server->route("/v1/models/", QHttpServerRequest::Method::Post, + [](const QString &model, const QHttpServerRequest &request) { + if (!MySettings::globalInstance()->serverChat()) + return QHttpServerResponse(QHttpServerResponder::StatusCode::Unauthorized); + return QHttpServerResponse( + QJsonDocument::fromJson("{\"error\": {\"message\": \"Not allowed to POST on /v1/models/*." + " (HINT: Perhaps you meant to use a different HTTP method?)\"," + " \"type\": \"invalid_request_error\", \"param\": null, \"code\": null}}").object(), + QHttpServerResponder::StatusCode::MethodNotAllowed); + } + ); + + m_server->route("/v1/completions", QHttpServerRequest::Method::Get, + [](const QHttpServerRequest &request) { + if (!MySettings::globalInstance()->serverChat()) + return QHttpServerResponse(QHttpServerResponder::StatusCode::Unauthorized); + return QHttpServerResponse( + QJsonDocument::fromJson("{\"error\": {\"message\": \"Only POST requests are accepted.\"," + " \"type\": \"invalid_request_error\", \"param\": null, \"code\": \"method_not_supported\"}}").object(), + QHttpServerResponder::StatusCode::MethodNotAllowed); + } + ); + + m_server->route("/v1/chat/completions", QHttpServerRequest::Method::Get, + [](const QHttpServerRequest &request) { + if (!MySettings::globalInstance()->serverChat()) + return QHttpServerResponse(QHttpServerResponder::StatusCode::Unauthorized); + return QHttpServerResponse( + QJsonDocument::fromJson("{\"error\": {\"message\": \"Only POST requests are accepted.\"," + " \"type\": \"invalid_request_error\", \"param\": null, \"code\": \"method_not_supported\"}}").object(), + QHttpServerResponder::StatusCode::MethodNotAllowed); + } + ); + m_server->afterRequest([] (QHttpServerResponse &&resp) { resp.addHeader("Access-Control-Allow-Origin", "*"); return std::move(resp);