零基础入门AI:一键本地运行各种开源大语言模型 - Ollama

什么是 Ollama?

Ollama 是一个可以在本地部署和管理开源大语言模型的框架,由于它极大的简化了开源大语言模型的安装和配置细节,一经推出就广受好评,目前已在github上获得了46k star。

不管是著名的羊驼系列,还是最新的AI新贵Mistral,等等各种开源大语言模型,都可以用Ollama实现一键安装并运行,支持的更多模型的列表可以查看Ollama官网。

Model Parameters Size Download
Llama 2 7B 3.8GB ollama run llama2
Mistral 7B 4.1GB ollama run mistral

本文就让我们一起入门Ollama。

如何安装 Ollama框架?

Ollama支持各个平台:Mac、Windows 和 Linux,也提供了docker image。 在Ollama官网或者Github可以下载,然后一键安装Ollama框架:

  • macOS
  • Windows
  • Linux: curl -fsSL https://ollama.com/install.sh | sh

由于Ollama刚支持windows不久,在windows上的相关配置还不够完善,以下我将主要以Linux上运行Ollama来举例说明。

运行 Ollama 服务

在Ollama安装完成后, 一般会自动启动 Ollama 服务,而且会自动设置为开机自启动。安装完成后,可以使用如下命令查看是否Ollama是否正常启动。如下例子中显示"Active: active (running)"表示Ollama已经正常启动。

yaml 复制代码
$ systemctl status ollama
● ollama.service - Ollama Service
     Loaded: loaded (/etc/systemd/system/ollama.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/ollama.service.d
             └─environment.conf
     Active: active (running) since Thu 2024-03-07 09:09:39 HKT; 4 days ago
   Main PID: 19975 (ollama)
      Tasks: 29 (limit: 69456)
     Memory: 1.1G
        CPU: 14min 44.702s
     CGroup: /system.slice/ollama.service
             └─19975 /usr/local/bin/ollama serve

在Linux上,如果Ollama未启动,可以用如下命令启动 Ollama 服务:ollama serve,或者 sudo systemctl start ollama

通过分析Linux的安装脚本install.sh,就会看到其中已经将ollama serve配置为一个系统服务,所以可以使用systemctl来 start / stop ollama进程。

ini 复制代码
    status "Creating ollama systemd service..."
    cat <<EOF | $SUDO tee /etc/systemd/system/ollama.service >/dev/null
[Unit]
Description=Ollama Service
After=network-online.target

[Service]
ExecStart=$BINDIR/ollama serve
User=ollama
Group=ollama
Restart=always
RestartSec=3
Environment="PATH=$PATH"

启动Ollama服务后,可以查看当前的Ollama版本,以及常用命令

sql 复制代码
~$ ollama -v
ollama version is 0.1.20
~$ ollama --help
Large language model runner

Usage:
  ollama [flags]
  ollama [command]

Available Commands:
  serve       Start ollama
  create      Create a model from a Modelfile
  show        Show information for a model
  run         Run a model
  pull        Pull a model from a registry
  push        Push a model to a registry
  list        List models
  cp          Copy a model
  rm          Remove a model
  help        Help about any command

Flags:
  -h, --help      help for ollama
  -v, --version   Show version information

Use "ollama [command] --help" for more information about a command.

如何下载并运行大语言模型?

至此,已经完成Ollama框架的安装,接下来,可以用一条命令在本地运行大语言模型。以著名的羊驼举例:ollama run llama2

如果还没有下载过指定的大语言模型,这条命令将会先执行ollama pull llama2,将大语言模型下载到本地,再在本地运行大语言模型。

下载完成后,运行效果如下:

less 复制代码
:~$ ollama run llama2
>>> who are you?

I am LLaMA, an AI assistant developed by Meta AI that can understand and respond to human input in a conversational manner. I am trained on a massive dataset of text from the internet and can 
generate human-like responses to a wide range of topics and questions. I can be used to create chatbots, virtual assistants, and other applications that require natural language understanding and
generation capabilities.

>>> Send a message (/? for help)

REST API

Ollama还提供了API接口:

vbnet 复制代码
curl http://localhost:11434/api/generate -d '{
  "model": "llama2",
  "prompt":"Why is the sky blue?",
  "stream": false
}'

返回结果如下:

json 复制代码
{
    "model": "llama2",
    "created_at": "2024-02-26T04:35:10.787352404Z",
    "response": "The sky appears blue because of a phenomenon called Rayleigh scattering, which occurs when sunlight enters Earth's atmosphere. The sunlight encounters tiny molecules of gases such as nitrogen and oxygen, which scatter the light in all directions. The shorter wavelengths of light, such as blue and violet, are scattered more than the longer wavelengths, such as red and orange. This is known as Rayleigh scattering, named after Lord Rayleigh, who first described the phenomenon in the late 19th century. As a result of this scattering, the light that reaches our eyes from the sun appears blue, especially when viewed from a distance. The closer we get to the horizon, the more the blue color appears to fade, as the light has to travel through more of the atmosphere, which scatters the shorter wavelengths even more. It's worth noting that the exact shade of blue can vary depending on the time of day and atmospheric conditions. For example, during sunrise and sunset, when the sun is low in the sky, the sky can take on a more orange or red hue due to the scattering of light by atmospheric particles. So, to summarize, the sky appears blue because of the way light interacts with the tiny molecules of gases in Earth's atmosphere, particularly nitrogen and oxygen.",
    "done": true,
    "total_duration": 7001870820,
    "load_duration": 4930376,
    "prompt_eval_duration": 60907000,
    "eval_count": 309,
    "eval_duration": 6931593000
}

