当前位置:AIGC资讯 > AIGC > 正文

GPT3.5 与word的结合,提前享受office copilot

1.你先要拥有GPT3.5  key

如何获取见上篇

2.配置office选项卡

 3.编写vba

Sub ChatGPT()

    Dim selectedText As String
    
    Dim apiKey As String
    
    Dim response As Object, re As String
    
    Dim midString As String
    
    Dim ans As String
    
    If Selection.Type = wdSelectionNormal Then
        
        selectedText = Selection.Text
        
        selectedText = Replace(selectedText, ChrW$(13), "")
        
        apiKey = "your key"
        
        URL = "https://api.openai.com/v1/chat/completions"
        
        Set response = CreateObject("MSXML2.XMLHTTP")
        
        response.Open "POST", URL, False
        
        response.setRequestHeader "Content-Type", "application/json"
        
        response.setRequestHeader "Authorization", "Bearer " + apiKey
        
        response.Send "{""model"":""gpt-3.5-turbo"", ""messages"":[{""role"":""user"",""content"":""" & selectedText & """}], ""temperature"":0.7}"
        
        re = response.responseText
        
        midString = Mid(re, InStr(re, """content"":""") + 11)
        
        ans = Split(midString, """")(0)
        
        ans = Replace(ans, "\n", "")
        
        Selection.Text = selectedText & vbNewLine & ans
        
        Else
        
        Exit Sub
    
    End If

End Sub

4.新建vba模块 插入代码

5.先在左边自定义选项卡,然后把邮编的宏放入左边即可

 6. 效果

 最终运行与网速有关,我这边还是要等一会儿。

结语:

实现上面的功能我主要看了b站up小卫是David的视频。代码也是他写的。原视频目前已删除,侵删。

更新时间 2023-11-20