@brief Handle the openai completion request @param request the request @param send_response the send response @param send_streaming_response the send streaming response
| 1311 | ///@param send_response the send response |
| 1312 | ///@param send_streaming_response the send streaming response |
| 1313 | void RestHandler::handle_openai_completion(const json& request, |
| 1314 | std::function<void(const json&)> send_response, |
| 1315 | StreamResponseCallback send_streaming_response, |
| 1316 | std::shared_ptr<CancellationToken> cancellation_token) { |
| 1317 | try { |
| 1318 | // Extract OpenAI-style parameters |
| 1319 | std::string prompt = request["prompt"]; |
| 1320 | std::string model = request.value("model", current_model_tag); |
| 1321 | std::string reasoning_effort = request.value("reasoning_effort", "medium"); |
| 1322 | bool stream = request.value("stream", false); |
| 1323 | json options = request.value("options", json::object()); |
| 1324 | |
| 1325 | // direct return if model not supported |
| 1326 | if (!supported_models.is_model_supported(model)) { |
| 1327 | throw std::runtime_error("Model " + model + " is not supported."); |
| 1328 | } |
| 1329 | |
| 1330 | int length_limit = request.value("max_tokens", 4096); |
| 1331 | |
| 1332 | if (!ensure_model_loaded(model)) { |
| 1333 | json error_response = {{"error", "Failed to load " + model + " model!"}}; |
| 1334 | send_response(error_response); |
| 1335 | return; |
| 1336 | } |
| 1337 | |
| 1338 | configure_chat_engine_parameters(options, request); |
| 1339 | |
| 1340 | chat_meta_info_t meta_info; |
| 1341 | meta_info.max_prefill_len = this->prefill_chunk_len; |
| 1342 | lm_uniform_input_t uniformed_input; |
| 1343 | header_print("FLM", "Start generating..."); |
| 1344 | |
| 1345 | if (stream) { |
| 1346 | // Create a wrapper callback that passes the pre-formatted SSE string directly |
| 1347 | auto openai_stream_callback = [&send_streaming_response](const std::string& data, bool is_final) { |
| 1348 | json data_json = data; |
| 1349 | send_streaming_response(data_json, is_final); |
| 1350 | }; |
| 1351 | streaming_ostream_openai ostream(model, openai_stream_callback); // streaming in completion format |
| 1352 | uniformed_input.prompt = prompt; |
| 1353 | try { |
| 1354 | bool success = auto_chat_engine->insert(meta_info, uniformed_input); |
| 1355 | if (!success) { |
| 1356 | json error_response = { {"error", "Max length reached"} }; |
| 1357 | send_response(error_response); |
| 1358 | this->auto_chat_engine->clear_context(); |
| 1359 | return; |
| 1360 | } |
| 1361 | } catch (const std::exception& e) { |
| 1362 | json error_response = {{"error", e.what()}}; |
| 1363 | send_response(error_response); |
| 1364 | this->auto_chat_engine->clear_context(); |
| 1365 | return; |
| 1366 | } |
| 1367 | try { |
| 1368 | auto_chat_engine->generate(meta_info, length_limit, ostream); |
| 1369 | } catch (const std::exception& e) { |
| 1370 | json error_response = {{"error", e.what()}}; |
no test coverage detected