Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
Run clang-format on #604
Justine Tunney267b8b590e9f
diff --git a/llamafile/server/client.cpp b/llamafile/server/client.cppindex 6c3caad8..63617d44 100644--- a/llamafile/server/client.cpp+++ b/llamafile/server/client.cpp@@ -587,7 +587,7 @@ Client::dispatcher()if (!g_url_prefix.empty()) {if (FLAG_verbose >= 2) {- SLOG("request path %.*s", (int)p.size(), p.data());+ SLOG("request path %.*s", (int)p.size(), p.data());}size_t prefix_len = g_url_prefix.size();@@ -598,8 +598,7 @@ Client::dispatcher()}// Adjust path view to exclude prefix- p = ctl::string_view(p.data() + prefix_len,- p.size() - prefix_len);+ p = ctl::string_view(p.data() + prefix_len, p.size() - prefix_len);}if (p == "/tokenize")@@ -610,6 +609,8 @@ Client::dispatcher()return embedding();if (p == "/completion")return completion();++ SLOG("path not found: %.*s", (int)p.size(), p.data());return send_error(404);}diff --git a/llamafile/server/main.cpp b/llamafile/server/main.cppindex 080b675f..ffa5d6f5 100644--- a/llamafile/server/main.cpp+++ b/llamafile/server/main.cpp@@ -61,7 +61,8 @@ main(int argc, char* argv[])llamafile_get_flags(argc, argv);// normalize URL prefix- g_url_prefix = normalize_url_prefix(FLAG_url_prefix);+ if (FLAG_url_prefix)+ g_url_prefix = normalize_url_prefix(FLAG_url_prefix);// initialize subsystemstime_init();@@ -130,4 +131,4 @@ main(int argc, char* argv[])while (!pthread_orphan_np())pthread_decimate_np();CheckForMemoryLeaks();-}\ No newline at end of file+}diff --git a/llamafile/server/normalize_url_prefix.cpp b/llamafile/server/normalize_url_prefix.cppnew file mode 100644index 00000000..6397d95e--- /dev/null+++ b/llamafile/server/normalize_url_prefix.cpp@@ -0,0 +1,40 @@+// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-+// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi+//+// Copyright 2024 Mozilla Foundation+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++#include "utils.h"++#include <cosmo.h>++ctl::string+normalize_url_prefix(ctl::string url_prefix)+{+ // Rule 1: Replace multiple slashes with single slash+ while (url_prefix.find("//") != ctl::string::npos) {+ url_prefix.replace(url_prefix.find("//"), 2, "/");+ }++ // Rule 2: Remove trailing slash+ if (!url_prefix.empty() && url_prefix.back() == '/') {+ url_prefix.pop_back();+ }++ // Rule 3: Convert single slash to empty string+ if (url_prefix == "/") {+ url_prefix.clear();+ }+ return url_prefix;+}diff --git a/llamafile/server/or_empty.cpp b/llamafile/server/or_empty.cppnew file mode 100644index 00000000..fbaa484d--- /dev/null+++ b/llamafile/server/or_empty.cpp@@ -0,0 +1,26 @@+// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-+// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi+//+// Copyright 2024 Mozilla Foundation+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++#include "utils.h"++ctl::string_view+or_empty(ctl::optional<ctl::string_view> x)+{+ if (x.has_value())+ return x.value();+ return {};+}diff --git a/llamafile/server/utils.h b/llamafile/server/utils.hindex 4435138d..9e1c3ae6 100644--- a/llamafile/server/utils.h+++ b/llamafile/server/utils.h@@ -25,28 +25,8 @@ extern const signed char kHexToInt[256];boolatob(ctl::string_view, bool);-static inline ctl::string_view-or_empty(ctl::optional<ctl::string_view> x)-{- if (x.has_value())- return x.value();- return {};-}+ctl::string_view+or_empty(ctl::optional<ctl::string_view> x);-static inline ctl::string normalize_url_prefix(ctl::string url_prefix) {- // Rule 1: Replace multiple slashes with single slash- while (url_prefix.find("//") != ctl::string::npos) {- url_prefix.replace(url_prefix.find("//"), 2, "/");- }-- // Rule 2: Remove trailing slash- if (!url_prefix.empty() && url_prefix.back() == '/') {- url_prefix.pop_back();- }-- // Rule 3: Convert single slash to empty string- if (url_prefix == "/") {- url_prefix.clear();- }- return url_prefix;-}\ No newline at end of file+ctl::string+normalize_url_prefix(ctl::string url_prefix);