お世話になります。
コードの理解が乏しくご確認をお願いしたいと思います。
下記のようにChatGPTに質問をして返信するものをネットで見つけて
コードをコピーしました。
こちらの内容をUserForm1から、質問と返信を記載できるようにしたいと
考えているのですが、下記のコードのどこを変えればよいのかが分かりません。
ご指導頂けないでしょうか
質問するTextBox1の設置箇所はわかりましたが、返信のためのTextBox2をどの
ようにコードへ反映するのかがわかりません。
お忙しいとは思いますが宜しくお願い致します。
フォーム名:UserForm1
質問を書き込むテキストボックス:TextBox1
ChatGPTからの回答はこちらにしたい:TextBox2
●チャットGPTメモ
Dim APIKey As String
Dim strTextToPredict As String
Dim strRequestBody, apiUrl, intCount As String
Dim strResponse As String
Dim strModel As String
Dim temp As Variant
Dim temp2, objhttp As Variant
Dim i As Integer
Dim intMaxTokens As Integer
'APIキーを入力します。
APIKey = "個人用APIキー"
'ChatGPTのAPIから出力される最大文字数を指定します。
intMaxTokens = 1000
'ChatGPT APIのモデルを指定します。
strModel = "text-davinci-003"
'ChatGPTへの質問内容を取得します。
strTextToPredict = TextBox1
'リクエスト先のURLを設定します。
apiUrl = "https://api.openai.com/v1/completions"
'XMLHTTPオブジェクトをセットします。
Set objhttp = CreateObject("MSXML2.XMLHTTP")
With objhttp
'リクエストのヘッダーを設定します。
.Open "POST", apiUrl, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Authorization", "Bearer " & APIKey
' リクエスト文を作成します。
strRequestBody = "{""model"":""" & strModel & """, ""prompt"":""" & strTextToPredict & """, ""max_tokens"":" & intMaxTokens & ", ""temperature"":0.5, ""top_p"":1}"
'リクエストを送信します。
.send strRequestBody
'APIからのレスポンスが返るまで待ちます。
Do While .readyState <> 4
DoEvents
Loop
'レスポンス情報をデバッグします。不要であれば削除してください。
'Debug.Print .responseText
'レスポンスのテキスト情報をExcel出力用変数に代入します。
strResponse = .responseText
End With
クリア
Range("B3:B100").Clear
'Json形式のデータの内回答テキストがある位置から分割します。
temp = Split(strResponse, "\n\n")
'配列数を取得します。
intCount = UBound(temp)
'配列分処理を繰り返します。
For i = 1 To intCount
If i = intCount Then
'最後の回答をセルに出力します。
temp2 = Split(temp(i), """,""")
Cells(i + 2, 2) = Replace(Replace(temp2(0), "\n", Chr(10)), "\", "")
Else
'最後の回答以外をセルに出力します。
Cells(i + 2, 2) = Replace(Replace(temp(i), "\n", Chr(10)), "\", "")
End If
Next i
Dim 最終行, AA As Integer
最終行 = Cells(Rows.Count, 2).End(xlUp).Row
For AA = 2 To 最終行
Cells(AA, 2).Interior.Color = RGB(226, 239, 218)
Next AA
Range("B:B").WrapText = True
MsgBox "回答が終わりました。"
If Range("D2") = "True" Then
最終行 = Cells(Rows.Count, 2).End(xlUp).Row
For i = 3 To 最終行
Application.Speech.Speak Cells(i, 2), True
Next i
End If