大模型-Ollama使用相关的笔记
- 解决Ollama外网访问问题(配置ollama跨域访问)
- Postman请求样例
解决Ollama外网访问问题(配置ollama跨域访问)
安装Ollama完毕后,
/etc/systemd/system/ollama.service
进行如下修改:
[Unit]
Description=Ollama Service
After=network-online.target
[Service]
ExecStart=/usr/bin/ollama serve
User=ollama
Group=ollama
Restart=always
RestartSec=3
Environment="OLLAMA_HOST=0.0.0.0"
Environment="OLLAMA_ORIGINS=*"
[Install]
WantedBy=default.target
重新加载systemd守护进程并启用Ollama服务
sudo systemctl daemon-reload
sudo systemctl enable ollama
sudo systemctl start ollama
python API访问
from ollama import Client
client = Client(
host='http://192.168.4.21:11434',
headers={'Content-Type': 'application/json'}
)
response = client.chat(model='internlm/internlm2.5:20b-chat', messages=[
{
'role': 'user',
'content': '天为什么是蓝色的',
},
])
print(response['message']['content'])
print("---------------------------------------------------------")
import requests,json
url = 'http://192.168.4.21:11434/api/generate'
headers = {
"Content-Type": "application/json"
}
data = {
'prompt': '做一个自我介绍吧?',
'model': 'internlm/internlm2.5:20b-chat',
'stream': False
}
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.text)