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();
        }
    }
}
相关推荐
beyond谚语3 天前
六、LangChain——StrOutputParser和JsonOutputParser
langchain·rag·ollama
大模型真好玩8 天前
大模型训练全流程实战指南实战篇(十四)——网络安全大模型数据获取
人工智能·ollama·deepseek
蜡台8 天前
本地搭建 AI 大模型完整实战指南(2026)
人工智能·ollama
lee_curry10 天前
Dify+Ollama私有化部署方案
ai大模型·dify·ollama
蛋先生DX11 天前
大模型本地部署神器的瘦身进阶原理,就这两招?
llm·llama·ollama
geminigoth12 天前
LangChain1.2学习第二章—— 调用大模型
ollama·本地部署大模型·deepseek接口·qwen接口·langchain调用大模型·阻塞大模型接口·非阻塞大模型接口
weixin_4713830312 天前
25 Ollama 本地部署
ollama
FriendshipT12 天前
DeepSeek Harness 使用 Ollama 本地部署的 AI 大模型(以Qwen3.8-27B为例)
人工智能·pytorch·深度学习·ollama·deepseek
uncle_ll12 天前
多模态大模型视听生成本地实战
llm·文生图·文生视频·多模态·ollama
Moon上有月亮13 天前
Ollama 完全指南:本地大语言模型的“开箱即用”方案
人工智能·语言模型·自然语言处理·ollama