@brief Handle the embeddings request @param request the request @param send_response the send response @param send_streaming_response the send streaming response
| 824 | ///@param send_response the send response |
| 825 | ///@param send_streaming_response the send streaming response |
| 826 | void RestHandler::handle_embeddings(const json& request, |
| 827 | std::function<void(const json&)> send_response, |
| 828 | StreamResponseCallback send_streaming_response) { |
| 829 | try { |
| 830 | std::string model = request["model"]; |
| 831 | std::vector<std::string> inputs; |
| 832 | |
| 833 | if (request["input"].is_string()) { |
| 834 | inputs.push_back(request["input"].get<std::string>()); |
| 835 | } |
| 836 | else if (request["input"].is_array()) { |
| 837 | for (const auto& item : request["input"]) { |
| 838 | inputs.push_back(item.get<std::string>()); |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | json response; |
| 843 | if (this->embed) { |
| 844 | json embedding_data = json::array(); |
| 845 | #ifndef FASTFLOWLM_LINUX_LIMITED_MODELS |
| 846 | for (size_t i = 0; i < inputs.size(); ++i) { |
| 847 | std::cout << "Embedding input[" << i << "]: " << "\n" << inputs[i] << std::endl; |
| 848 | std::vector<float> embedding_result = this->auto_embedding_engine->embed(inputs[i], embedding_task_type_t::task_query); |
| 849 | embedding_data.push_back({ |
| 850 | {"object", "embedding"}, |
| 851 | {"embedding", embedding_result}, |
| 852 | {"index", i} |
| 853 | }); |
| 854 | } |
| 855 | #else |
| 856 | throw std::runtime_error("Embedding models are not supported in this build"); |
| 857 | #endif |
| 858 | |
| 859 | response = { |
| 860 | {"object", "list"}, |
| 861 | {"data", embedding_data}, |
| 862 | {"model", model}, |
| 863 | {"usage", { |
| 864 | {"prompt_tokens", 0}, |
| 865 | {"total_tokens", 0} |
| 866 | }} |
| 867 | }; |
| 868 | } |
| 869 | else { |
| 870 | header_print("Warning", "No embedding model loaded"); |
| 871 | } |
| 872 | send_response(response); |
| 873 | } |
| 874 | catch (const std::exception& e) { |
| 875 | json error_response = {{"error", e.what()}}; |
| 876 | send_response(error_response); |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | ///@brief Handle the models request |
| 881 | ///@param request the request |