first commit
This commit is contained in:
Vendored
+92
@@ -0,0 +1,92 @@
|
||||
"use strict";
|
||||
var common_vendor = require("../../common/vendor.js");
|
||||
var api_modules_config = require("./config.js");
|
||||
require("../../stores/index.js");
|
||||
var stores_modules_AIResponse = require("../../stores/modules/AIResponse.js");
|
||||
var stores_modules_tabIndex = require("../../stores/modules/tabIndex.js");
|
||||
const {
|
||||
responseText,
|
||||
showResponseText,
|
||||
isLoading,
|
||||
isDone
|
||||
} = common_vendor.storeToRefs(stores_modules_AIResponse.useAIReponseStore());
|
||||
const {
|
||||
tabIndex
|
||||
} = common_vendor.storeToRefs(stores_modules_tabIndex.useTabStore());
|
||||
const API_KEY = "sk-b65f99c1b2ab416aaf340891cf4ca308";
|
||||
const AI_URL = "https://api.deepseek.com/chat/completions";
|
||||
const AI_MODEL = "deepseek-chat";
|
||||
const errMessage = {
|
||||
401: "API key \u65E0\u6548",
|
||||
403: "API key \u4F59\u989D\u4E0D\u8DB3"
|
||||
};
|
||||
const AIChat = async (q, symbol_1, symbol_2, symbol_3) => {
|
||||
const pageCfg = await api_modules_config.getPageConfig();
|
||||
let callWord = pageCfg.call_word;
|
||||
callWord = callWord.replace("[q]", q).replace("[symbol_1]", symbol_1).replace("[symbol_2]", symbol_2).replace("[symbol_3]", symbol_3);
|
||||
console.log(callWord);
|
||||
return await new Promise((resolve, reject) => {
|
||||
const requestTask = common_vendor.index.request({
|
||||
url: AI_URL,
|
||||
method: "POST",
|
||||
header: {
|
||||
"content-type": "application/json",
|
||||
"Authorization": `Bearer ${API_KEY}`
|
||||
},
|
||||
data: {
|
||||
model: AI_MODEL,
|
||||
messages: [
|
||||
{ role: "system", content: "You are a helpful assistant." },
|
||||
{ role: "user", content: callWord }
|
||||
],
|
||||
stream: true
|
||||
},
|
||||
enableChunked: true,
|
||||
responseType: "arraybuffer",
|
||||
success: (res) => {
|
||||
if (res.statusCode !== 200) {
|
||||
resolve({
|
||||
code: res.statusCode,
|
||||
message: errMessage[res.statusCode]
|
||||
});
|
||||
}
|
||||
isDone.value = true;
|
||||
},
|
||||
fail: (error) => {
|
||||
},
|
||||
complete: (complete) => {
|
||||
}
|
||||
});
|
||||
requestTask.onChunkReceived((res) => {
|
||||
var _a;
|
||||
const uint8Array = new Uint8Array(res.data);
|
||||
const decoder = new common_vendor.textEncodingShim.exports.TextDecoder("utf-8");
|
||||
const chunk = decoder.decode(uint8Array).toString().split("data: ");
|
||||
for (let i = 1; i < chunk.length; i++) {
|
||||
try {
|
||||
const result = JSON.parse(chunk[i]);
|
||||
if (result.choices[0].delta.content) {
|
||||
isLoading.value = false;
|
||||
responseText.value += (_a = result.choices[0].delta) == null ? void 0 : _a.content;
|
||||
}
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
common_vendor.watch(
|
||||
() => tabIndex.value,
|
||||
(newVal, oldVal) => {
|
||||
if (newVal !== oldVal) {
|
||||
requestTask.abort();
|
||||
isLoading.value = false;
|
||||
responseText.value = "";
|
||||
return;
|
||||
}
|
||||
},
|
||||
{
|
||||
deep: true
|
||||
}
|
||||
);
|
||||
});
|
||||
};
|
||||
exports.AIChat = AIChat;
|
||||
Reference in New Issue
Block a user