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();
        }
    }
}
相关推荐
SCBAiotAigc1 天前
在Ubuntu上使用docker compose安装普通(不支持GPU)的Ollama服务
人工智能·python·ubuntu·ollama
fanruitian2 天前
windows安装离线大模型 ollama qwen
大模型·qwen·ollama
福大大架构师每日一题3 天前
ollama v0.13.5 发布详解:新模型接入、引擎升级与工具能力增强
ollama
咩图4 天前
使用ollama完成私有大模型搭建
大模型·ollama·私有化
福大大架构师每日一题4 天前
ollama v0.13.4 发布——全新模型与性能优化详解
stm32·嵌入式硬件·性能优化·ollama
thginWalker6 天前
本地安装Ollama并使用Python调用
ollama
大模型教程7 天前
使用Langchain4j和Ollama3搭建RAG系统
langchain·llm·ollama
scx_link10 天前
使用docker安装ollama及ollama拉取模型的总结
运维·docker·容器·ollama
福大大架构师每日一题11 天前
ollama v0.13.2 最新更新详解:Qwen3-Next首发与性能优化
性能优化·ollama
starvapour13 天前
配置ollama的显卡和模型保存路径(Ubuntu, systemd)
linux·ubuntu·ollama