当前位置: 首页 > news >正文

前端使用koa实现调取deepseekapi实现ai聊天

流程,前端调取聊天接口,聊天接口做中转,我们使用node.js(用koa更方便快捷)去调取deepseek模型获取数据,deepseek模型需要下载Ollama。

1.Ollama安装好后访问代表成功

2.安城成功后去下载模型

3.命令窗可使用win+r ,使用命令ollama run deepseek-r1:1.xxxxxx 根据对应模型启动

1.koa的代码(中转站,相当于后端,去调取deepseek)


const Koa = require('koa');
const Router =require('koa-router')
const axios = require('axios');
const bodyParser = require('koa-bodyparser'); // 引入中间件
//实例化
const app = new Koa();
const router =new Router()
app.use(bodyParser()); 
app.use(router.routes()).use(router.allowedMethods())
//导入数据
// const indexData=require('./datas/index.json')
const OLLAMA_BASE_URL = 'http://127.0.0.1:11434';
//获取模型列表
router.get('/models', async (ctx) => {try {const response = await axios.get(`${OLLAMA_BASE_URL}/api/tags`);ctx.body = {success: true,models: response.data.models,};} catch (error) {ctx.status = 500;ctx.body = {success: false,error: error.message,};}});// 与 DeepSeek 模型对话
router.post('/api/chat', async (ctx) => {try {const { prompt } = ctx.request.body;if (!prompt) {ctx.status = 400;ctx.body = { error: 'Prompt is required' };return;}const response = await axios.post(`${OLLAMA_BASE_URL}/api/generate`, {model:"deepseek-r1:1.5b",prompt,stream: false // 设置为 true 如果需要流式响应});ctx.body = {code:200,success: true,response: response.data};} catch (error) {console.error('Ollama API error:', error);ctx.status = 500;ctx.body = {error: 'Failed to get response from Ollama',details: error.message};}});//监听某个端口
app.listen(3001,'0.0.0.0',(error)=>{if(error){console.log('服务器启动错误',error)return}else{console.log('服务器启动成功端口号为127.0.0.1:3001')}
})

2.前端代码只需要正常调用koa暴露的接口就好(只需关注使用方式,参数自定义)

//聊天发送onChatSubmit() {let keyword = this.chatKeyword;this.chatKeyword = "";this.chatAiList.push({ai: "",my: keyword || "",});chatApi.chat({ prompt: keyword || "" }).then((res) => {this.aiChatWord = res?.response?.response || "";this.chatAiList.push({ai: this.aiChatWord || "",my: "",});}).catch((err) => {});},

http://www.dtcms.com/a/339655.html

相关文章:

  • RabbitMQ:SpringAMQP Fanout Exchange(扇型交换机)
  • Apache ECharts 6.0.0 版本-探究自定义动态注册机制(二)
  • HTML5视频加密播放的主要优势
  • 本地存储(Local Storage)与Cookie的深度对比
  • RWA在DeFi中的应用
  • 行业分析---领跑汽车2025第二季度财报
  • EasyExcel 合并单元格最佳实践:基于注解的自动合并与样式控制
  • 大模型如何通过人工指标来评估效果?有哪些常用的人工指标?
  • FGF21对牛磺胆酸钠诱导的急性胰腺损伤的应答
  • DAY48 随机函数与广播机制
  • 补偿电流控制的APF并联型有源电力滤波器simulink
  • 2025年09月计算机二级MySQL选择题每日一练——第一期
  • 《深入源码理解webpack构建流程》
  • springboot 项目正常启动后自动结束
  • 当硅基生命遇见碳基萌宠:Deepoc具身智能如何重新定义“宠物监护者”
  • 【QT入门到晋级】进程间通信(IPC)-socket(包含性能优化案例)
  • UX 设计入门第二课:如何洞察人心?用户研究方法论导览
  • elementplus组件文本框设置前缀
  • 07复杂度分析实战习题集
  • PG靶机 - Pebbles
  • 无人机/航测/三维建模领域常见的“航线规划或建模方式
  • 基于单片机智能拐杖/导盲杖/老人防摔倒设计
  • python-林粒粒的视频笔记1
  • 网络数据包
  • 用relation-graph构建关系图谱 vue版
  • 单片 、物联网、51单片机、软硬件之基于STM32与蓝牙的仓储管控系统的设计与实现/基于物联网的仓库管理系统
  • 【实时Linux实战系列】基于实时Linux的物联网系统设计
  • AI硬件 - AMD显卡架构演进及产品线
  • 快速傅里叶变换:数字信号处理的基石算法
  • ubuntu24.04 用apt安装的mysql修改存储路径(文件夹、目录)