OFFICE_오피스/엑셀_매크로
엑셀 VBA를 사용하여 작성되었으며, GPT-3.5 API에 대한 요청 및 응답 처리를 수행합니다.
AICanvas
2023. 4. 14. 13:11
728x90
SMALL
먼저, 사용자 인증 정보와 GPT-3.5 API 요청을 위한 필수 매개변수를 설정합니다.
' 사용자 인증 정보
Const API_KEY As String = "your_api_key"
' GPT-3.5 API 요청을 위한 필수 매개변수
Const API_URL As String = "https://api.openai.com/v1/engines/davinci-codex/completions"
Const API_MODEL As String = "davinci-codex"
Const API_MAX_TOKENS As Long = 150
Const API_STOP_SEQUENCE As String = vbNewLine
다음으로, 사용자가 입력한 텍스트를 GPT-3.5 API에 전송하여 응답을 받아오는 함수를 작성합니다.
' GPT-3.5 API에 요청을 보내고, 응답을 받아오는 함수
Private Function GetResponseFromAPI(ByVal prompt As String) As String
Dim xhr As Object
Set xhr = CreateObject("MSXML2.XMLHTTP")
xhr.Open "POST", API_URL, False
xhr.setRequestHeader "Content-Type", "application/json"
xhr.setRequestHeader "Authorization", "Bearer " & API_KEY
Dim json As Object
Set json = CreateObject("Scripting.Dictionary")
json("prompt") = prompt
json("model") = API_MODEL
json("max_tokens") = API_MAX_TOKENS
json("stop") = API_STOP_SEQUENCE
Dim body As String
body = JsonConverter.ConvertToJson(json)
xhr.Send body
Dim response As Object
Set response = JsonConverter.ParseJson(xhr.responseText)
Dim choices As Object
Set choices = response("choices")(0)
Dim text As String
text = choices("text")
GetResponseFromAPI = text
End Function
마지막으로, 사용자 입력을 받아 GPT-3.5 API에 전송하고, 응답을 받아 출력하는 매크로를 작성합니다.
Sub ChatWithGPT3()
' 사용자 입력 받기
Dim prompt As String
prompt = InputBox("Enter your message:", "GPT-3 Chatbot")
' GPT-3.5 API에 요청 보내기
Dim response As String
response = GetResponseFromAPI(prompt)
' 응답 출력하기
MsgBox response, vbInformation, "GPT-3 Chatbot"
End Sub
이제 위의 코드를 엑셀 VBA 프로젝트에 추가하고, API 키를 입력하고 실행하면 사용자가 입력한 메시지에 대한 GPT-3.5 API 응답을 받아 출력할 수 있습니다.
728x90
LIST