ollama 是一个用于部署和管理 LLMs 的工具,它可以帮助用户轻松地将复杂的模型部署到生产环境中。本文将介绍在 Linux 系统下如何使用 ollama 快速部署 InternLM2.5。
https://github.com/InternLM/InternLM
1.环境配置
ollama 安装
使用如下 shell 脚本下载并安装 ollama。脚本执行成功后,可以在终端执行 ollama --version, 输出 ollama 版本号说明已成功安装。
curl -fsSL https://ollama.com/install.sh | sh
服务配置
ollama 安装成功后默认会开启服务。如果未启动服务,可以执行 ollama serve 启动服务。如果要修改服务端口号,或者 GPU 运行设备等配置,则需要修改 ollama 服务配置文件并重启服务。可执行 sudo systemctl edit ollama.service 修改服务配置,参考内容如下:
[Service]
Environment="OLLAMA_HOST=0.0.0.0:11434"
Environment="CUDA_VISIBLE_DEVICES=1,2"
然后执行如下命令重启 ollama 服务:
sudo systemctl daemon-reload
sudo systemctl restart ollama
2.模型准备
模型下载
从 ollama 模型库拉取 InternLM2.5 模型,默认 latest 标签是 internlm2_5-7b-chat 模型。
# 拉取internlm2_5-7b-chat模型
ollama pull internlm/internlm2.5
# 拉取 internlm2_5-7b-chat-1m模型
ollama pull internlm/internlm2.5:7b-chat-1m
模型下载完成后执行 ollama list 列出本地已有模型。
NAME ID SIZE MODIFIED
internlm/internlm2.5:latest 5d4be058b1fb 15 GB 3 hours ago
执行 ollama show internlm/internlm2.5 显示模型参数量,精度类型,对话模板等信息。
Model
arch internlm2
parameters 7.7B
quantization F16
context length 32768
embedding length 4096
Parameters
stop "<|im_end|>"
System
You are an AI assistant whose name is InternLM (书生·浦语).
- InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.
模型创建
查看模型信息后,我们可以基于该模型创建自己的模型,并修改对话模版,采样等相关参数。创建新模型也非常简单,先创建如下 Modelfile 进行模型配置。更多可配置参数请参考官方文档(https://github.com/ollama/ollama/blob/main/docs/modelfile.md#instructions)。
FROM internlm/internlm2.5
PARAMETER temperature 0.8
PARAMETER top_p 0.8
PARAMETER top_k 50
# 最大输出长度
PARAMETER num_predict 512
PARAMETER num_ctx 4096
PARAMETER seed 1234
SYSTEM """You are InternLM2-Chat, a harmless AI assistant."""
然后执行 create 命令创建新模型,模型名称我们设定为 mymodel。
ollama create mymodel -f ./Modelfile
3.模型部署
模型运行
本地模型准备好后,执行 ollama run mymodel 就可轻松将本地模型部署,并在终端进行对话。对话结束后按 ctrl + d 可终止模型运行。以下是运行示例:
>>> 你好
您好!请问有什么我可以帮您的?
>>> 你是谁?
我是由上海人工智能实验室开发的人工智能助手,名为InternLM2。我的主要功能是帮助用户解答问题、提供信息以及执行一些简单的任务。
>>> 你会唱歌吗?
作为一个人工智能,我没有实际的唱歌能力。不过我可以帮你找到歌曲的歌词、音乐视频或者相关的歌手信息等。请问有什么具体的歌曲或歌手需要我帮忙查
找的吗?
>>>
服务调用
ollama 也提供模型服务,服务默认是启动的。这里我们使用 curl 调用 /api/generate 接口,示例如下:
curl http://localhost:11434/api/generate -d '{
"model": "mymodel",
"prompt": "1+0等于几?",
"stream": false
}'
generate 接口返回结果是 json 字符串,可以将其拷贝至在线查看网站 jsonviewer(https://jsonviewer.stack.hu/)进行查看。如下所示,返回结果包含模型名称,模型响应内容等信息。其中 context 是对话历史的 token_ids 序列,可在下次请求中使用以实现对话历史记忆功能。
工具调用示例
此外,也可编写 Python 脚本调用服务。下面展示了如何替换模型默认对话模板参数实现天气查询的工具调用功能。我们使用 requests 库进行两次服务调用,并输出模型响应。
import json
import requests
api_url = 'http://localhost:11434/api/generate'
# 需要调用的函数工具
functions = [{
'name': 'get_current_weather',
'description': 'Get the current weather in a given location',
'parameters': {
'type': 'object',
'properties': {
'location': {
'type': 'string',
'description': 'The city and state, e.g. San Francisco, CA',
},
'unit': {
'type': 'string'
},
},
'required': ['location'],
},
}]
custom_system = f"""You are InternLM2-Chat, a harmless AI assistant.<|im_end|>
<|im_start|>system name=<|plugin|>
{json.dumps(functions, ensure_ascii=False)}
"""
#自定义对话模板
custom_template = """{{ if .System }}<|im_start|>system
{{ .System }}<|im_end|>
{{ end }}{{ if .Prompt }}<|im_start|>{{ .Prompt }}<|im_end|>
{{ end }}<|im_start|>assistant
{{ .Response }}<|im_end|>
"""
# 发送请求数据
data = {
'model': 'mymodel',
'prompt': 'user\nI want to know today\'s weather in Shanghai',
'system': custom_system,
'stream': False,
'template': custom_template
}
headers = {'Content-Type': 'application/json'}
# 第一次请求询问天气
response = requests.post(api_url, data=json.dumps(data), headers=headers)
assert response.status_code == 200
json_response = json.loads(response.text)
# 记录对话历史
context = json_response['context']
print(json_response['response'])
# 更新prompt和context
data.update({
'context': context,
'prompt': 'environment name=<|plugin|>\n{"temperature": 22}',
})
# 第二次请求发送参数信息
response = requests.post(api_url, data=json.dumps(data), headers=headers)
assert response.status_code == 200
json_response = json.loads(response.text)
print(json_response['response'])
执行 Python 脚本,输出如下。可以看出 <|action_start|><|plugin|>和<|action_end|> 之间是我们想要的工具调用信息,同时模型也正确的输出了工具调用结果,即上海的天气温度。
To fulfill the user's request for today's weather in Shanghai, I will call the \"get_current_weather\" function with the argument specifying the location as 'Shanghai'. This API call is necessary because it retrieves real-time weather data specifically for Shanghai. By using this information, I can provide the user with accurate and up-to-date weather details regarding temperature, humidity, wind speed, etc., helping them plan their day accordingly.<|action_start|><|plugin|>
{"name": "get_current_weather", "parameters": {"location": "Shanghai"}}<|action_end|>
Today's weather in Shanghai is currently at a temperature of 22 degrees Celsius (72 degrees F
4.模型发布
当验证模型功能正确之后,可以将新创建的模型上传到 ollama 模型库,以便于其他人下载和使用你的模型。模型发布也很简单,首先登录 ollama 官网(https://ollama.com/signup) 注册并登录账户,然后在用户设置页面添加 ollama 秘钥。公钥存放位置为 /usr/share/ollama/.ollama/id_ed25519.pub。
最后将模型重命名为 用户名/模型名称:标签 格式,执行上传操作。模型上传成功后,可登录 ollama 用户模型页面,查看和编辑模型卡片信息。# 复制模型,设置模型名称
ollama cp mymodel myusername/mymodel:latest
# 上传模型
ollama push myusername/mymodel:latest
# 删除本地无用模型
ollama rm mymodel