Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

LLM selection servlet (stub)

Michael Peter Christen9888473d36e9
diff --git a/htroot/LLMSelection_p.html b/htroot/LLMSelection_p.html
new file mode 100644
index 000000000..923de1a46
--- /dev/null
+++ b/htroot/LLMSelection_p.html
@@ -0,0 +1,124 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
+<!-- This page is only XHTML 1.0 Transitional because target is being used in a links -->
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <title>YaCy '#[clientname]#': LLM Selection</title>
+ #%env/templates/metas.template%#
+ </head>
+ <body id="IndexControl">
+ #%env/templates/header.template%#
+ #%env/templates/submenuIndexImport.template%#
+ <script>
+ function setHoststub() {
+ const provider = document.getElementById("provider").value;
+ const hoststubInput = document.getElementById("hoststub");
+
+ if (provider === "OLLAMA") {
+ hoststubInput.value = "http://localhost:11434";
+ } else if (provider === "LMSTUDIO") {
+ hoststubInput.value = "http://localhost:1234";
+ } else if (provider === "OPENAI") {
+ hoststubInput.value = "https://api.openai.com";
+ } else if (provider === "OPENROUTER") {
+ hoststubInput.value = "https://openrouter.ai/api";
+ } else {
+ hoststubInput.value = ""; // Clear the hoststub if another provider is selected
+ }
+ }
+
+ async function loadModels() {
+ const provider = document.getElementById("provider").value;
+ const hoststub = document.getElementById("hoststub").value;
+ const apiUrl = `${hoststub}/api/tags`;
+
+ try {
+ const response = await fetch(apiUrl);
+ const data = await response.json();
+
+ const models = data.models;
+ const modelsContainer = document.getElementById("modelsContainer");
+
+ models.forEach(model => {
+ const radioBtn = document.createElement("input");
+ radioBtn.type = "radio";
+ radioBtn.name = "model";
+ radioBtn.value = model.model;
+ radioBtn.id = model.model;
+
+ const label = document.createElement("label");
+ label.htmlFor = model.model;
+ label.textContent = model.model;
+
+ const br = document.createElement("br");
+
+ modelsContainer.appendChild(radioBtn);
+ modelsContainer.appendChild(label);
+ modelsContainer.appendChild(br);
+ });
+
+
+ } catch (error) {
+ console.error("Error fetching models:", error);
+ alert("Failed to load models. Check the hoststub and console for errors.");
+ }
+ }
+ </script>
+
+
+ <h2>LLM Selection</h2>
+
+ <form id="llmForm">
+ <fieldset><legend>Provider</legend>
+ <dl>
+ <dt class="TableCellDark">Select a LLM Provider</dt>
+ <dd>
+ <select name="provider" id="provider" class="form-control" onchange="setHoststub()">
+ <option value="OLLAMA" selected="selected">Ollama</option>
+ <option value="LMSTUDIO">LMStudio</option>
+ <option value="OPENAI">OpenAI</option>
+ <option value="OPENROUTER">Open Router</option>
+ </select>&nbsp; This makes a preset to the Hoststub value
+ </dd>
+
+ <dt class="TableCellDark">Hoststub</dt>
+ <dd><input type="text" name="hoststub" id="hoststub" value="http://localhost:11434" size="30" maxlength="60" class="form-control"/>&nbsp; you can probably leave this to the default value
+ </dd>
+
+ <dt class="TableCellDark">API Key</dt>
+ <dd><input type="text" name="apikey" id="apikey" value="" size="30" maxlength="60" class="form-control"/>&nbsp; (not required for Ollama or LMStudio)
+ </dd>
+
+ <dt class="TableCellDark">Max Token</dt>
+ <dd>
+ <select id="maxtoken" name="maxtoken" class="form-control">
+ <option selected="selected">4096</option>
+ <option>8192</option>
+ <option>16384</option>
+ <option>32768</option>
+ <option>65536</option>
+ <option>131072</option>
+ <option>262.44</option>
+ </select>&nbsp; You must set the Context Length in the LLM provider to fit to your selected max token; in Ollama you find a Context Length slider in the settings
+ </dd>
+
+ <dt>&nbsp;</dt>
+ <dd><input name="llmselection" value="Load Model Name List" class="btn btn-primary" style="width:240px;" onclick="loadModels()"/>
+ </dd>
+ </dl>
+ </fieldset>
+ </form>
+
+ <fieldset id="modelsContainer"><legend>Models</legend></fieldset>
+
+ <fieldset><legend>LLM List</legend>
+ <table border="0" summary="Pack List Archive">
+ <tr class="TableHeader"><td>Pack</td><td>Process</td><td>Size (KB)</td></tr>
+ #{packs}#
+ <tr class="TableCell#(dark)#Light::Dark#(/dark)#"><td>#[file]#</td><td>#[type]#</td><td>#[size]#</td></tr>
+ #{/packs}#
+ </table>
+ </fieldset>
+
+ #%env/templates/footer.template%#
+ </body>
+</html>
diff --git a/source/net/yacy/htroot/LLMSelection_p.java b/source/net/yacy/htroot/LLMSelection_p.java
new file mode 100644
index 000000000..2e8a556e9
--- /dev/null
+++ b/source/net/yacy/htroot/LLMSelection_p.java
@@ -0,0 +1,161 @@
+// IndexExport_p.java
+// -----------------------
+// (C) 2004-2007 by Michael Peter Christen; [email protected], Frankfurt a. M., Germany
+// first published 2004 on http://yacy.net
+//
+// This is a part of YaCy, a peer-to-peer based web search engine
+//
+// LICENSE
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+package net.yacy.htroot;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Date;
+
+import net.yacy.cora.date.GenericFormatter;
+import net.yacy.cora.protocol.RequestHeader;
+import net.yacy.data.WorkTables;
+import net.yacy.search.Switchboard;
+import net.yacy.search.SwitchboardConstants;
+import net.yacy.search.index.Fulltext;
+import net.yacy.search.index.Segment;
+import net.yacy.search.schema.CollectionSchema;
+import net.yacy.server.serverObjects;
+import net.yacy.server.serverSwitch;
+
+public class LLMSelection_p {
+
+ public static serverObjects respond(@SuppressWarnings("unused") final RequestHeader header, final serverObjects post, final serverSwitch env) {
+ // return variable that accumulates replacements
+ final Switchboard sb = (Switchboard) env;
+
+ final serverObjects prop = new serverObjects();
+
+ final Segment segment = sb.index;
+ // we have two counts of document: total number and such that are exportable with status code 200
+ final long ucount = segment.fulltext().collectionSize();
+ long ucount200 = ucount;
+ try {
+ ucount200 = segment.fulltext().getDefaultConnector().getCountByQuery(CollectionSchema.httpstatus_i.getSolrFieldName() + ":200");
+ } catch (final IOException e1) {}
+
+ // set default values
+ prop.put("reload", 0);
+ prop.put("lurlexport", 0);
+ prop.putNum("ucount", ucount);
+ prop.putNum("ucount200", ucount200);
+
+ // show Pack folder contents
+ int i = 0;
+ boolean dark = true;
+ for (final String file: sb.packsInHold()) {
+ prop.put("packs_" + i + "_file", file);
+ prop.put("packs_" + i + "_type", "hold");
+ prop.put("packs_" + i + "_size", new File(sb.packsHoldPath, file).length() / 1024);
+ prop.put("packs_" + i + "_dark", dark ? "1" : "0");
+ i++;
+ dark = !dark;
+ }
+ for (final String file: sb.packsInLoaded()) {
+ prop.put("packs_" + i + "_file", file);
+ prop.put("packs_" + i + "_type", "loaded");
+ prop.put("packs_" + i + "_size", new File(sb.packsLoadedPath, file).length() / 1024);
+ prop.put("packs_" + i + "_dark", dark ? "1" : "0");
+ i++;
+ dark = !dark;
+ }
+ for (final String file: sb.packsInLive()) {
+ prop.put("packs_" + i + "_file", file);
+ prop.put("packs_" + i + "_type", "live");
+ prop.put("packs_" + i + "_size", new File(sb.packsLivePath, file).length() / 1024);
+ prop.put("packs_" + i + "_dark", dark ? "1" : "0");
+ i++;
+ dark = !dark;
+ }
+ prop.put("packs", i);
+
+ if (post == null || env == null) {
+ return prop; // nothing to do
+ }
+
+ if (post.containsKey("lurlexport")) {
+ try {
+ // parse format
+ Fulltext.ExportFormat format = Fulltext.ExportFormat.elasticsearch;
+ final String fname = post.get("format", "full-elasticsearch");
+ final boolean dom = fname.startsWith("dom"); // if dom== false complete urls are exported, otherwise only the domain
+ final boolean text = fname.startsWith("text");
+ if (fname.endsWith("rss")) format = Fulltext.ExportFormat.rss;
+ if (fname.endsWith("solr")) format = Fulltext.ExportFormat.solr;
+ if (fname.endsWith("elasticsearch")) format = Fulltext.ExportFormat.elasticsearch;
+
+ final String filter = post.get("exportfilter", ".*");
+ String query = post.get("exportquery", "*:*");
+ final String collection = post.get("collection", "user");
+ query += " AND " + CollectionSchema.collection_sxt.getSolrFieldName() + ":\"" + collection + "\"";
+
+ // store this call as api call: we do this even if there is a chance that it fails because recurring calls may do not fail
+ sb.tables.recordAPICall(post, "IndexPackGenerator_p.html", WorkTables.TABLE_API_TYPE_DUMP, "PackGenerator, q=" + query);
+
+ // start the export
+ /*
+ Tier Tags:
+ | Tier | Size | Notes |
+ |-----------|-----------|--------------------|
+ | common | ≤ 1 GB | IndexPackGenerator |
+ | uncommon | 1–5 GB | large web crawls |
+ | rare | 5–50 GB | custom parser |
+ | epic | 50–200 GB | special infra |
+ | legendary | any | human curation |
+ */
+ final long now = System.currentTimeMillis();
+ final long doccount = sb.index.fulltext().getDefaultConnector().getCountByQuery(query);
+ if (doccount == 0) throw new IOException("number of exported documents == 0");
+ final String category = post.get("category", "scroll"); // core, scroll, codex, gem, fiction, map, echo, spirit, vault
+ final String tier = "common"; // common, uncommon, rare, epic, legendary, legendary
+ final String origin = "web"; // web, synth,
+ String slug = post.get("slug", "export").trim().replaceAll(" ", "-");
+ if (slug.isEmpty()) slug = "export";
+
+ // if collection is not user, the slug is the collection name
+ if (!"user".equals(collection)) {
+ slug = collection.trim().replaceAll(" ", "-");
+ }
+ // we can not construct the file name
+ final String filename =
+ SwitchboardConstants.YACY_PACK_PREFIX +
+ category + "-" + tier + "-" + origin + "_" +
+ slug + "_" +
+ GenericFormatter.SHORT_DAY_FORMATTER.format(new Date(now));
+ // file name schema: YaCyPack_<category>-<tier>-<origin>_<slug>_<YYMMDD>.jsonlist
+ // possible storage paths are: hold, load, loaded, unload, live; we use hold here, loaded would also be correct
+ } catch (final IOException e) {
+ prop.put("lurlexporterror", 1);
+ prop.put("lurlexporterror_exportfile", "-no export-");
+ prop.put("lurlexporterror_exportfailmsg", e.getMessage());
+ return prop;
+ }
+ }
+
+ // insert constants
+ prop.putNum("ucount", ucount);
+ // return rewrite properties
+ return prop;
+ }
+
+}
\ No newline at end of file