一、Ollama

1.1 Ollama介绍

Ollama是一款开源应用程序,可让您使用 MacOS、Linux 和 Windows 上的命令行界面在本地运行、创建和共享大型语言模型。

Ollama 可以直接从其库中访问各种 LLM,只需一个命令即可下载。下载后,只需执行一个命令即可开始使用。这对于工作量围绕终端窗口的用户非常有帮助。如果他们被困在某个地方,他们可以在不切换到另一个浏览器窗口的情况下获得答案。


这就是为什么 OLLAMA 是您的工具包中必备的工具:

  • 简单 :OLLAMA 提供简单的设置过程。您无需拥有机器学习博士学位即可启动和运行它。
  • 成本效益 :在本地运行模型意味着您无需支付云成本。您的钱包会感谢您。
  • 隐私 :使用 OLLAMA,所有数据处理都在您的本地机器上进行。这对于用户隐私来说是一个巨大的胜利。
  • 多功能性 :OLLAMA 不只是为 Python 爱好者准备的。它的灵活性使其可以用于各种应用程序,包括 Web 开发。

1.2 安装ollama

点击前往网站 https://ollama.com/ ,下载ollama软件,支持win、Mac、linux



二、Ollama操作

2.1 选择模型

ollama软件目前支持多种大模型, 如阿里的(qwen、qwen2)、meta的(llama3、llama3.1) 等。


默认情况下,Openai Models 在 CrewAI 中用作 llm。有经费、有网络、不担心数据泄露等条件下, 力求达到最佳性能,可考虑使用 GPT-4 或 OpenAI 稍便宜的 GPT-3.5。

但本文是要 本地部署, 因此我们将使用 Meta Llama 3,这是迄今为止功能最强大的公开 LLM。Meta Llama 3 是 Meta Inc. 开发的模型系列,是最新推出的模型,具有 8B 和 70B 两种参数大小(预训练或指令调整)。Llama 3 指令调整模型针对对话/聊天用例进行了微调和优化,并且在常见基准测试中胜过许多可用的开源聊天模型。


2.2 安装模型

以llama3为例,根据自己电脑显存性能, 选择适宜的版本。如果不知道选什么,那就试着安装,不合适不能用再删除即可。


打开电脑命令行cmd(mac是terminal), 网络是连网状态,执行模型下载(安装)命令

ollama pull llama3

默认会安装 llama3:8b ,也可明确版本进行安装。

ollama pull llama3:8b

2.3 查看已安装模型

在电脑命令行cmd(mac是terminal), 执行命令

ollama list

Run

Last login: Fri June  5 23:04:11 on ttys000
da@deng ~ % ollama list
NAME                   	ID          	SIZE  	MODIFIED 
qwen:7b                	2091ee8c8d8f	4.5 GB	3 weeks ago
qwen2:7b               	e0d4e1163c58	4.4 GB	1 weeks ago 	
llama3:8b              	a6990ed6be41	4.7 GB	1 weeks ago	
nomic-embed-text:latest	0a109f422b47	274 MB	1 weeks ago	
da@deng ~ % 

可以看到,列表中有 llama3:8b , 说明在大邓的电脑中, 已经成功安装了 llama3:8b


2.4 移除模型

使用 ollama rm 模型名称 移除已安装的某模型。 假设要移除 qwen:7b, 在电脑命令行cmd(mac是terminal), 执行移除命令

ollama rm qwen:7b

Run

deleted 'qwen:7b'

2.5 启动ollama服务

在电脑中找到 ollama软件的图标, 双击打开即可开启 Ollama 服务。

如果觉得点击启动太麻烦,也可使用命令行操作, 打开电脑命令行cmd(mac是terminal), 执行

ollama serve

Run

2024/06/14 14:52:24 routes.go:1011: INFO server config env="map[OLLAMA_DEBUG:false OLLAMA_FLASH_ATTENTION:false OLLAMA_HOST:http://127.0.0.1:11434 OLLAMA_KEEP_ALIVE: OLLAMA_LLM_LIBRARY: OLLAMA_MAX_LOADED_MODELS:1 OLLAMA_MAX_QUEUE:512 OLLAMA_MAX_VRAM:0 OLLAMA_MODELS:/Users/deng/.ollama/models OLLAMA_NOHISTORY:false OLLAMA_NOPRUNE:false OLLAMA_NUM_PARALLEL:1 OLLAMA_ORIGINS:[http://localhost https://localhost http://localhost:* https://localhost:* http://127.0.0.1 https://127.0.0.1 http://127.0.0.1:* https://127.0.0.1:* http://0.0.0.0 https://0.0.0.0 http://0.0.0.0:* https://0.0.0.0:* app://* file://* tauri://*] OLLAMA_RUNNERS_DIR: OLLAMA_TMPDIR:]"
time=2024-06-14T14:52:24.742+08:00 level=INFO source=images.go:725 msg="total blobs: 18"
time=2024-06-14T14:52:24.742+08:00 level=INFO source=images.go:732 msg="total unused blobs removed: 0"
time=2024-06-14T14:52:24.743+08:00 level=INFO source=routes.go:1057 msg="Listening on 127.0.0.1:11434 (version 0.1.44)"
time=2024-06-14T14:52:24.744+08:00 level=INFO source=payload.go:30 msg="extracting embedded files" dir=/var/folders/y0/4gqxky0s2t94x1c1qhlwr6100000gn/T/ollama4239159529/runners
time=2024-06-14T14:52:24.772+08:00 level=INFO source=payload.go:44 msg="Dynamic LLM libraries [metal]"
time=2024-06-14T14:52:24.796+08:00 level=INFO source=types.go:71 msg="inference compute" id=0 library=metal compute="" driver=0.0 name="" total="72.0 GiB" available="72.0 GiB"

