【微信小程序(云开发模式)变通实现DeepSeek支持语音】

整体架构

  1. 前端(微信小程序):
    • 使用微信小程序云开发能力,实现录音功能。
    • 将录音文件上传到云存储。
    • 调用云函数进行语音识别和 DeepSeek 处理。
    • 界面模仿 DeepSeek,支持文本编辑。
  2. 后端(云函数 + Node.js):
    • 使用云函数调用腾讯云语音识别(ASR)服务。
    • 调用 DeepSeek API 处理文本。

步骤 1:初始化云开发环境

  1. 在微信开发者工具中创建小程序项目,并开通云开发。

  2. project.config.json 中配置云函数目录:

    json

    复制

    {
      "cloudfunctionRoot": "cloud/functions/"
    }
    
  3. 初始化云开发环境:

    javascript

    复制

    wx.cloud.init({
      env: 'your-env-id', // 替换为你的云环境 ID
      traceUser: true
    });
    

步骤 2:编写云函数

1. 云函数结构

复制

cloud/
├── functions/
│   ├── asr/              # 语音识别云函数
│   │   ├── index.js
│   │   ├── config.json
│   │   └── package.json
│   ├── deepseek/         # DeepSeek 处理云函数
│   │   ├── index.js
│   │   ├── config.json
│   │   └── package.json
2. 语音识别云函数 (asr/index.js)

javascript

复制

const tencentcloud = require('tencentcloud-sdk-nodejs');
const AsrClient = tencentcloud.asr.v20190614.Client;
const fs = require('fs');
const path = require('path');

exports.main = async (event, context) => {
  const { fileID } = event;

  // 下载录音文件
  const fileContent = await wx.cloud.downloadFile({
    fileID: fileID
  });
  const audioPath = fileContent.tempFilePath;
  const audioData = fs.readFileSync(audioPath, { encoding: 'base64' });

  // 调用腾讯云语音识别
  const client = new AsrClient({
    credential: {
      secretId: process.env.TENCENT_SECRET_ID, // 从环境变量获取
      secretKey: process.env.TENCENT_SECRET_KEY
    },
    region: 'ap-guangzhou',
    profile: {
      httpProfile: {
        endpoint: 'asr.tencentcloudapi.com'
      }
    }
  });

  const params = {
    EngineModelType: '16k_zh',
    VoiceFormat: 'wav',
    Data: audioData
  };

  try {
    const response = await client.SentenceRecognition(params);
    return {
      success: true,
      data: response.Result
    };
  } catch (error) {
    return {
      success: false,
      message: error.message
    };
  }
};
3. DeepSeek 处理云函数 (deepseek/index.js)

javascript

复制

const axios = require('axios');

