1、封装deepseek.js
import axios from 'axios'
class DeepSeekSDK {
constructor () {
// this.apiKey = 'sk-xxxx'
this.apiKey = ''
this.baseURL = '/deepseek'
this.buffer = ''
this.client = axios.create({
baseURL: this.baseURL,
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
}
})
}
async postData (data, filterData, source) {
try {
this.buffer = ''
const response = await this.client.post('/chat/completions', data, {
responseType: 'text', // 设置响应类型为流
cancelToken: source.token,
onDownloadProgress: (progressEvent) => {
const chunk = progressEvent.currentTarget.responseText
this.buffer += chunk
let eventEndIndex
while ((eventEndIndex = this.buffer.indexOf('\n\n')) >= 0) {
const event = this.buffer.substring(0, eventEndIndex)
this.buffer = this.buffer.substring(eventEndIn