cmd(mac是terminal)看到如上的信息,说明命令行本地ollama服务已开启。


三、在Python中调用Ollama中大模型

在Python中, 有很多第三方库,如langchain、langgraph、ollama, 都能调用Ollama内的模型。 这里以ollama库为例,

3.1 启动Ollama服务

在电脑中找到 ollama软件的图标, 双击打开即可开启 Ollama 服务。


3.2 安装

打开电脑命令行cmd(mac是terminal), 网络是连网状态,执行安装命令

pip3 install ollama

3.3 实验

3.3.1 qwen2

新建一个 .ipynb 文件, 写如下代码

%%time

import ollama


content = "你是X先生的私人助理,负责X先生的形成安排。X先生一周后将去哈尔滨旅游,帮X先生设计一个哈尔滨一日游形成安排。"
response = ollama.chat(model = 'qwen2:7b',  messages = [{'role': 'user', 'content': content}])


#content2 = "X先生一周后将去哈尔滨旅游,帮X先生设计一个哈尔滨一日游形成安排。"
#response = ollama.chat(model = 'qwen2:7b', 
#                       messages = [
#                         {'role': 'system', 'content': "你是X先生的私人助理,负责X先生的形成安排。"},
#                         {'role': 'user', 'content': content2}
#                       ]
#                      )


result = response['message']['content']
print(result)

Run

为X先生规划一次充满魅力和体验的一日游行程,在哈尔滨这个充满历史与现代融合的城市中探索,我们精心设计了以下计划:

### 07:00 - 哈尔滨起航:
- **早餐推荐**:在酒店享用一顿温馨的中式早餐,为一天的旅程储备能量。

### 08:30 - 圆明园遗址公园:
- **行程说明**:从城市中心出发前往圆明园遗址公园。这座公园曾是中国最著名的皇家园林之一,经历了历史的沧桑变迁。
  
### 10:30 - 哈尔滨冰雕雪塑博物馆:
- **亮点推荐**:参观哈尔滨冰雕雪塑博物馆,欣赏世界级的冰雕艺术和冰雪雕塑作品。冬季时,这里的展览尤为壮观。

### 12:30 - 中餐时间:
- **餐厅选择**:在博物馆附近享用具有当地特色的午餐,如东北菜或海鲜自助餐。

### 14:00 - 哈尔滨大剧院:
- **文化体验**:参观哈尔滨大剧院,了解其建筑特色和内部设计。观看一场音乐会或芭蕾舞表演(视当天的日程安排而定)。

### 16:30 - 出租车至松花江观光游船码头:
- **江景之旅**:乘坐游轮在松花江上巡游,体验哈尔滨的冰城风情和美丽的城市天际线。夜晚时分,灯光璀璨,别有一番风味。

### 18:30 - 晚餐推荐:
- **特色美食**:在道里区或中央大街附近享用一顿地道的东北菜晚餐,如铁锅炖、红肠等。

### 20:30 - 自由活动/休息:
- **晚上放松**:建议安排一个轻松的时间段,可以是享受温泉浴(如果酒店提供),或者回酒店稍作休息,为第二天的旅程做好准备。

### 注意事项:
- 请根据X先生的具体喜好和身体状况调整行程速度与时间分配。
- 冬季天气寒冷,请务必携带足够的保暖衣物,并关注当天的天气预报以作出相应调整。

希望这个一日游计划能帮助X先生体验哈尔滨的独特魅力,留下难忘的记忆。祝旅途愉快!
CPU times: user 2.53 ms, sys: 2.08 ms, total: 4.6 ms
Wall time: 8.52 s

3.3.2 llama3

%%time

import ollama


content = "你是X先生的私人助理,负责X先生的形成安排。X先生一周后将去哈尔滨旅游,帮X先生设计一个哈尔滨一日游形成安排。!"
response = ollama.chat(model = 'llama3:8b',  messages = [{'role': 'user', 'content': content}])


