@brief Handle the generate request @param request the request @param send_response the send response @param send_streaming_response the send streaming response
| 591 | ///@param send_response the send response |
| 592 | ///@param send_streaming_response the send streaming response |
| 593 | void RestHandler::handle_generate(const json& request, |
| 594 | std::function<void(const json&)> send_response, |
| 595 | StreamResponseCallback send_streaming_response, |
| 596 | std::shared_ptr<CancellationToken> cancellation_token) { |
| 597 | try { |
| 598 | std::string prompt = request["prompt"]; |
| 599 | bool stream = request.value("stream", true); |
| 600 | std::string model = request.value("model", current_model_tag); |
| 601 | json options = request.value("options", json::object()); |
| 602 | |
| 603 | int length_limit = request.value("max_tokens", 4096); |
| 604 | auto load_start_time = time_utils::now(); |
| 605 | // TODO: Use Another Check Function avoid loading again |
| 606 | if (!ensure_model_loaded(model)) { |
| 607 | json error_response = {{"error", "Failed to load " + model + " model!"}}; |
| 608 | send_response(error_response); |
| 609 | return; |
| 610 | } |
| 611 | auto load_end_time = time_utils::now(); |
| 612 | |
| 613 | chat_meta_info_t meta_info; |
| 614 | lm_uniform_input_t uniformed_input; |
| 615 | meta_info.max_prefill_len = this->prefill_chunk_len; |
| 616 | meta_info.load_duration = (uint64_t)time_utils::duration_ns(load_start_time, load_end_time).first; |
| 617 | header_print("FLM", "Start generating..."); |
| 618 | |
| 619 | if (stream) { |
| 620 | // Streaming response using streaming_ostream |
| 621 | auto total_start_time = time_utils::now(); |
| 622 | streaming_ostream ostream(model, send_streaming_response, false); |
| 623 | uniformed_input.prompt = prompt; |
| 624 | try { |
| 625 | bool success = auto_chat_engine->insert(meta_info, uniformed_input); |
| 626 | if (!success){ |
| 627 | json error_response = {{"error", "Max length reached"}}; |
| 628 | send_response(error_response); |
| 629 | this->auto_chat_engine->clear_context(); |
| 630 | return; |
| 631 | } |
| 632 | } catch (const std::exception& e) { |
| 633 | json error_response = {{"error", e.what()}}; |
| 634 | send_response(error_response); |
| 635 | this->auto_chat_engine->clear_context(); |
| 636 | return; |
| 637 | } |
| 638 | try { |
| 639 | auto_chat_engine->generate(meta_info, length_limit, ostream); |
| 640 | } catch (const std::exception& e) { |
| 641 | json error_response = {{"error", e.what()}}; |
| 642 | send_response(error_response); |
| 643 | this->auto_chat_engine->clear_context(); |
| 644 | return; |
| 645 | } |
| 646 | auto total_end_time = time_utils::now(); |
| 647 | auto history = this->auto_chat_engine->get_history(); |
| 648 | // std::cout << "history: " << history.first << std::endl; |
| 649 | meta_info.total_duration = (uint64_t)time_utils::duration_ns(total_start_time, total_end_time).first; |
| 650 | ostream.finalize_generate(meta_info, history.second); |
no test coverage detected