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();
        }
    }
}
相关推荐
问道飞鱼2 天前
【大模型知识】Chroma + Ollama + Llama 3.1 搭建本地知识库
llama·知识库·chroma·ollama
xiucai_cs3 天前
AI RAG 本地知识库实战
人工智能·知识库·dify·rag·ollama
玄同7653 天前
Llama.cpp 全实战指南:跨平台部署本地大模型的零门槛方案
人工智能·语言模型·自然语言处理·langchain·交互·llama·ollama
阿尔的代码屋4 天前
[大模型实战 01] 本地大模型初体验:Ollama 部署与 Python 调用指南
qwen·modelscope·大模型实战·ollama·大模型部署
skywalk81634 天前
尝试在openi启智社区的dcu环境安装ollama最新版0.15.2(失败)
linux·运维·服务器·ollama
Lo-Y-eH4 天前
Mac 安装 Ollama 部署 DeepSeek 模型
macos·ollama·deepseek·cherry studio
Jack_abu6 天前
谷歌开源翻译模型 TranslateGemma 深度解析与本地部署实践
llm·机器翻译·ollama·开源翻译模型
雨声不在7 天前
ollama日常使用
ai·ollama
skywalk81637 天前
使用llama.cpp和ollama推理LFM2.5-1.2B模型
llama·ollama·lfm2.5-1.2b
用什么都重名8 天前
【Dify学习笔记】:Dify插件离线转换教程
插件·dify·ollama