Skip to Content
PyTorch 中的 AI 和 ML 编码
book

PyTorch 中的 AI 和 ML 编码

by Laurence Moroney
July 2025
Beginner to intermediate
444 pages
6h 20m
Chinese
O'Reilly Media, Inc.
Content preview from PyTorch 中的 AI 和 ML 编码

第 17 章 用 Ollama 服务 LLMs

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

我们已经探讨了如何使用转换器下载模型,并建立一个简单的管道,让你可以使用它进行推理或微调。不过,如果我不向你展示开源的 Ollama 项目,那就太失职了,它通过为你提供一个环境,让你对 LLM 进行完整的封装,你既可以在终端上与它聊天,也可以将它用作服务器,通过 HTTP POST 连接到服务器并读取输出,从而将这一切联系在一起。

像 Ollama 这样的技术将成为下一代 LLMs 的先锋,它们将让你在数据中心内拥有专用服务器,或在计算机上拥有专用进程。这将使它们对你完全保密。

Ollama 的核心是一个开源项目,它简化了在电脑上下载、运行和管理 LLMs 的过程。它还能处理非功能性的困难需求,如内存管理和模型优化,并提供标准化的交互接口,如 HTTP POST 到模型的能力。

Ollama 还是您应该考虑的重要战略工具,因为它在基于云的第三方服务(如 GPT、Claude 和 Gemini)与本地部署的服务之间架起了一座桥梁。它不仅能为您提供本地开发环境,还能让您在自己的数据中心内为多个内部用户提供服务。

通过在本地运行模型,您可以确保数据的完全私密性,消除网络延迟,并实现离线工作。这在涉及敏感数据或需要一致、低延迟响应的应用场景中尤为重要。

Ollama 还支持越来越多的流行开源模型库,包括 Llama、Mistral 和 Gemma,它还支持针对特定任务进行优化的各种专用模型。每个模型都可以通过简单的命令调出并运行,其方式类似于 Docker 容器的工作方式。该平台可自动处理模型量化,优化模型以在消费级硬件上高效运行,同时保持良好的性能。

在本章中,我们将从三个方面探索 Ollama:安装它并开始使用;了解如何实例化特定模型并使用它们;探索让你构建 LLM 应用程序以保护隐私的 RESTful API。

开始使用 Ollama

Ollama 项目位于ollama.com。 上手运行非常简单,主屏幕上提供了 macOS、Linux 和 Windows 的下载选项。还要注意的是,Windows 版本需要 Windows Subsystem for Linux (WSL)。在本章中,我使用的是 macOS 版本。

导航到网站后,会看到友好的欢迎下载提示(见图 17-1)。

图 17-1. 开始使用 Ollama

下载并安装好 Ollama 后,就可以启动它了,你会在屏幕顶部的系统栏中看到它。Ollama 的主要界面是命令行。

通过ollama run 命令,你可以下载并使用模型。因此,举例来说,如果你想使用 Gemma,可以通过 Google 执行以下操作:

>ollama run gemma2:2b

你需要确保记下使用的参数,这些参数可以在Ollama 上的模型文档页面找到。虽然 Ollama 可以并将量化优化后的模型在本地运行,但它并不能创造奇迹,只有适合你的系统资源(最重要的是内存)的模型才能运行。在本例中,我运行的是gemma2:2b (20 亿参数)版本,需要约 8 GB 的 GPU 内存。在 macOS 上,与 M 系列芯片共享内存的效果很好,而在配备 16GB 内存的 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

在企业中实施 MLOps

在企业中实施 MLOps

Yaron Haviv, Noah Gift
图解大模型 : 生成式AI 原理与实战

图解大模型 : 生成式AI 原理与实战

Jay Alammar, Maarten Grootendorst

Publisher Resources

ISBN: 9798341662599