| 222 | } |
| 223 | |
| 224 | void MnncliServer::Start(MNN::Transformer::Llm* llm, bool is_r1, const std::string& host, int port) { |
| 225 | this->is_r1_ = is_r1; |
| 226 | // Create a server instance |
| 227 | httplib::Server server; |
| 228 | |
| 229 | // Define a route for the GET request on "/" |
| 230 | server.Get("/", [this](const httplib::Request& req, httplib::Response& res) { |
| 231 | AllowCors(res); |
| 232 | res.set_content(html_content, "text/html"); |
| 233 | }); |
| 234 | server.Post("/reset", [&](const httplib::Request &req, httplib::Response &res) { |
| 235 | LOG_DEBUG("POST /reset"); |
| 236 | AllowCors(res); |
| 237 | llm->reset(); |
| 238 | res.set_content("{\"status\": \"ok\"}", "application/json"); |
| 239 | }); |
| 240 | |
| 241 | server.Get("/v1/models", [&](const httplib::Request &req, httplib::Response &res) { |
| 242 | LOG_DEBUG("GET /v1/models"); |
| 243 | AllowCors(res); |
| 244 | json models_response = { |
| 245 | {"object", "list"}, |
| 246 | {"data", json::array({ |
| 247 | { |
| 248 | {"id", "ModelScope/MNN/Qwen2.5-0.5B-Instruct"}, |
| 249 | {"object", "model"}, |
| 250 | {"created", static_cast<int>(time(nullptr))}, |
| 251 | {"owned_by", "mnn"} |
| 252 | } |
| 253 | })} |
| 254 | }; |
| 255 | res.set_content(models_response.dump(), "application/json"); |
| 256 | }); |
| 257 | server.Options("/v1/models", [](const httplib::Request& /*req*/, httplib::Response& res) { |
| 258 | AllowCors(res); |
| 259 | res.status = 200; |
| 260 | }); |
| 261 | |
| 262 | server.Options("/chat/completions", [](const httplib::Request& /*req*/, httplib::Response& res) { |
| 263 | AllowCors(res); |
| 264 | res.status = 200; |
| 265 | }); |
| 266 | |
| 267 | server.Options("/v1/chat/completions", [](const httplib::Request& /*req*/, httplib::Response& res) { |
| 268 | AllowCors(res); |
| 269 | res.status = 200; |
| 270 | }); |
| 271 | // Handler function for chat completions |
| 272 | auto chatCompletionsHandler = [&](const httplib::Request &req, httplib::Response &res) { |
| 273 | LOG_DEBUG("POST chat/completions, handled by thread: " + std::to_string(std::hash<std::thread::id>{}(std::this_thread::get_id()))); |
| 274 | AllowCors(res); |
| 275 | if (!json::accept(req.body)) { |
| 276 | json err; |
| 277 | err["error"] = "Invalid JSON in request body."; |
| 278 | res.status = 400; |
| 279 | res.set_content(err.dump(), "application/json"); |
| 280 | return; |
| 281 | } |
no test coverage detected