@brief create lm server @param models the model list @param downloader the downloader @param default_tag the default tag @param port the port @return the server
| 862 | ///@param port the port |
| 863 | ///@return the server |
| 864 | std::unique_ptr<WebServer> create_lm_server(model_list& models, ModelDownloader& downloader, program_args_t& args) { |
| 865 | auto server = std::make_unique<WebServer>(args.host, args.port, args.cors); |
| 866 | auto rest_handler = std::make_shared<RestHandler>(models, downloader, args); |
| 867 | |
| 868 | // Register Ollama-compatible routes |
| 869 | server->register_handler("POST", "/api/show", |
| 870 | [rest_handler](const http::request<http::string_body>& req, |
| 871 | std::function<void(const json&)> send_response, |
| 872 | std::function<void(const json&, bool)> send_streaming_response, |
| 873 | std::shared_ptr<HttpSession> session, |
| 874 | std::shared_ptr<CancellationToken> cancellation_token) { |
| 875 | json request_json; |
| 876 | if (!req.body().empty()) { |
| 877 | request_json = json::parse(req.body()); |
| 878 | } |
| 879 | rest_handler->handle_show(request_json, send_response, send_streaming_response); |
| 880 | }); |
| 881 | |
| 882 | server->register_handler("POST", "/api/generate", |
| 883 | [rest_handler](const http::request<http::string_body>& req, |
| 884 | std::function<void(const json&)> send_response, |
| 885 | std::function<void(const json&, bool)> send_streaming_response, |
| 886 | std::shared_ptr<HttpSession> session, |
| 887 | std::shared_ptr<CancellationToken> cancellation_token) { |
| 888 | json request_json; |
| 889 | if (!req.body().empty()) { |
| 890 | request_json = json::parse(req.body()); |
| 891 | } |
| 892 | rest_handler->handle_generate(request_json, send_response, send_streaming_response, cancellation_token); |
| 893 | }); |
| 894 | |
| 895 | server->register_handler("POST", "/api/chat", |
| 896 | [rest_handler](const http::request<http::string_body>& req, |
| 897 | std::function<void(const json&)> send_response, |
| 898 | std::function<void(const json&, bool)> send_streaming_response, |
| 899 | std::shared_ptr<HttpSession> session, |
| 900 | std::shared_ptr<CancellationToken> cancellation_token) { |
| 901 | json request_json; |
| 902 | if (!req.body().empty()) { |
| 903 | request_json = json::parse(req.body()); |
| 904 | } |
| 905 | rest_handler->handle_chat(request_json, send_response, send_streaming_response, cancellation_token); |
| 906 | }); |
| 907 | |
| 908 | server->register_handler("GET", "/api/ps", |
| 909 | [rest_handler](const http::request<http::string_body>& req, |
| 910 | std::function<void(const json&)> send_response, |
| 911 | std::function<void(const json&, bool)> send_streaming_response, |
| 912 | std::shared_ptr<HttpSession> session, |
| 913 | std::shared_ptr<CancellationToken> cancellation_token) { |
| 914 | json request_json; |
| 915 | rest_handler->handle_ps(request_json, send_response, send_streaming_response); |
| 916 | }); |
| 917 | |
| 918 | server->register_handler("POST", "/api/embeddings", |
| 919 | [rest_handler](const http::request<http::string_body>& req, |
| 920 | std::function<void(const json&)> send_response, |
| 921 | std::function<void(const json&, bool)> send_streaming_response, |
no test coverage detected