Ollama常见命令
📌 安装 Ollama
curl -fsSL https://ollama.com/install.sh | sh
或参考 Ollama 官网 下载安装。
🚀 模型管理
1. 查看已下载的模型
ollama list
例如:
ollama pull mistral
3. 删除本地模型
ollama rm <model_name>
4. 查看模型详情
ollama show <model_name>
💬 运行模型
1. 交互式对话
ollama run <model_name>
ollama run mistral
然后输入问题进行对话。
2. 直接运行命令
ollama run <model_name> "你的问题"ollama run mistral "什么是机器学习?"
🛠 自定义模型
1. 创建 Modelfile
FROM mistral
然后执行:
ollama create mymodel -f Modelfile
2. 运行自定义模型
ollama run mymodel
🌐 API 调用
1. 运行 API 服务器
ollama serve
2. 通过 cURL
访问
curl http://localhost:11434/api/generate -d '{ "model": "mistral", "prompt": "你好,介绍一下你自己。", "stream": false }'
3. Python 代码示例
import requests
response = requests.post("http://localhost:11434/api/generate", json={ "model": "mistral", "prompt": "什么是机器学习?" })
print(response.json()["response"])
或者
import ollama
res=ollama.chat(model="deepseek-r1:1.5b",stream=False,messages=[{"role": "user","content": "你是谁"}],options={"temperature":0})
print(res)
📦 导入 & 导出模型
1. 导出模型
ollama save <model_name> <file_path>
例如:
ollama save mistral ./mistral.ollama
2. 导入模型
ollama load <file_path>
例如:
ollama load ./mistral.ollama