Excel中调用Kimi Moonshot大模型的方法
两年前马老师分享了如何在Excel中制作UDF(User Defined Formula)用户定义公式从而调用ChatGPT的方法。随着国产大模型的跟进,兼容OpenAI的API的大模型不断出现。本篇马老师将再次分享用Excel调用Kimi的Moonshot大模型的方法。

我们要实现的效果如上图所示,这个公式有两个主要参数。其一是提示词(prompt)要操作的对象单元格范围,其二是应用到每个单元格中的提示词。
获取Moonshot API
首先你要注册Kimi,然后在Moonshot AI的用户中心的API Key管理后台中获得API Key。
https://platform.moonshot.cn/console/api-keys

注册以后,你就将有15元免费额度。记得复制Key,一会要用。
在Excel中制作UDF
打开Excel,按Alt+F11打开开发者界面。先制作一个personal.xlsb。这个方法微软官方有详细介绍:https://support.microsoft.com/zh-cn/office/-66c97ab3-11c2-44db-b021-ae005a9bc790
需要注意的是,你需要在你工作的工作簿中添加Personal.xlsb参照,以避免出现#NAME?错误。具体方法请查看:
https://www.myonlinetraininghub.com/creating-a-reference-to-personal-xlsb-for-user-defined-functions-udfs
然后打开PERSONAL.XLSB的Module1,将下面代码贴进去,并替换你的API Key:
Option Explicit
Public Function Kimi(ByVal question As String, _
Optional ByVal instruction As String = "回答问题,简单输出,返回不包含 leading_text", _
Optional ByVal temperature As Single = 0.3, _
Optional ByVal maxtoken As Integer = 500, _
Optional ByVal model As String = "moonshot-v1-8k") As String
Const apiKey As String = "sk-yk***xLM04"
Const apiEndpoint As String = "https://api.moonshot.cn/v1/chat/completions"
Dim requestHeaders As String
Dim httpRequest As Object
Dim httpResponse As String
Dim cursor As Long
Set httpRequest = CreateObject("MSXML2.XMLHTTP")
httpRequest.Open "POST", apiEndpoint, False
httpRequest.SetRequestHeader "Content-Type", "application/json"
httpRequest.SetRequestHeader "Authorization", "Bearer " & apiKey
httpRequest.Send "{""model"": """ & model & """,""messages"": [{""role"": ""system"", ""content"": """ & _
instruction & """},{""role"": ""user"", ""content"": """ & question & """}], ""temperature"": " & temperature & "}"
If httpRequest.Status = 200 Then
'Debug.Print httpRequest.responseText
cursor = InStr(1, httpRequest.responseText, """content"":")
Kimi = Replace(Replace(Mid(httpRequest.responseText, cursor + 11, InStr(cursor, httpRequest.responseText, "},""finish_reason" _
) - cursor - 12), "\n", vbCrLf), "\""", Chr(34))
Else
Kimi = "API Error: " & httpRequest.responseText
End If
End Function
Public Function Moonshot(ByVal target As Range, _
Optional ByVal instruction As String = "回答问题,简单输出,返回不包含 leading_text", _
Optional ByVal temperature As Single = 0.3, _
Optional ByVal maxtoken As Integer = 500, _
Optional ByVal model As String = "moonshot-v1-8k") As Variant
Dim answer As VbMsgBoxResult
answer = MsgBox("Ask Kimi?", vbYesNo, "Click No and lost all results :(")
If answer = vbNo Then Exit Function
Application.ScreenUpdating = False
Dim result() As Variant
Dim cell As Range
Dim i As Integer
ReDim result(1 To target.Rows.Count, 1 To 1)
With KimiPrgbar
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
.KimiLabel.Width = 1
.Show (False)
End With
DoEvents
For i = 1 To target.Rows.Count
Set cell = target.Cells(i, 1)
result(i, 1) = Kimi(cell.Value, instruction, temperature, maxtoken, model)
Call UpdateProgressBar(i, target.Rows.Count)
Next i
KimiPrgbar.Hide
Application.ScreenUpdating = True
Application.Calculation = XlCalculation.xlCalculationManual
Moonshot = result
End Function
Sub UpdateProgressBar(currentStep As Integer, totalSteps As Integer)
With KimiPrgbar
.KimiLabel.Width = (currentStep / totalSteps) * KimiPrgbar.KimiFrame.Width
.KimiFrame.Caption = currentStep & "/" & totalSteps
.Repaint
.Show (False)
End With
DoEvents
End Sub
建立一个配套的进度条
接下来要建立一个进度条控件,因为如果是上百行的运行需要大量时间。
- 右键在VBAProject(PERSONAL.XLSB)上插入一个UserForm,取名(Name)为KimiPrgbar。
- 在这个UserForm上画一个Frame,取名为KimiFrame。
- 在这个Frame上画一个Label,取名为KimiLabel。自定义一个你喜欢的进度条颜色作为BackColor。
完工后长这样:
接下来就大功告成了。
公式和参数说明
Kimi函数是单次调用大模型的函数,可以单独使用:
参数名 | 说明 |
question | 必填。Prompt提示词。 |
instruction | 可选。全局提示词。 |
temperature | 可选。返回的丰富度,默认0.3。 |
maxtoken | 可选。最大返回Token数。默认500。 |
model | 可选。目前是 moonshot-v1-8k ,moonshot-v1-32k ,moonshot-v1-128k 其一。默认8k |
Moonshot函数可以对一个列的单元格依次执行kimi函数并返回一个列作为结果。
参数名 | 说明 |
target | 必填。全局提示词操作的对象。 |
instruction | 可选。全局提示词。 |
temperature | 可选。返回的丰富度,默认0.3。 |
maxtoken | 可选。最大返回Token数。默认500。 |
model | 可选。目前是 moonshot-v1-8k ,moonshot-v1-32k ,moonshot-v1-128k 其一。默认8k |
使用频率限制
为了避免被过度薅羊毛,月之暗面对API进行了限制。但基本上只要充值累计超过50元,每分钟可以请求的次数就能有200,每分钟能够返回的Token能有128K。这可以满足大部分用户的需求。
用户等级 | 累计充值金额 | 并发 | RPM | TPM | TPD |
---|---|---|---|---|---|
Free | ¥ 0 | 1 | 3 | 32,000 | 1,500,000 |
Tier1 | ¥ 50 | 50 | 200 | 128,000 | 10,000,000 |
Tier2 | ¥ 100 | 100 | 500 | 128,000 | 20,000,000 |
Tier3 | ¥ 500 | 200 | 5,000 | 384,000 | Unlimited |
Tier4 | ¥ 5,000 | 400 | 5,000 | 768,000 | Unlimited |
Tier5 | ¥ 20,000 | 1,000 | 10,000 | 2,000,000 | Unlimited |
因为实际用量非常小,所以建议多个用户共享一个API Key。
以上就是马老师本篇的分享,粉丝福利,赶快来试试吧。