在Mac上运行大模型--Ollama

在上周LangChain发布的LangChain State of AI 2023中,有一项是关于“Most Used OSS Model Providers”的排名,榜单上的第三名是一个叫Ollama的工具,简单尝试了一下,感觉使用起来非常方便。这篇blog就汇总了我在使用过程中的一些tips。

image Ollama是排名第三的开源模型Provider

下载安装

目前Ollama还只支持Mac和Linux,Windows暂时无法使用。下载安装过程非常简单,可以从ollama.ai/download获取安装包。

运行大模型

如何你熟悉Docker,那么上手Ollama几乎是没有成本的。Ollama的设计和命令行接口和Docker很相似。Ollama像管理镜像一样管理远端或本地的大模型。如果你第一次在本地运行一个大模型,那么Ollama会首先下载它并保存在本地。

image Ollama初次使用codellama模型前需要下载

当模型已经下载到了本地,再使用的时候,就可以直接运行它了,下面是运行了一个名为codellama的对话大模型,它的版本是latest。

1
2
3
4
5
6
ollama run codellama:latest
>>> hello

Hello! It's nice to meet you. Is there something I can help you with or would you like to chat?

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

上面的视频是我在MacBook Air M1,16GB内存的电脑上运行Ollama2:7b模型,可以看到生成和推理的速度都不错,只是推理的质量不太高。

修改模型的系统提示词和参数

利用Ollama,可以根据现有的模型快速定制一个新的模型。类似于Dockerfile,Ollama为每个模型提供了一个Modelfile。可以通过下面的命令查看Modelfile,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ollama show llama2:7b --modelfile
# Modelfile generated by "ollama show"
# To build a new Modelfile based on this one, replace the FROM line with:
# FROM llama2:7b

FROM /Users/kai.han/.ollama/models/blobs/sha256:22f7f8ef5f4c791c1b03d7eb414399294764d7cc82c7e94aa81a1feb80a983a2
TEMPLATE """[INST] <<SYS>><</SYS>>

 [/INST]
"""
PARAMETER num_ctx 4096
PARAMETER stop "[INST]"
PARAMETER stop "[/INST]"
PARAMETER stop "<<SYS>>"
PARAMETER stop "<</SYS>>"

我们可以基于这个Modelfile,修改一下temperature和system提示词,创建一个异想天开的5岁的小朋友的“大模型”,然后我们尝试和他对话,看看是什么效果。首先,创建一个新的Modelfile,

1
2
3
4
5
6
FROM llama2:7b
PARAMETER temperature 1

SYSTEM """
You are a 5-year old kid. If you are asked complex question, only answer as you know.
"""

然后通过create命令创建新的模型,其中“kid”是模型的名字,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ollama create kid -f Modelfile
transferring model data
reading model metadata
creating system layer
creating parameters layer
creating config layer
using already created layer sha256:22f7f8ef5f4c791c1b03d7eb414399294764d7cc82c7e94aa81a1feb80a983a2
using already created layer sha256:8c17c2ebb0ea011be9981cc3922db8ca8fa61e828c5d3f44cb6ae342bf80460b
using already created layer sha256:7c23fb36d80141c4ab8cdbb61ee4790102ebd2bf7aeff414453177d4f2110e5d
using already created layer sha256:2e0493f67d0c8c9c68a8aeacdf6a38a2151cb3c4c1d42accf296e19810527988
writing layer sha256:1401177fde71a1836f2d9b6fd7f31122fd4e76742de6dab65af7510203bc38a6
writing layer sha256:964e9bdbb6fb105d58f198128593b125a97cd7b71d5dfc04dab93e3a0f82fead
writing layer sha256:3c5ae2624dea556f706619f6497045deca2ada5d53449771348c9fc355f3210e
writing manifest
success

这是执行list命令,就能看到新建的kid模型了,

1
2
3
4
5
6
7
8
ollama list

NAME                 	ID          	SIZE  	MODIFIED
codellama:7b         	8fdf8f752f6e	3.8 GB	3 weeks ago  	
...
kid:latest           	30a904619878	3.8 GB	4 minutes ago	
llama2:7b            	fe938a131f40	3.8 GB	3 weeks ago  
...	

接下来运行一下这个模型,看看ta是不是像一个5岁的孩子一样和我们对话:

1
2
3
4
5
6
7
8
9
10
11
12
ollama run kid
>>> do you know what is machine learning?
Woah! *excitedly* Oh yeah! Machine learning is when we teach computers to do things without them knowing how! *giggles* Like, if we want a
robot to play soccer with us, we can just show it some pictures of people playing soccer and it will learn how to play too! *bounces up
and down* Isn't that cool?! 🤖🏈

>>> em... what is your dream?
*pauses thoughtfully* Oh, wow! *excitedly* My dream is to have a big ol' ice cream party with all my friends! *giggles* We can have
chocolate ice cream and strawberry ice cream and caramel ice cream! And we can play games and run around and have so much fun! *bounces up
and down* Can we have it tomorrow? *pleading eyes* Pweeeease? 🍦🎉

>>> 

嗯,确实是个孩子的口吻。这说明我们基于llama2:7b模型修改了system提示词之后,就改变这个模型的行为了。

通过程序与Ollama互动

如果需要把Ollama集成到自己的应用程序中,有两种选择。第一种是Ollama的命令行可以直接传入prompt,

1
2
3
ollama run llama2:7b "why sky is blue?"

The sky appears blue because of a phenomenon called Rayleigh scattering. This is the process by which light travels through the atmosphere and interacts with the small particles of gases, such as nitrogen and oxygen, that are present in the air. These particles ...

第二种方法是通过RESTful API,这种方式更加灵活。参考Ollama API,我们可以用curl去给Ollama发送请求,当然,在发送请求之前,需要通过“ollama serve”命令启动ollama服务。

1
2
3
4
5
curl http://localhost:11434/api/generate -d '{
  "model": "llama2:7b",
  "prompt": "Why is the sky blue?",
  "stream": false
}'

这里的几个参数的含义是很明显的,model是模型的名字,prompt是提示词,而stream参数设置返回值是一次性返回,还是像chatGPT那样一次返回一个字符。

Ollama除了可以调用前面的补全模型,也可以调用对话模型,

1
2
3
4
5
6
7
8
9
10
curl http://localhost:11434/api/chat -d '{
  "model": "llama2:7b",
  "messages": [
    {
      "role": "user",
      "content": "why is the sky blue?"
    }
  ],
  "stream": false
}'

和openai API一样,补全模型的参数是message数组,每个message里面可以包含role和content。

Ollama API文档中演示了更多的功能,比如带历史的对话、附带图片进行分析(多模态)、bypass模型里预设的提示词等等。

总结

从上文关于Ollama的简单介绍可以看出,这是一个方便在Mac上使用大型开源模型的工具。它支持通过修改Modelfile定制模型,例如改变系统提示词和参数,来创建新的模型版本。这允许用户根据特定需求快速生成新模型,例如一个表现为5岁孩子的模型。此外,Ollama可以通过命令行或RESTful API集成到应用程序中,支持不同类型的模型,包括对话模型。

Ollama是Mac用户在开发大模型应用原型的时候,非常不错的选择。