Word に OpenAI コードを導入するには、VBA エディターを使用する必要があります。OpenAI コードを Word にインポートする手順は次のとおりです。
-
Word 文書を開き、Alt + F11 を押して VBA エディタを開きます。
-
VBA エディタで、[挿入] メニューを選択し、[モジュール] を選択します。
-
新しく作成したモジュールで、OpenAI コードをモジュールに貼り付けます。
-
モジュールを保存し、VBA エディタを閉じます。
-
Word 文書で、OpenAI コードを適用するテキストを選択します。
-
「開発者」タブを開き、「マクロ」を選択します。
-
[マクロ名] ボックスに「ChatGPT」 (またはコードで定義したもの) と入力し、 [作成] をクリックします。
コードを追加します:
コード
Option Explicit
Sub ChatGPT3()
Const apiKey As String = "你的OPENAI的KEY秘钥"
Const modelName As String = "gpt-3.5-turbo"
If Selection.Type = wdSelectionNormal Then
Dim selectedText As String: selectedText = Replace(Selection.text, ChrW$(13), "")
Dim ans As String: ans = GetOpenAIResponse(SendOpenAIRequest(apiKey, modelName, selectedText))
Selection.text = selectedText & vbNewLine & ans
End If
End Sub
Function SendOpenAIRequest(apiKey As String, modelName As String, prompt As String) As String
With CreateObject("MSXML2.XMLHTTP")
.Open "POST", "https://api.openai.com/v1/chat/completions", False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Authorization", "Bearer " & apiKey
.send "{""model"":""" & modelName & """, ""messages"":[{""role"":""user"",""content"":""" & prompt & """}], ""temperature"":0.7}"
SendOpenAIRequest = .responseText
End With
End Function
Function GetOpenAIResponse(responseText As String) As String
GetOpenAIResponse = Replace(Split(responseText, """content"":""")(1), "\n", "")
End Function
空白の領域を右クリックし、「リボンのカスタマイズ」を選択します。
左側のボックスでマクロを選択し、右側で [新しいグループ] を選択し、作成したばかりの ChatGPT を選択します。図に示すように、名前を変更できます。
図に示すように、「名前の変更」を選択します。
次のように、質問したい質問を入力してテキストを選択し、ChatGPT をクリックして使用します。
最終的な効果:
チュートリアルのリンクはここをクリックしてください: Master AI - Control the Future - Learn Video Tutorials - Tencent Classroom