1.deepseek本地部署
需要魔法下载
python
curl -fsSL https://ollama.com/install.sh | sh
在官网找到需要下载的deepseek模型版本

复制命令到终端
python
ollama run deepseek-r1:7b
停止ollama服务
python
sudo systemctl stop ollama # sudo systemctl stop ollama.service
开启ollama服务
python
sudo systemctl start ollama.service
查看ollama服务状态
python
sudo systemctl status ollama.service
使用allama_gui
一个简易界面来使用模型
- 安装:python -m pip install ollama_gui
- 运行:python -m ollama_gui

2.Dify本地部署
安装好docker
python
git clone https://github.com/langgenius/dify.git
python
cd dify
cd docker
cp .env.example .env
#########
#在.env文件的最后添加
#启用自定义模型
CUSTOM_MODEL_ENABLED=true
#指定Ollama的API地址(根据部署环境调整IP)
OLLAMA_API_BASE_URL=host.docker.internal:11434OLLAMA_API_BASE_URL=host.docker.internal:11434
########
docker compose up -d
此处若报错
python
+] Running 9/9
✘ sandbox Error Cannot connect to the Docker daemon at unix:///var... 3.8s
✘ api Error Cannot connect to the Docker daemon at unix:///var/run... 3.8s
✘ worker Error Cannot connect to the Docker daemon at unix:///var/... 3.8s
✘ weaviate Error Cannot connect to the Docker daemon at unix:///va... 3.8s
✘ web Error Cannot connect to the Docker daemon at unix:///var/run... 3.8s
✘ db Error Cannot connect to the Docker daemon at unix:///var/run/... 3.8s
✘ ssrf_proxy Error Get "https://registry-1.docker.io/v2/": proxyco... 3.8s
✘ redis Error Get "https://registry-1.docker.io/v2/": proxyconnect... 3.8s
✘ plugin_daemon Error Cannot connect to the Docker daemon at unix:... 3.8s
Error response from daemon: Get "https://registry-1.docker.io/v2/": proxyconnect tcp: dial tcp 11.0.250.2:10023: connect: connection refused
配置代理下载
python
systemctl edit docker.service
python
[Service]
Environment="HTTP_PROXY=http://ip:port/"
Environment="HTTPS_PROXY=http://ip:port/"
Environment="NO_PROXY=localhost,127.0.0.0/8"

访问http://localhost/install 如果服务器没有界面可在另外浏览器访问http://服务器IP/install

登陆后进入界面点击右上角设置

点击模型供应商下滑找到Ollama

点击添加模型
服务器终端输入ollama list找到添加的模型名称拷贝输入

基础URL填入可能报错
python
2f0>: Failed to establish a new connection: [Errno 111] Connection refused'))
解决办法
python
systemctl edit ollama.service
对于每个环境变量,在 [Service]
部分下添加一行 Environment
python
[Service]
Environment="OLLAMA_HOST=0.0.0.0"
python
systemctl daemon-reload
systemctl restart ollama
添加模型以及基础的URL之后,在右上角的系统设置中添加基本模型,找不到就刷新页面重试

点击创建空白应用

输入应用名称并点击创建

在界面中进行对话

打造本地的RAG
选择bge-m3 或者 nomic-embed-text模型
在终端执行命令
python
ollama pull bge-m3 #ollama pull nomic-embed-text
下载完毕后在模型供应商中添加嵌入模型

保存后出现在系统模型设置里面添加模型,若没有出现刷新页面即可

点击知识库

点击创建知识库

上传文档后点击下一步,默认设置不用改,点击保存并处理即可

出现一下界面创建完成

点击工作室和聊天助手

点击上下文添加按钮添加文档

在对话框提问即可

API调用python实现
python
import requests
import json
# API的基本信息
url = 'your-API/chat-messages' # 替换为实际的API端点
api_key = 'your-API_KEY' # 替换为你的API密钥
# 设置请求头
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
# 设置请求数据
data = {
"inputs": {
# "text": "你的输入文本"
},
"query": "你好",
"responsemode": "blocking",
"conversationid": "",
"user": "1"
}
# 发送POST请求
response = requests.post(url, headers=headers, json=data)
# 检查响应
if response.status_code == 200:
# 请求成功,解析JSON响应
print(response.json())
else:
# 请求失败,打印错误信息
print(f"Error: Received status code {response.status_code}")
print(response.text)
参考链接