DeepSeek-R1 免费本地部署 和 Python 调用

1、安装 Ollama 及R1本地测试
打开ollama官网
点击download,根据系统选择对应的安装包
下载后点击Install安装
通过Win+R打开命令行
cmd
输入代码测试安装成功,同时查看相关命令
ollama -v
下面回到ollama官网点击左上角Models
如果你的显卡有8G以上显存或8G以上内存,可以尝试7b的模型,如果没有就直接选择1.5b模型。下面是集中模型的下载代码,可以直接复制,粘贴到命令行。
DeepSeek-R1-Distill-Qwen-1.5B
ollama run deepseek-r1:1.5b
DeepSeek-R1-Distill-Qwen-7B
ollama run deepseek-r1:7b
复制安装命令在命令行,enter运行,等待下载完成。
通过ollama list查看已安装模型
ollama list
通过ollama run +模型名称运行模型,我这里是7b模型因此使用
ollama run deepseek-r1:7b
如果是1.5b模型则使用
ollama run deepseek-r1:1.5b
现在就可以开始对话了

如想要更改MODELS,HOST,等环境变量
set OLLAMA_MODELS=E:\ollama
set OLLAMA_HOST=127.0.0.1:8000
set OLLAMA_ORIGINS=*

2、Python 调用
1.运行服务
ollama serve
2.Python 调用

generate方式:

import requests
import json
import subprocess
while True:
    user_input = input(">>>")
    if user_input=='/bye':break
    if user_input=='/clear':
        subprocess.call('cls',shell=True)
        continue
    url = "http://localhost:11434/api/generate"
    data = { "model": "deepseek-r1:7b", "prompt": user_input }
    response = requests.post(url, json=data,stream=True)
    for line in response.iter_lines(): 
        if line: 
            data = json.loads(line.decode('utf-8'))
            print(data["response"],end='')
    print("\n")

chat方式:

import requests
import json
import subprocess
url = "http://localhost:11434/api/chat"
headers = {"Content-Type": "application/json"}
messages = [
    {
        "role": "system",
        "content": "You are a warm-hearted assistant, and you only speak Chinese."
    },
    {
        "role": "user",
        "content": "李华去公园玩把腿摔断了,被送到医院了。腿摔断一般要多长时间好?"
    },
    {
        "role": "assistant",
        "content": "腿摔断的恢复时间不一,通常需要几个月才能基本康复。\n\n如果是轻微的骨折,一般可以在1-2个月内恢复,主要需要注意休息和康复锻炼。"
    }
]
while True:
    user_input = input(">>>")#李华为啥去医院了?
    if user_input=='/bye':break
    if user_input=='/clear':
        subprocess.call('cls',shell=True)
        continue
    prompt= {
         "role": "user",
         "content": user_input
     }
    messages.append(prompt)
    # 初始对话
    data = {
      "model": "deepseek-r1:7b",
      "messages": messages,
      "stream": False
    }
    response = requests.post(url, headers=headers, data=json.dumps(data))
    print(response.json()['message']['content'])
    messages.append(response.json()['message'])
    if len(messages) >21:
        del messages[1:3]
 

作者:小6哥

物联沃分享整理
物联沃-IOTWORD物联网 » DeepSeek-R1 免费本地部署 和 Python 调用

发表回复