AI 正迅速成为现代应用的核心组成部分,但在本地运行大语言模型(LLM)仍令人头疼。从选择合适的模型、应对硬件兼容性问题到优化性能,开发者往往在构建应用前就已举步维艰。与此同时,越来越多开发者希望能在本地灵活运行 LLM,用于开发、测试甚至离线场景 - 这正是 Docker Model Runner 的用武之地。
docker model run ai/smollm2:360M-Q4_K_M "Give me a fact about whales." Whales are magnificent marine animals that have fascinated humans for centuries. They belong to the order Cetacea and have a unique body structure that allows them to swim and move around the ocean. Some species of whales, like the blue whale, can grow up to 100 feet (30 meters) long and weigh over 150 tons (140 metric tons) each. They are known for their powerful tails that propel them through the water, allowing them to dive deep to find food or escape predators.
那么当我们运行 docker model run 命令时,底层究竟发生了什么?了解技术原理很有必要,因为它与多年来使用的 docker container run 命令有很大不同。对于 Model Runner,这个命令不会启动任何类型的容器。相反,它会调用由 Docker Desktop 中的 Model Runner 托管的推理服务 API 端点,并提供 OpenAI 兼容的 API。推理服务器将 llama.cpp 作为推理引擎,以原生主机进程形式运行,按需加载请求的模型,然后对接收到的请求执行推理。之后,该模型会一直保留在内存中,直到请求另一个模型,或者达到预定义的非活动超时(目前为 5 分钟)。
这也意味着在从主机进程或容器内与特定模型交互之前,无需执行 docker model run。Model Runner 会透明地按需加载请求的模型,前提是该模型已预先拉取并在本地可用。
OpenAiChatModel model = OpenAiChatModel.builder() .baseUrl("http://localhost:12434/engines/v1") .modelName("ai/smollm2:360M-Q4_K_M") .build(); String answer = model.chat("Give me a fact about whales."); System.out.println(answer);
寻找更多模型
现在,你可能不只想使用 SmolLM 模型,所以你可能会想知道当前还有哪些其他模型可用于 Model Runner。最简单的入门方式是查看Docker Hub上的 ai/命名空间。