Nodejs调用deepseek本地模型的方法

1、需求启动ollama deepseek服务(windows电脑忽略)

ollama serve

2、Nodejs调用deepseek代码如下

https://www.npmjs.com/package/ollama

npm i ollama --save
//import { Ollama } from 'ollama'
const { Ollama } = require('ollama');

const ollama = new Ollama({ host: 'http://127.0.0.1:11434' }) 
let prompt = `你好`;
const response = await ollama.chat({
  model: 'deepseek-r1:7b',
  messages: [{ role: 'user', content: prompt }],
})

console.log(response.message.content)

deepSeek参数参考(自己记录用)

 import requests
  
 # 定义 API 的 URL (你的调用地址)
 url = api_url
 # 定义请求头,包括认证信息(如果有)
 headers = {
  "Content-Type": "application/json",
  'Authorization': 'Bearer '+ api_key
 }
  
 # content可以替换为自己想和模型对话的内容
 # "max_tokens":每个输出序列要生成的最大Tokens数量。
 # "top_k": -1 表示考虑所有Tokens。
 # "top_p": 1,表示模型会考虑Token总和概率100%,这相当于没有使用 "Top-P sampling"。
 # "temperature": 0,表示模型统会触发贪婪采样以进行下一个单词预测,即简单地选取词汇表中概率最高的单词
 # "stream": "false"表示以非流式方式,等待模型输出所有字符再返回给客户端
 payload = {
  "model": "DeepSeek-V3",
  "messages": [
  {"role": "user", "content": "介绍下大语言模型"} 
  ],
  "max_tokens": 150,
  "top_k": -1,
  "top_p": 1,
  "temperature": 0,
  "stream": "false"
 }
  
 # 发送 POST 请求
 response = requests.post(url, headers=headers, json=payload)
 result = response.json()['choices'][0]['message']['content']
 print("Response:", result)

Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