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();
        }
    }
}
相关推荐
razelan2 天前
本地大模型系列:2.通过API让本地大模型为你服务
人工智能·api·ollama·本地大模型
樂油2 天前
小龙虾OpenClaw本地部署(四)连接ollama模型(qwen3.5:0.8b为例)
ollama·openclaw
弗锐土豆3 天前
使用ollama运行本地大模型
llm·大语言模型·安装·ollama
SP八岐大兔3 天前
Ollama安装及运行模型
linux·服务器·ollama
竹之却3 天前
Ubuntu 系统安装 Ollama 教程
linux·运维·ubuntu·ollama
小田学Python4 天前
简明教程:实现OpenCLaw轻量级应用服务器部署及Ollama大模型本地化
ai·大模型·ollama·openclaw
封奚泽优4 天前
Zotero(Awesome GPT)+Ollama
gpt·zotero·ollama
小田学Python4 天前
Dify+Ollama模型搭建攻略:本地环境实战指南
大模型·qwen·dify·ollama
lcx_defender4 天前
【Ollama】Windows系统使用Ollama部署本地大模型
ollama
蜡台5 天前
整合一些 免费甚至无限量token平台,方便OpenClaw 使用,及一些相关使用配置代码
nvidia·token·ollama·openrouter·openclaw·龙虾