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();
        }
    }
}
相关推荐
尽兴-3 天前
【10 分钟!M4 Mac mini 离线部署「私有 ChatGPT」完整实录】
macos·ai·chatgpt·大模型·ollama·私有化
成子不是橙子3 天前
Langchain | Ollama | Python快速上手使用LLM的DEMO
开发语言·python·langchain·ollama
深色風信子4 天前
SpringAI 本地调用 Ollama
rag·ollama·springai·springai ollama
大模型教程4 天前
AI 新玩法:GraphRAG × Ollama 打造更聪明的智能体
程序员·llm·ollama
tongsound7 天前
ollama部署本地模型(deepseek,qwen,gemma3,...)
ollama·deepseek
laizi_laizi7 天前
ollama+openwebui本地部署及踩坑记录
ollama·openwebui
深色風信子10 天前
SpringBoot 集成 LangChain4j 本地调用 Ollama
java·spring boot·spring·ollama·langchain4j
不语n13 天前
Windows+Docker+AI开发板打造智能终端助手
python·docker·树莓派·香橙派·dify·ollama·ai开发板
安娜的信息安全说15 天前
Ollama 使用详解:本地部署大语言模型的指南
人工智能·ai·语言模型·ollama
安娜的信息安全说16 天前
Hugging Face vs Ollama:云端协作与本地隐私的AI平台对决
人工智能·hugging face·ollama