ollama-大模型的部署与使用
linux
curl -fsSL https://ollama.com/install.sh | sh
因为安装过程有使用github相关资源,国内访问会有问题,需要加速。或者使用docker进行安装。各大云平台都有提供docker加速方法。
ollama的所有命令
Available Commands:
serve Start ollama
create Create a model from a Modelfile
show Show information for a model
run Run a model
stop Stop a running model
pull Pull a model from a registry
push Push a model to a registry
list List models
ps List running models
cp Copy a model
rm Remove a model
help Help about any command
# 查看给定模型的模型文件
ollama show --modelfile llama3.2
python中使用
在python中使用的时候应该先保证服务器中启动了ollama serve,否则会报如下错误:
ConnectionError: Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download
安装方式
pip install ollama
使用方式
from ollama import chat
from ollama import ChatResponse
response: ChatResponse = chat(model='llama3.2', messages=[
{
'role': 'user',
'content': 'Why is the sky blue?',
},
])
print(response['message']['content'])
# or access fields directly from the response object
print(response.message.content)
Model File的指令
使用方式:
创建模型文件
./Modelfile
使用以下命令创建模型:
ollama create choose-a-model-name -f ./Modelfile
运行模型:
ollama run choose-a-model-name
PARAMETER
TEMPLATE
TEMPLATE 是传递到模型中的完整提示模板。它可以包括(可选)系统消息、用户消息以及模型的响应。注意:语法可能因模型而异。模板使用 Go 模板语法。
模板变量:
示例:
TEMPLATE """{{ if .System }}<|im_start|>system
{{ .System }}<|im_end|>
{{ end }}{{ if .Prompt }}<|im_start|>user
{{ .Prompt }}<|im_end|>
{{ end }}<|im_start|>assistant
"""
使用modelscope加速模型的下载
搜索对应的模型,比如qwen,找到想要使用的模型,复制名称,前边添加
modelscope.cn/
使用
ollama run modelscope.cn/Qwen/Qwen3-8B-GGUF
其他问题
部分模型的运行需要更新大ollama的新版
License:
CC BY 4.0