使用API接口,就可以实现更多灵活的功能,比如与IDE插件配合,实现本地的编程助手,可参考如下文章: 零基础入门AI:搭建本地的编程助手

FAQ

如何查看运行的日志?

在Linux上运行命令journalctl -u ollama,即可查看运行日志。

如何配置本地大模型对局域网提供服务?

在Linux上创建如下配置文件,并配置环境变量 OLLAMA_HOST 来指定对局域网提供服务的地址,再重启Ollama服务即可。

ini 复制代码
:~$ cat /etc/systemd/system/ollama.service.d/environment.conf
[Service]
Environment=OLLAMA_HOST=0.0.0.0:11434

如此配置后,即可由一台GPU服务器为本地局域网提供大语言模型的服务。

本地有多张GPU,如何用指定的GPU来运行Ollama?

在Linux上创建如下配置文件,并配置环境变量 CUDA_VISIBLE_DEVICES 来指定运行Ollama的GPU,再重启Ollama服务即可。

ini 复制代码
:~$ cat /etc/systemd/system/ollama.service.d/environment.conf
[Service]
Environment=CUDA_VISIBLE_DEVICES=1,2

下载的大模型存储在哪个路径?

默认情况下,不同操作系统存储的路径如下:

  • macOS: ~/.ollama/models
  • Linux: /usr/share/ollama/.ollama/models
  • Windows: C:\Users<username>.ollama\models

如何修改大模型存储的路径?

Linux平台安装Ollama时,默认安装时会创建用户ollama,再将模型文件存储到该用户的目录/usr/share/ollama/.ollama/models。但由于大模型文件往往特别大,有时需要将大模型文件存储到专门的数据盘,此时就需要修改大模型文件的存储路径。

官方提供的方法是设置环境变量"OLLAMA_MODELS",但我在Linux上尝试后,并没有成功。

分析Linux版的安装脚本install.sh后,我发现是由于其中创建了用户ollama和用户组ollama,然后将大模型存储到了该用户的目录/usr/share/ollama/.ollama/models,而我的帐户对ollama帐户的一些操作并不能生效,即使我再手动将我的帐户添加进ollama用户组,也仍然会有一些权限问题,导致对ollama帐户的目录操作不生效。

由于新建的ollama帐户并没有给我带来额外的便利,最后我用以下步骤来实现修改大模型文件的存储路径:

  1. 修改安装文件 install.sh,取消其中创建用户ollama的步骤,参考如下:

    shell 复制代码
    # if ! id ollama >/dev/null 2>&1; then 
        # status "Creating ollama user..." 
        # $SUDO useradd -r -s /bin/false -m -d /usr/share/ollama ollama 
    # fi 
    # status "Adding current user to ollama group..." 
    # $SUDO usermod -a -G ollama $(whoami)
  2. 修改安装文件 install.sh,使用我的帐户来启动ollama服务,参考如下:

    ini 复制代码
        status "Creating ollama systemd service..."
        cat <<EOF | $SUDO tee /etc/systemd/system/ollama.service >/dev/null
    [Unit]
    Description=Ollama Service
    After=network-online.target
    
    [Service]
    ExecStart=$BINDIR/ollama serve
    User=<myusername>
    Group=<myusername>
  3. 修改安装文件 install.sh,添加如下配置中指定环境变量OLLAMA_MODELS指定存储路径,再用此安装文件来安装ollama。

    ini 复制代码
    Environment="OLLAMA_MODELS=/home/paco/lab/LLM/ollama/OLLAMA_MODELS"

    或者在安装完成后,创建如下配置文件,并配置环境变量OLLAMA_MODELS来指定存储路径,再重启Ollama服务。

    ini 复制代码
    :~$ cat /etc/systemd/system/ollama.service.d/environment.conf
    [Service]
    Environment=OLLAMA_MODELS=<path>/OLLAMA_MODELS
相关推荐
顾北121 小时前
MCP协议实战|Spring AI + 高德地图工具集成教程
人工智能
wfeqhfxz25887822 小时前
毒蝇伞品种识别与分类_Centernet模型优化实战
人工智能·分类·数据挖掘
中杯可乐多加冰2 小时前
RAG 深度实践系列(七):从“能用”到“好用”——RAG 系统优化与效果评估
人工智能·大模型·llm·大语言模型·rag·检索增强生成
珠海西格电力科技2 小时前
微电网系统架构设计:并网/孤岛双模式运行与控制策略
网络·人工智能·物联网·系统架构·云计算·智慧城市
FreeBuf_2 小时前
AI扩大攻击面,大国博弈引发安全新挑战
人工智能·安全·chatgpt
weisian1513 小时前
进阶篇-8-数学篇-7--特征值与特征向量:AI特征提取的核心逻辑
人工智能·pca·特征值·特征向量·降维
Java程序员 拥抱ai3 小时前
撰写「从0到1构建下一代游戏AI客服」系列技术博客的初衷
人工智能
186******205313 小时前
AI重构项目开发全流程:效率革命与实践指南
人工智能·重构
森之鸟4 小时前
多智能体系统开发入门:用鸿蒙实现设备间的AI协同决策
人工智能·harmonyos·m
铁蛋AI编程实战4 小时前
大模型本地轻量化微调+端侧部署实战(免高端GPU/16G PC可运行)
人工智能·架构·开源