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();
        }
    }
}
相关推荐
匹诺曹i3673 天前
memory_search 对了、7B 总结错了:OpenClaw D4 来源引用与 8 个坑
ollama
匹诺曹i3674 天前
docx 报表进 OpenClaw 本地 RAG:memory 索引实践与 6 个坑
ollama
福大大架构师每日一题16 天前
ollama v0.30.8 最新更新解读:修复启动提供方选择错误,提示词缓存更稳,MLX 推理与递归模型全面增强
ollama
大佐不会说日语~17 天前
在 Windows 本地用 Docker 部署向量模型(bge-m3)
windows·docker·容器·llm·ollama
腾飞开源18 天前
05_Dify接入Ollama本地大模型
人工智能·项目实战·dify·ai智能体·ollama·企业级应用·接入模型
逻极18 天前
Windows 平台 Ollama AMD GPU 一键编译指南:基于 ROCm 7.1 的自动化实战
人工智能·windows·stm32·自动化·gpu·amd·ollama
yuanzhengme20 天前
Ollama【部署 07】搭建本地智能体的简单说明(局域网离线部署Ollama+模型迁移+Docker部署AnythingLLM)
运维·docker·容器·大模型·ollama·本地智能体
hdsoft_huge21 天前
部署 Nacos + Ollama + vLLM + MCP 完整图文教程(1Panel 面板,命令行安装两种方式)
python·vllm·ollama·mcp
小七-七牛开发者23 天前
本地模型为什么能跑起来?从 llama.cpp 量化说起
agent·llama·模型部署·ollama·本地模型