ollama list模型列表获取 接口代码

ollama list模型列表获取 接口代码

复制代码
curl http://localhost:11434/v1/models

coding

复制代码
package hcx.ollama;

/**
 * @ClassName DockerOllamaList
 * @Description TODO
 * @Author dell
 * @Date 2025/5/26 11:31
 * @Version 1.0
 **/


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DockerOllamaListApi {
    public static List<String> getOllamaModelNames(String host, int port) {
        List<String> modelNames = new ArrayList<>();
        try {
            URL url = new URL(String.format("http://%s:%d/v1/models", host, port));
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(conn.getInputStream())
            );
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            // 使用正则表达式提取模型ID
            Pattern pattern = Pattern.compile("\"id\":\"([^\"]+)\".*?\"object\":\"model\"");
            Matcher matcher = pattern.matcher(response.toString());

            while (matcher.find()) {
                modelNames.add(matcher.group(1));
            }

        } catch (Exception e) {
            throw new RuntimeException("获取Ollama模型列表失败", e);
        }
        return modelNames;
    }

    public static void main(String[] args) {
        try {
            // 使用Ollama API获取模型列表
            List<String> modelNames = getOllamaModelNames("127.0.0.1", 11434);
            System.out.println("模型列表:");
            for (String modelName : modelNames) {
                System.out.println(modelName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
相关推荐
liwulin05061 天前
【ollama】自定义结构化输出
linux·前端·数据库·ollama
做个文艺程序员1 天前
Linux第27篇:在Linux服务器部署本地大模型:Ollama+开源LLM实战
linux·服务器·开源·大模型·ollama
寒水馨1 天前
Windows下载、安装ollama-v0.32.1(附安装包OllamaSetup.exe)
windows·llm·大语言模型·llama·本地部署·ollama·模型运行
gerrywhu1 天前
【产品研发】基于本地OCR大模型文书自动生成软件开发
ollama·本地部署大模型·离线部署·ocr大模型·paddleocr-vl·glm-ocr·文档自动生成
skywalk81632 天前
用ollama启动duan-translator 小模型
服务器·ollama·段言
行者-全栈开发5 天前
【码动四季】Spring AI + RAG 电商知识库:AtomCode 如何让 Embedding 对齐从 3 天缩短到 4 小时
embedding·向量检索·ollama·spring ai·pgvector·atomcode·rag电商知识库
weipt6 天前
用Ollama开发聊天程序实战
python·ollama
闲猫9 天前
Ollama 本地部署,Python、SpringAI对接Ollama
人工智能·python·ollama·spring ai
zhou1352848226712 天前
Linux 系统安装 Ollama 详细教程
linux·ubuntu·wsl·ollama
xifangge202513 天前
用 Ollama 本地部署 Codex + 接入 DeepSeek-R1 零成本避坑指南(附桥接配置)
ai编程·codex·ollama·deepseek