exports.main = async (event, context) => {
  const { text } = event;

  try {
    const response = await axios.post(
      'https://api.deepseek.com/v1/chat/completions',
      {
        model: 'deepseek-chat',
        messages: [
          { role: 'system', content: '你是一个文本处理助手。' },
          { role: 'user', content: text }
        ]
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${process.env.DEEPSEEK_API_KEY}` // 从环境变量获取
        }
      }
    );

    return {
      success: true,
      data: response.data.choices[0].message.content
    };
  } catch (error) {
    return {
      success: false,
      message: error.message
    };
  }
};
4. 安装依赖

在云函数目录下运行:

bash

复制

npm install tencentcloud-sdk-nodejs axios
5. 配置环境变量

在云开发控制台中,为云函数配置环境变量:

  • TENCENT_SECRET_ID: 腾讯云 SecretId
  • TENCENT_SECRET_KEY: 腾讯云 SecretKey
  • DEEPSEEK_API_KEY: DeepSeek API 密钥

步骤 3:编写小程序前端

1. 页面结构

复制

miniprogram/
├── pages/
│   ├── index/
│   │   ├── index.js       # 页面逻辑
│   │   ├── index.wxml     # 页面结构
│   │   └── index.wxss     # 页面样式
├── app.js                 # 小程序逻辑
├── app.json               # 小程序配置
└── app.wxss               # 全局样式
2. 页面逻辑 (index.js)

javascript

复制

Page({
  data: {
    isRecording: false,      // 是否正在录音
    recordTime: 0,           // 录音时长
    resultText: '',          // 识别结果
    editedText: '',          // 编辑后的文本
    isLoading: false         // 加载状态
  },

  // 开始录音
  startRecord() {
    this.setData({ isRecording: true, recordTime: 0 });

    this.recorderManager = wx.getRecorderManager();
    this.recorderManager.start({
      format: 'wav',
      sampleRate: 16000,
      numberOfChannels: 1,
      encodeBitRate: 48000
    });

    this.timer = setInterval(() => {
      this.setData({ recordTime: this.data.recordTime + 1 });
    }, 1000);

    this.recorderManager.onStop(async (res) => {
      clearInterval(this.timer);
      this.setData({ isRecording: false });
      await this.uploadAudio(res.tempFilePath); // 上传录音文件
    });
  },

  // 停止录音
  stopRecord() {
    if (this.recorderManager) {
      this.recorderManager.stop();
    }
  },

  // 上传录音文件
  async uploadAudio(filePath) {
    this.setData({ isLoading: true });

    try {
      // 上传到云存储
      const uploadRes = await wx.cloud.uploadFile({
        cloudPath: `audios/${Date.now()}.wav`,
        filePath: filePath
      });

      // 调用语音识别云函数
      const asrRes = await wx.cloud.callFunction({
        name: 'asr',
        data: { fileID: uploadRes.fileID }
      });

      if (asrRes.result.success) {
        this.setData({ resultText: asrRes.result.data, editedText: asrRes.result.data });
      } else {
        wx.showToast({ title: '识别失败', icon: 'none' });
      }
    } catch (error) {
      wx.showToast({ title: '上传失败', icon: 'none' });
    } finally {
      this.setData({ isLoading: false });
    }
  },

  // 编辑文本
  handleEditText(e) {
    this.setData({ editedText: e.detail.value });
  },

  // 调用 DeepSeek 处理文本
  async processText() {
    const { editedText } = this.data;
    if (!editedText) {
      wx.showToast({ title: '请输入文本', icon: 'none' });
      return;
    }

    this.setData({ isLoading: true });

    try {
      const deepseekRes = await wx.cloud.callFunction({
        name: 'deepseek',
        data: { text: editedText }
      });

      if (deepseekRes.result.success) {
        this.setData({ resultText: deepseekRes.result.data });
      } else {
        wx.showToast({ title: '处理失败', icon: 'none' });
      }
    } catch (error) {
      wx.showToast({ title: '网络错误', icon: 'none' });
    } finally {
      this.setData({ isLoading: false });
    }
  }
});
3. 页面结构 (index.wxml)

xml

复制

<view class="container">
  <!-- 录音按钮 -->
  <view class="record-button">
    <button
      bindtap="{{isRecording ? 'stopRecord' : 'startRecord'}}"
      class="{{isRecording ? 'stop-button' : 'start-button'}}"
    >
      {{isRecording ? '停止录音' : '开始录音'}}
    </button>
    <text class="record-time" wx:if="{{isRecording}}">录音中:{{recordTime}}s</text>
  </view>

  <!-- 识别结果 -->
  <view class="result-box">
    <text class="result-title">识别结果:</text>
    <textarea
      value="{{editedText}}"
      bindinput="handleEditText"
      placeholder="识别结果将显示在这里"
      class="result-text"
    ></textarea>
  </view>

  <!-- 处理按钮 -->
  <button bindtap="processText" class="process-button">调用 DeepSeek 处理</button>

  <!-- 处理结果 -->
  <view class="result-box">
    <text class="result-title">处理结果:</text>
    <textarea
      value="{{resultText}}"
      placeholder="处理结果将显示在这里"
      disabled
      class="result-text"
    ></textarea>
  </view>

  <!-- 加载状态 -->
  <view class="loading" wx:if="{{isLoading}}">
    <text>处理中...</text>
  </view>
</view>

运行 HTML

4. 页面样式 (index.wxss)

css

复制

.container {
  padding: 20px;
  background-color: #F5F5F5;
  min-height: 100vh;
}

.record-button {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 20px;
}

.start-button {
  background-color: #07C160;
  color: white;
  width: 80%;
}

.stop-button {
  background-color: #F5222D;
  color: white;
  width: 80%;
}

.record-time {
  margin-top: 10px;
  font-size: 14px;
  color: #666666;
}

.result-box {
  width: 100%;
  margin-top: 20px;
}

.result-title {
  font-size: 16px;
  font-weight: bold;
  color: #333333;
  margin-bottom: 10px;
}

.result-text {
  width: 100%;
  height: 100px;
  background-color: #FFFFFF;
  border: 1px solid #CCCCCC;
  border-radius: 5px;
  padding: 10px;
  font-size: 14px;
  color: #333333;
}

.process-button {
  margin-top: 20px;
  background-color: #1890FF;
  color: white;
  width: 80%;
}

.loading {
  margin-top: 20px;
  font-size: 14px;
  color: #666666;
  text-align: center;
}

步骤 4:测试

  1. 在微信开发者工具中运行小程序。
  2. 测试录音、语音识别、文本编辑和 DeepSeek 处理功能。

注意事项

  1. 云函数部署:
    • 确保云函数已上传并部署。
  2. 环境变量:
    • 在云开发控制台中正确配置环境变量。
  3. 录音权限:
    • 确保小程序已获取录音权限。
### 微信小程序中集成 DeepSeek API 或 SDK #### 一、准备工作 为了在微信小程序中成功集成 DeepSeek API 或者 SDK,开发者需先完成如下准备事项: - 注册并获取 DeepSeek 提供的有效 API Key 和其他必要的认证凭证[^1]。 - 安装所需的依赖库或工具包。如果采用的是 SDK 方式,则应按照官方文档指示下载对应版本的 SDK 并配置好环境。 #### 二、直接调用微信云开发能力访问 DeepSeek API 对于希望简化架构设计的应用场景而言,可以直接利用微信提供的云函数来发起 HTTP 请求至 DeepSeek API 接口。这种方式不需要额外部署自己的服务器资源,在一定程度上降低了运维成本和技术门槛。 ```javascript // 在 cloudfunctions/deepseekApi/index.js 文件内编写如下代码片段 const axios = require('axios'); exports.main = async (event, context) => { try { const response = await axios.post( 'https://api.deepseek.com/v1/endpoint', // 替换成实际API地址 { /* 请求体参数 */ }, { headers: { Authorization: `Bearer ${process.env.DEEPSEEK_API_KEY}` } } ); return response.data; } catch (error) { console.error(error); throw new Error(`Failed to call DeepSeek API`); } }; ``` 此部分实现了通过云函数间接调用外部服务的能力,并且可以方便地处理异步操作以及错误情况下的回滚机制。 #### 三、借助中间件转发请求给 DeepSeek API 当业务逻辑较为复杂或者出于安全性的考虑时,可以选择搭建一个独立的服务端作为代理层。该方案允许更灵活的数据预处理流程同时也便于维护日志记录等功能模块。 ```nodejs // 使用 Node.js Express 创建简单的HTTP Server 来桥接客户端与目标API之间的通信 const express = require('express'); const bodyParser = require('body-parser'); const request = require('request-promise-native'); const app = express(); app.use(bodyParser.json()); app.post('/proxy-deepseek-api', async(req, res) => { try{ let options = { method:'POST', uri:`https://api.deepseek.com/v1/${req.body.endpoint}`, // 动态拼接路径 body:req.body.payload, json:true, auth:{ bearer: process.env.DEEPSEEK_API_KEY } }; const result = await request(options); res.status(200).json(result); }catch(err){ console.log(err.message); res.sendStatus(500); } }); app.listen(process.env.PORT || 8080, ()=>{ console.log('Server is running...'); }); ``` 上述实例展示了如何构建一个简易网关用于接收来自前端的小程序请求并将它们传递给指定的目标 URL 。这有助于保护内部网络不受外界攻击的同时也使得跨域资源共享变得更加容易管理。 #### 四、引入 DeepSeek SDK 至项目工程文件夹下 假如 DeepSeek 支持以软件开发套件的形式分发其产品特性的话,那么最便捷的做法莫过于将其打包成 npm 包或是静态链接库等形式导入到本地工作区里边去了。不过需要注意一点就是必须遵循特定平台所规定的兼容性指南来进行适配调整。 假设存在名为 deepseek-sdk 的 NPM 包可供安装使用: ```bash npm install @deepseek/sdk --save ``` 接着可以在页面组件 JS 中这样写入初始化语句: ```javascript import * as DeepSeek from '@deepseek/sdk'; Page({ onLoad() { this.deepSeekClient = new DeepSeek.Client({ apiKey: YOUR_DEEPSEEK_API_KEY }); // 继续定义其他的生命周期方法... }, handleSomeEvent(e) { this.deepSeekClient.someMethod() .then(response => {/* 处理返回数据 */} ) .catch(error => {/* 错误捕获 */}) } }); ``` 这段脚本说明了怎样创建一个新的 Client 实例并与之交互从而触发某些预定的动作序列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术与健康

你的鼓励将是我最大的创作动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值