微信搜一搜【葡萄城社区】并关注,了解更多动态

SpreadJS 已经接入 DeepSeek 啦!

相信这段时间,大家都被【DeepSeek】刷屏了。DeepSeek 以其强大的技术能力和创新的解决方案,迅速成为行业焦点,吸引了众多厂商纷纷接入合作。很多使用葡萄城表格技术的开发者们也在问, SpreadJS 能不能接入 DeepSeek 呢?

当然能!本文将介绍 SpreadJS 接入 DeepSeek 的效果和接入方法。有了之前 SpreadJS 接入 ChatGPT 的经验,接入 DeepSeek 更是轻而易举。

SpreadJS 接入 DeepSeek 之后的效果

1.接入之后,先问问是不是 DeepSeek 吧。

确认了,是DeepSeek,没问题。

2.确认之后,使用 DeepSeek 根据单元格数据做动态提问

3.看不懂公式的意思,也问问 DeepSeek 吧

不好意思,没想到DeepSeek认真负责,还给出了示例。Dialog 要弄大点。

4.再试一试用 DeepSeek 生成公式

这次有点过于认真了,如果直接返回公式就可以直接插入单元格了。

5.再和DeepSeek来一点互动

5.1 数据看不懂,看看 DeepSeek 怎么说的。

5.2 怎么创建数据透视表

5.3 说的没错,让他创建吧

透视表都创建了,各种图表也不在话下。

最后,SpreadJS 怎么接入DeepSeek呢

把之前 Demo 里 OpenAI 的地址换成 DeepSeek 的地址,模型改成 DeepSeek 的模型就好啦。主要代码再放一遍,完整工程可以找技术顾问获取。

  1. 自定义 DeepSeek 提问函数

    // 自定义DeepSeek提问函数
    var DeepSeek_Query = function () { };
    DeepSeek_Query.prototype = new GC.Spread.CalcEngine.Functions.AsyncFunction('DeepSeek.QUERY', 1, 1, {
        description: "向GPT提问,直接返回结果",
        parameters: [
            {
                name: "问题"
            }]
    });
    DeepSeek_Query.prototype.defaultValue = function () { return 'Loading...'; };
    DeepSeek_Query.prototype.evaluateAsync = function (context, arg) {
        if (!arg) {
            return GC.Spread.CalcEngine.Errors.NotAvailable;
        }
    
        const response = openai.chat.completions.create({
            model: modelInfo.model,
            messages: [
                { role: "system", content: "You are a helpful excel assistant. " },
                { role: "user", content: arg + ",?只返回结果。" }
            ],
        });
        response.then(function (completion) {
            let desc = completion.choices[0].message.content;
            context.setAsyncResult(desc);
        });
    };
    GC.Spread.CalcEngine.Functions.defineGlobalCustomFunction("DeepSeek.QUERY", new DeepSeek_Query());
  2. 设计器公式分析命令

    let formulaAnalyze = {
        "title":"智能公式分析",
        "text":"公式分析",
        "iconClass":"ribbon-button-formulaAnalyze",
        "bigButton":"=ribbonHeight>toolbarHeight",
        "commandName":"formulaAnalyze",
        execute: function(designer){
            let spread = designer.getWorkbook(),sheet = spread.getActiveSheet();
            let formula = sheet.getFormula(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
            if(formula){           
                let loading = ElLoading.service({ lock: true, text: "Loading", background: "rgba(0, 0, 0, 0.7)"});
                const response = openai.chat.completions.create({
                    model: modelInfo.model,
                    messages: [
                        { role: "system", content: "You are a helpful assistant. 直接告诉我公式的意义,不用计算结果,答复里不能重复问题。" },
                        { role: "user", content: formula + ",这个公式有什么意义?" }
                    ],
                });
                response.then(function(completion){
                    loading.close();
                    let desc = completion.choices[0].message.content;
                    GC.Spread.Sheets.Designer.showMessageBox(desc, "", GC.Spread.Sheets.Designer.MessageBoxIcon.info)
                }).catch(function(){loading.close()});
            }
            else{
                GC.Spread.Sheets.Designer.showMessageBox("单元格没有公式", "提醒", GC.Spread.Sheets.Designer.MessageBoxIcon.warning)
            }
        }
    }
  3. 创建透视表Function Calling
        let messages = [{"role": "system","content": "你是一个数据透视表分析助手。"},
            {
                "role": "user", 
                "content": 
`根据表格标题内容和需求描述推荐创建数据透视表需要的行、列和值字段。
表格标题为:
---
${headerList}
---
需求描述:
---
${bindingData.description}
---`
            }];
        let functions = [{"type": "function", 
            "function":{
            "name": "pivot_talbe_analyze",
            "description": "对数据创建数据透视表,返回数据透视表结果",
            "parameters": {
                "type": "object",
                "properties": {
                    "rowFieldName": {
                        "type": "string",
                        "description": "行字段名称"
                    },
                    "columnFieldName": {
                        "type": "string",
                        "description": "列段名称"
                    },
                    "dataFieldName": {
                        "type": "string",
                        "description": "值字段名称"
                    },
                },
                "required": ["rowFieldName", "dataFieldName"]
            },
            "strict": true
        }}]
        try {
            var completion = await openai.chat.completions.create({
                "model": "qwen-plus",
                "messages": messages,
                "tools": functions,
                "function_call": {"name": "pivot_talbe_analyze"}
            });
            if(completion.choices[0].message.tool_calls){
                let args = JSON.parse(completion.choices[0].message.tool_calls[0].function.arguments);
                spread.suspendPaint();
                let activeSheetIndex = spread.getActiveSheetIndex();
                spread.addSheet(activeSheetIndex);
                spread.setActiveSheetIndex(activeSheetIndex);
                let newSheet = spread.getSheet(activeSheetIndex);
                let pivotTable = newSheet.pivotTables.add(getUniquePivotName(newSheet), pivotRange, 2, 0, GC.Spread.Pivot.PivotTableLayoutType.outline, GC.Spread.Pivot.PivotTableThemes.medium2);
                pivotTable.add(args.rowFieldName, args.rowFieldName, GC.Spread.Pivot.PivotTableFieldType.rowField);
                if(args.columnFieldName){
                    pivotTable.add(args.columnFieldName, args.columnFieldName, GC.Spread.Pivot.PivotTableFieldType.columnField);
                }
                pivotTable.add(args.dataFieldName, "求和项:" + args.dataFieldName, GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum);

                spread.resumePaint();
            }
        }
        catch(err){
            console.log(err)
        }
        finally{
        }
微信搜一搜【葡萄城社区】并关注,了解更多动态

葡萄城技术团队
2.7k 声望30.3k 粉丝

葡萄城是专业的软件开发技术和低代码平台提供商,聚焦软件开发技术,以“赋能开发者”为使命,致力于通过表格控件、低代码和BI等各类软件开发工具和服务,一站式满足开发者需求,帮助企业提升开发效率并创新开发模式。