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();
        }
    }
}
相关推荐
曲幽1 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
曲幽2 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
曲幽3 天前
FastAPI + Ollama 实战:搭一个能查天气的AI助手
python·ai·lora·torch·fastapi·web·model·ollama·weatherapi
bwz999@88.com7 天前
CentOS 7安装ollama
linux·centos·ollama
bwz999@88.com7 天前
CentOS 7 docker安装ollama、open-webui
ollama
@SmartSi9 天前
Ollama 实战:从零开始本地运行大语言开源模型
llm·ollama
风笑谷10 天前
openclaw+ollama+企业微信 实操记录
企业微信·ollama·openclaw
CrazyStoneZw13 天前
openclaw配置ollama本地模型
本地化·ollama·openclaw
love530love15 天前
【OpenClaw 本地实战 Ep.2】零代码对接:使用交互式向导快速连接本地 LM Studio 用 CUDA GPU 推理
人工智能·windows·gpu·cuda·ollama·lm studio·openclaw
yyoc9718 天前
Mac基于LLaMA Factory微调模型导入Ollama踩坑记录
大模型·微调·llama·ollama