1. ollama大模型本地使用指南
1.1 安装并拉起ollama服务
#安装spacemit-ollama-toolkit
sudo apt update
sudo apt install spacemit-ollama-toolkit
#拉起ollama服务
ollama serve
1.2 下载模型文件
模型仓库地址:
https://archive.spacemit.com/spacemit-ai/ModelZoo/gguf/
#仓库提供3种qwen模型:qwen2.5:0.5b,qwen2.5:1.5b以及qwen2.5:3b
#通过wget拉取模型到当前文件夹(下方为0.5b大模型)
wget https://archive.spacemit.com/spacemit-ai/ModelZoo/gguf/qwen2.5-0.5b-q4_0_16_8.gguf
#1.5b大模型拉取方式:
#wget https://archive.spacemit.com/spacemit-ai/ModelZoo/gguf/qwen2.5-1.5b-q4_0_16_8.gguf
#3b大模型拉取方式:
#wget https://archive.spacemit.com/spacemit-ai/ModelZoo/gguf/qwen2.5-3b-q4_0_16_8.gguf
1.3 准备modelfile
模型配置文件
ollama
需要创建modelfile
文件来配置模型。
modelfile示例:
# the gguf models must be set to q4_0_16_8
FROM ${model_path}
# set the temperature to 1 [higher is more creative, lower is more coherent]
PARAMETER temperature 0.7
PARAMETER top_p 0.8
PARAMETER repeat_penalty 1.05
PARAMETER top_k 20
TEMPLATE """{{ if .Messages }}
{{- if or .System .Tools }}<|im_start|>system
{{ .System }}
{{- if .Tools }}
# Tools
You are provided with function signatures within <tools></tools> XML tags:
<tools>{{- range .Tools }}
{"type": "function", "function": {{ .Function }}}{{- end }}
</tools>
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{"name": <function-name>, "arguments": <args-json-object>}
</tool_call>
{{- end }}<|im_end|>
{{ end }}
{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1 -}}
{{- if eq .Role "user" }}<|im_start|>user
{{ .Content }}<|im_end|>
{{ else if eq .Role "assistant" }}<|im_start|>assistant
{{ if .Content }}{{ .Content }}
{{- else if .ToolCalls }}<tool_call>
{{ range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}}
{{ end }}</tool_call>
{{- end }}{{ if not $last }}<|im_end|>
{{ end }}
{{- else if eq .Role "tool" }}<|im_start|>user
<tool_response>
{{ .Content }}
</tool_response><|im_end|>
{{ end }}
{{- if and (ne .Role "assistant") $last }}<|im_start|>assistant
{{ end }}
{{- end }}
{{- else }}
{{- if .System }}<|im_start|>system
{{ .System }}<|im_end|>
{{ end }}{{ if .Prompt }}<|im_start|>user
{{ .Prompt }}<|im_end|>
{{ end }}<|im_start|>assistant
{{ end }}{{ .Response }}{{ if .Response }}<|im_end|>{{ end }}"""
# set the system message
SYSTEM """You are Qwen, created by Alibaba Cloud. You are a helpful assistant."""
# The number of threads must be set to 4
PARAMETER num_thread 4
只需要改动modelfile
文件中的${model_path}
即可,把${model_path}
改成第2点里面存放模型的路径即可。
1.4 使用ollama创建模型
#在modelfile路径下执行以创建ollama格式的模型,下方qwen2.5:0.5b名字可以随意取
ollama create qwen2.5:0.5b -f modelfile
#ollama list可以查看模型
#使用ollama run qwen2.5:0.5b运行大模型
ollama run qwen2.5:0.5b
2. ollama python API使用指南
Ollama官方提供的有多种语言版本的 API ,这里主要介绍 ollama-python 的 API。
使用 ollama-python 前要执行以下命令安装:
pip install ollama
#调用接口之前需要先启动ollama服务
ollama serve
ollama 服务端启动后,python应用调用 ollama-python 提供的RESTful API就可以与 ollama 服务端通信,与命令行执行 ollama 命令的效果一致。
ollama-python 提供的接口详情请看链接:Ollama Python Library