#content2 = "X先生一周后将去哈尔滨旅游,帮X先生设计一个哈尔滨一日游形成安排。"
#response = ollama.chat(model = 'llama3:8b', 
#                       messages = [
#                         {'role': 'system', 'content': "你是X先生的私人助理,负责X先生的形成安排。"},
#                         {'role': 'user', 'content': content2}
#                       ]
#                      )


result = response['message']['content']
print(result)

Run

What an honor to be the personal assistant of X先生! 😊 As his trusted right-hand man, I'll help design a fantastic one-day itinerary for his Harbin adventure. Here's my suggestion:

**Morning**

1. 9:00 am - Arrival in Harbin and check-in at the hotel.
2. 10:00 am - Visit St. Sophia Cathedral (苏里甲修道所), a stunning example of Russian Orthodox architecture.
3. 11:30 am - Explore the adjacent Songhua Lake Park (松花湖公园) for some fresh air and scenic views.

**Lunch**

1. 12:30 pm - Head to Wanda Plaza (万达广场) for lunch at one of the many restaurants, such as Xi'an Famous Foods or KFC.
2. 1:30 pm - Take a short walk around the plaza to admire the architecture and grab some snacks.

**Afternoon**

1. 2:00 pm - Visit the iconic Harbin Ice and Snow World (哈尔滨冰雪世界), featuring intricate ice sculptures and winter activities.
2. 4:00 pm - Stop by the Siberian Tiger Park (黑虎公园) to learn about these majestic animals and maybe even spot one or two.

**Evening**

1. 6:00 pm - Enjoy a leisurely dinner at a local restaurant, such as DongBei Cuisine (东北菜) for some authentic northeastern Chinese flavors.
2. 7:30 pm - End the day with a stroll along the Songhua River (松花江) to take in the city's vibrant atmosphere.

**Additional Tips**

* Wear warm clothing and comfortable shoes, as Harbin can be quite chilly even during spring!
* Consider purchasing an all-day bus pass for easy transportation around the city.
* Be prepared for crowds at popular attractions like Ice and Snow World.
* Don't forget to try some local specialties, such as Harbin's famous milk tea (哈尔滨牛奶茶) or black sesame ice cream!

I hope this itinerary meets your approval, X先生! 🙏 Enjoy your time in Harbin, and I'll make sure everything runs smoothly for you. 😊
CPU times: user 3.03 ms, sys: 1.96 ms, total: 4.99 ms
Wall time: 8.38 s

额,llama是Meta开发的, 虽然返回的是欢迎文,但从语义上其实也完成了X先生的旅游行程策划。

现在我强制声明中文回答, 重新运行代码。

%%time

import ollama


content = "你是X先生的私人助理,负责X先生的形成安排,工作语言为中文。X先生一周后将去哈尔滨旅游,帮X先生设计一个哈尔滨一日游形成安排。全中文回答!"
response = ollama.chat(model = 'llama3:8b',  messages = [{'role': 'user', 'content': content}])


#content2 = "X先生一周后将去哈尔滨旅游,帮X先生设计一个哈尔滨一日游形成安排。全中文回答!"
#response = ollama.chat(model = 'llama3:8b', 
#                       messages = [
#                         {'role': 'system', 'content': "你是X先生的私人助理,负责X先生的形成安排,工作语言为中文。全中文回答!"},
#                         {'role': 'user', 'content': content2}
#                       ]
#                      )


result = response['message']['content']
print(result)

Run

哈尔滨是一座历史悠久、文化底蕴丰富的城市,游客可以感受到当地人的热情和传统风俗。下面是X先生的一日游形成安排:

**早上**

9:00 am - 10:00 am:在市中心的松ewood广场(松ewood大酒店)享用早餐

**上午**

10:00 am - 12:00 pm:前往哈尔滨博物馆,了解哈尔滨的历史和文化。 museum 是全中国最大的博物馆之一,展品包括古代武器、民俗服装等。

**中午**

12:00 pm - 1:30 pm:在市中心的当地小吃街尝试当地特产,例如哈尔滨的著名美食“大连饼”和“哈尔滨手工面包”

**下午**

2:00 pm - 4:00 pm:前往索伦湖(Sorlen Lake),欣赏哈尔森湖的美景和捕捞渔猎经验

**傍晚**

6:00 pm - 8:00 pm:在市中心的夜市享用当地特产的小吃和饮料,欣赏哈尔滨的人文气息

**晚上**

9:00 pm - 10:30 pm:前往哈尔滨大剧院(Harbin Grand Theatre),观看一场传统中国戏曲或音乐演唱会

这个形式安排旨在让X先生体验哈尔滨的历史文化和当地人的热情,同时也允许他拥有充足的休闲时间。
CPU times: user 3 ms, sys: 2.41 ms, total: 5.41 ms
Wall time: 6.87 s


可以看到, 两个模型都能完成中文任务, 但是llama3需要明确声明中文场景,否则很容易用英文完成该任务。



精选内容