1、 打开deep seek网站地址
2、 点击API开发平台
3、 创建一个key(注意:key只能复制查看一次,请保存记事本中)
4、 保存好自己的API Key
5、 下载最新版WPS,随便创建word文档,点击"工具"-->"开发工具"
6、 查看代码
7、 在打开的WPS宏编辑器界面中,左侧找到“Project(Normal.dotm)”,在"代码"上右击,选择"插入"->创建一个deep_seek"模块"
8、 将下面代码直接复制到deep_seek中,改成自己apikey就可以
// WPS文本扩写宏
const DEEPSEEK_API_KEY = "使用自己申请的API-KEY";
// deepseek的接口地址
const DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions";
// 调用模型名称
const DEEPSEEK_MODEL = "deepseek-chat";
// 提示词模板
function PROMPT_TEMPLATE(text) {
return `你是一位专业的AI文档助手:
1. 保持原文核心意思不变
2. 增加相关细节和背景信息
3. 使用正式、专业的语言风格
请扩写以下文本:
${text}`;
}
function expandText() {
const selection = Application.Selection;
// 检查是否有选中文本
console.log("开始执行宏,检查选中文本...");
if (!selection || !selection.Text) {
console.log("未检测到选中文本");
alert("请选择需要扩写的文本");
return;
}
const originalText = selection.Text;
console.log("检测到选中文本:", originalText);
try {
// 显示加载提示
alert("正在扩写文本,请稍候...");
// 调用OpenAI API
console.log("开始调用OpenAI API...");
callOpenAI(originalText)
.then(response => {
// 检查API响应是否有效
if (!response || typeof response !== "string") {
throw new Error("API返回无效的响应");
}
console.log("成功获取API响应:", response);
// 用扩写结果替换原文本
selection.Text = response;
console.log("文本替换完成");
})
.catch(error => {
// 恢复原文本
selection.Text = originalText;
throw error;
})
.finally(() => {
console.log("完成");
});
} catch (error) {
console.error("扩写失败:", error);
// 显示错误信息
alert("扩写失败:" + error.message);
}
}
function callOpenAI(text) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
const url = DEEPSEEK_API_URL;
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", `Bearer ${DEEPSEEK_API_KEY}`);
xhr.onreadystatechange = function() {
console.log("XHR状态变化:", xhr.readyState);
if (xhr.readyState === 4) {
console.log("API请求完成,状态码:", xhr.status);
try {
console.log("HTTP请求报文:", JSON.stringify({
url: url,
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${DEEPSEEK_API_KEY}`
},
body: JSON.stringify(data)
}));
console.log("API响应原始数据:", xhr.responseText);
const json = JSON.parse(xhr.responseText);
if (!json.choices || !json.choices[0] || !json.choices[0].message || !json.choices[0].message.content) {
console.error("API返回无效的数据结构");
reject(new Error("API返回无效的数据结构"));
} else {
console.log("成功解析API响应");
console.log("完整扩写结果:", json.choices[0].message.content);
resolve(json.choices[0].message.content.trim());
}
} catch (error) {
console.error("解析API响应失败:", error);
reject(new Error("解析API响应失败"));
}
}
};
const data = {
model: DEEPSEEK_MODEL,
messages: [
{
role: "user",
content: PROMPT_TEMPLATE(text)
}
],
temperature: 1.5
};
xhr.send(JSON.stringify(data));
});
}
9、 点击保存,保存为启用宏文件
10、 回到文字编辑界面,点击“文件”菜单,选择“选项”
11、 在选项对话框中,找到“自定义功能区”,在右侧的“自定义功能区”中,“工具”选项卡下点击“新建组”
12、 选中上一步新建的组,在左侧“自定义功能区”中下拉,找到“宏”,可以看到宏代码的名称“expandText”,选中,点击中间的“添加”按钮,添加到新建的组下
13、 回到文字编辑界面,看到“工具”栏下,多了“AI文档”工具