如何通过接口调用本地部署的 DeepSeek 模型?

如何通过接口调用本地部署的 DeepSeek 模型?

有没有nodejs python golang通过接口调用本地部署的 DeepSeek 模型的方法

5 回复

下面分别给大家提供了nodejs python golang通过接口调用本地部署的 DeepSeek 模型的方法

通过接口调用本地部署的 DeepSeek教程 :https://www.bilibili.com/video/BV1r7PRe9EFq/?p=4

更多DeepSeek AI大模型系列模型教程https://www.itying.com/goods-1206.html

Nodejs通过接口调用本地部署的 DeepSeek 模型

import { Ollama } from 'ollama'

const ollama = new Ollama({ host: 'http://127.0.0.1:11434' })

let prompt=`
curl http://localhost:11434/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-r1:7b",
"messages": [
{
"role": "user",
"content": "你好"
}
]
}' 把上面代码转换成golang代码
`
const response = await ollama.chat({
  model: 'qwen2:1.5b',
  messages: [{ role: 'user', content: prompt }],
})

console.log(response.message.content)

Golang通过接口调用本地部署的 DeepSeek 模型

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type RequestBody struct {
	Model    string    `json:"model"`
	Messages []Message `json:"messages"`
}

func main() {
	url := "http://localhost:11434/v1/chat/completions"

	requestBody := RequestBody{
		Model: "deepseek-r1:1.5b",
		Messages: []Message{
			{
				Role:    "user",
				Content: "你好,你叫什么",
			},
		},
	}

	// 将结构体编码为 JSON
	jsonData, err := json.Marshal(requestBody)
	if err != nil {
		fmt.Println("Error encoding JSON:", err)
		return
	}

	// 创建 HTTP 请求
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
	if err != nil {
		fmt.Println("Error creating request:", err)
		return
	}

	// 设置请求头
	req.Header.Set("Content-Type", "application/json")

	// 发送请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("Error sending request:", err)
		return
	}
	defer resp.Body.Close()

	// 读取响应
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error reading response:", err)
		return
	}

	// 打印响应
	fmt.Println("Response:", string(body))
}

Python通过接口调用本地部署的 DeepSeek 模型

from ollama import Client
client = Client(
  host='http://127.0.0.1:11434'  #大模型所在服务器的地址  
)
response = client.chat(model='deepseek-r1:1.5b', messages=[
 {
      'role': 'system',
      'content': '你是一个乐于助人的助手'
 },
  {
    'role': 'user',
    'content': '你叫什么名字',
  },
])

print(response['message']['content'])

更多关于如何通过接口调用本地部署的 DeepSeek 模型?的实战系列教程也可以访问 https://www.itying.com/goods-1206.html


哈哈,调用本地部署的DeepSeek模型?简单得像写个“Hello World”!首先,确保你的模型已经乖乖地坐在本地服务器上。然后,用你最喜欢的编程语言(Python、Java、Go,随你挑)写个HTTP请求,像这样:

import requests

url = "http://localhost:5000/predict"  # 假设你的模型在5000端口
data = {"input": "你的输入数据"}
response = requests.post(url, json=data)
print(response.json())

搞定!如果模型不听话,检查一下端口和输入格式,别让它偷懒。😄

要通过接口调用本地部署的DeepSeek模型,你需要先确保模型已经正确部署,并且可以通过HTTP或API的方式访问。以下是一个基本步骤:

  1. 启动服务:确保你的DeepSeek模型已经被封装成一个可以提供服务的接口(比如Flask或Django应用),监听在某个本地地址和端口上。

  2. 获取API文档:查看模型服务提供的API文档,了解如何构造请求以及预期的响应格式。

  3. 发送请求:使用编程语言中的HTTP库(如Python的requests模块)向该服务发送POST请求,请求体中包含需要分析的数据。

  4. 处理响应:接收并解析服务器返回的结果,根据具体需求进行后续处理。

例如,如果你的服务运行在localhost:5000上,你可以使用如下Python代码来调用它:

import requests

url = "http://localhost:5000/predict"
data = {"input": "your data here"}

response = requests.post(url, json=data)
result = response.json()

请根据实际的服务地址和接口文档调整URL及数据结构。

要在本地部署DeepSeek模型并通过接口调用它,你需要先确保模型已经成功部署。这通常涉及到安装必要的依赖库、配置环境变量以及运行模型服务。假设模型已经被封装为一个服务,你可以使用HTTP请求或RPC等方式来调用。

  1. 启动服务:首先需要启动模型服务,这可能是一个使用Flask、Django或者其它框架搭建的Web服务。

  2. 发送请求:可以使用Python的requests库发送HTTP POST请求给该服务,附带输入数据。例如:

    import requests
    
    url = "http://localhost:5000/predict"  # 假设这是你的模型服务地址
    data = {"input": "your data here"}
    response = requests.post(url, json=data)
    print(response.json())
    
  3. 处理响应:根据模型输出格式处理返回的数据。

请根据实际使用的框架和服务端点进行调整。如果遇到具体技术问题,可能需要查阅相关文档或社区支持。

要通过要通过接口调用本地部署的 DeepSeek 模型,可以按照以下步骤进行:

1. 启动本地模型服务

首先,确保 DeepSeek 模型已经在本地部署并启动。通常,模型会通过 HTTP 或 WebSocket 提供接口服务。假设模型部署在本地主机的 8000 端口,服务端代码如下:

from flask import Flask, request, jsonify
import torch

app = Flask(__name__)
model = torch.load('deepseek_model.pth')  # 加载模型

@app.route('/predict', methods=['POST'])
def predict():
    data = request.json
    input_data = data['input']
    output = model.predict(input_data)  # 使用模型进行预测
    return jsonify({'output': output})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

2. 客户端调用接口

通过 HTTP POST 请求调用模型的 predict 接口。Python 示例代码如下:

import requests

url = 'http://localhost:8000/predict'
data = {'input': 'your_input_data'}

response = requests.post(url, json=data)

if response.status_code == 200:
    result = response.json()
    print('Model output:', result['output'])
else:
    print('Failed to get response:', response.status_code)```

### 3. 注意事项
- **模型格式**:确保模型为 `torch.save` 保存的 `.pth` 文件。
- **依赖项**:服务端和客户端需安装必要的库,如 `Flask` 和 `requests`。
- **性能优化**:调试模式可能不适用于生产环境,建议使用 `gunicorn` 或 `uWSGI` 部署。

通过这些步骤,您可以成功调用本地部署的 DeepSeek 模型。
回到顶部