diff options
| author | Michael Peter Christen <mc@yacy.net> | 2025-11-16 21:43:26 +0100 |
|---|---|---|
| committer | Michael Peter Christen <mc@yacy.net> | 2025-11-16 21:43:26 +0100 |
| commit | bfdbcf58393b91dbbcf6dec713544480fea0b0fa (patch) | |
| tree | 42eb47c0776a4bd624e5ac138e8f77c0cfdf1778 | |
| parent | ff15a744b272f5713d9c7e11dca1416e9f250c6a (diff) | |
added OpenAI models servlet and Ollama tags servlet
| -rw-r--r-- | defaults/web.xml | 24 | ||||
| -rw-r--r-- | source/net/yacy/http/servlets/OllamaTagsServlet.java | 87 | ||||
| -rw-r--r-- | source/net/yacy/http/servlets/OpenAIModelsServlet.java | 87 |
3 files changed, 196 insertions, 2 deletions
diff --git a/defaults/web.xml b/defaults/web.xml index 43090a8e1..0e5241e6c 100644 --- a/defaults/web.xml +++ b/defaults/web.xml @@ -53,7 +53,17 @@ <servlet> <servlet-name>RAGProxyServlet</servlet-name> <servlet-class>net.yacy.http.servlets.RAGProxyServlet</servlet-class> - </servlet> + </servlet> + + <servlet> + <servlet-name>OllamaTagsServlet</servlet-name> + <servlet-class>net.yacy.http.servlets.OllamaTagsServlet</servlet-class> + </servlet> + + <servlet> + <servlet-name>OpenAIModelsServlet</servlet-name> + <servlet-class>net.yacy.http.servlets.OpenAIModelsServlet</servlet-class> + </servlet> <servlet> <servlet-name>MCPSearchServlet</servlet-name> @@ -97,7 +107,17 @@ <servlet-name>RAGProxyServlet</servlet-name> <url-pattern>/v1/chat/completions</url-pattern> </servlet-mapping> - + + <servlet-mapping> + <servlet-name>OllamaTagsServlet</servlet-name> + <url-pattern>/api/tags</url-pattern> + </servlet-mapping> + + <servlet-mapping> + <servlet-name>OpenAIModelsServlet</servlet-name> + <url-pattern>/v1/models</url-pattern> + </servlet-mapping> + <servlet-mapping> <servlet-name>MCPSearchServlet</servlet-name> <url-pattern>/tools</url-pattern> diff --git a/source/net/yacy/http/servlets/OllamaTagsServlet.java b/source/net/yacy/http/servlets/OllamaTagsServlet.java new file mode 100644 index 000000000..85dc190ca --- /dev/null +++ b/source/net/yacy/http/servlets/OllamaTagsServlet.java @@ -0,0 +1,87 @@ +/** + * OllamaTagsServlet + * Copyright 2025 by Michael Peter Christen + * First released 16.11.2025 at https://yacy.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program in the file lgpl21.txt + * If not, see <http://www.gnu.org/licenses/>. + */ + +package net.yacy.http.servlets; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.ServletOutputStream; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.solr.servlet.cache.Method; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import net.yacy.ai.LLM; + +public class OllamaTagsServlet extends HttpServlet { + + private static final long serialVersionUID = 3411344789759603107L; + + @Override + public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException { + response.setContentType("application/json;charset=utf-8"); + + HttpServletResponse hresponse = (HttpServletResponse) response; + HttpServletRequest hrequest = (HttpServletRequest) request; + + // Add CORS headers + hresponse.setHeader("Access-Control-Allow-Origin", "*"); + hresponse.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); + hresponse.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); + + final Method reqMethod = Method.getMethod(hrequest.getMethod()); + + // We expect a POST request + if (reqMethod != Method.GET) { + hresponse.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); + return; + } + + ServletOutputStream out = response.getOutputStream(); + try { + JSONObject data = new JSONObject(); + JSONArray models = new JSONArray(); + data.put("models", models); + + // the model names are not the real model names but the usage classes that can be assigned to a model + for (LLM.LLMUsage usage: LLM.LLMUsage.values()) { + String modelName = usage.name(); + JSONObject model = new JSONObject(); + model.put("name", modelName); + model.put("model", modelName); + models.put(model); + } + + out.println(data.toString()); + out.flush(); + hresponse.setStatus(200); + out.close(); // close this here to end transmission + } catch (JSONException e) { + e.printStackTrace(); + } + } +} diff --git a/source/net/yacy/http/servlets/OpenAIModelsServlet.java b/source/net/yacy/http/servlets/OpenAIModelsServlet.java new file mode 100644 index 000000000..c4d9f0f71 --- /dev/null +++ b/source/net/yacy/http/servlets/OpenAIModelsServlet.java @@ -0,0 +1,87 @@ +/** + * OpenAIModelsServlet + * Copyright 2025 by Michael Peter Christen + * First released 16.11.2025 at https://yacy.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program in the file lgpl21.txt + * If not, see <http://www.gnu.org/licenses/>. + */ + +package net.yacy.http.servlets; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.ServletOutputStream; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.solr.servlet.cache.Method; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import net.yacy.ai.LLM; + +public class OpenAIModelsServlet extends HttpServlet { + + private static final long serialVersionUID = 3411544789739603107L; + + @Override + public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException { + response.setContentType("application/json;charset=utf-8"); + + HttpServletResponse hresponse = (HttpServletResponse) response; + HttpServletRequest hrequest = (HttpServletRequest) request; + + // Add CORS headers + hresponse.setHeader("Access-Control-Allow-Origin", "*"); + hresponse.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); + hresponse.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); + + final Method reqMethod = Method.getMethod(hrequest.getMethod()); + + // We expect a POST request + if (reqMethod != Method.GET) { + hresponse.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); + return; + } + + ServletOutputStream out = response.getOutputStream(); + try { + JSONObject data = new JSONObject(); + JSONArray models = new JSONArray(); + data.put("data", models); + + // the model names are not the real model names but the usage classes that can be assigned to a model + for (LLM.LLMUsage usage: LLM.LLMUsage.values()) { + String modelName = usage.name(); + JSONObject model = new JSONObject(); + model.put("id", modelName); + model.put("object", "model"); + models.put(model); + } + + out.println(data.toString()); + out.flush(); + hresponse.setStatus(200); + out.close(); // close this here to end transmission + } catch (JSONException e) { + e.printStackTrace(); + } + } +} |
