@@ -45,6 +45,10 @@ Private mobjRequest As clsOpenAIRequest
4545'Open AI defined constants
4646Private Const API_ENDPOINT_CHAT As String = "https://api.openai.com/v1/chat/completions"
4747Private Const API_ENDPOINT_COMPLETIONS As String = "https://api.openai.com/v1/completions"
48+ Private Const API_ENDPOINT_IMAGE_CREATION As String = "https://api.openai.com/v1/images/generations"
49+
50+ Private Const DEFAULT_LOCAL_LOCATION As String = "C:\Users\Public\Downloads\"
51+ Private mstrFolderToSave As String
4852
4953'More models can be found here: https://platform.openai.com/docs/models/overview
5054Private Const DEFAULT_CHAT_MODEL As String = "gpt-3.5-turbo"
@@ -102,6 +106,14 @@ Public Property Let PresencePenalty(ByVal value As Double)
102106 mobjRequest.PresencePenalty = value
103107End Property
104108
109+ Public Property Let FolderToSaveTo(ByVal value As String )
110+ mstrFolderToSave = value
111+ End Property
112+
113+ Public Property Get FolderToSaveTo() As String
114+ FolderToSaveTo = mstrFolderToSave
115+ End Property
116+
105117
106118Public Sub IsLogOutputRequired (ByVal value As Boolean )
107119'Purpose: Calling routines can switch off messages in this framework from appearing in the Immediate window
@@ -122,7 +134,7 @@ Public Sub Log(ByVal strMessage As String)
122134End Sub
123135
124136
125- Private Function GetResponseFromAPI (ByVal strRequestJson As String , ByVal strEndPoint As String ) As clsOpenAIResponse
137+ Private Function GetResponseFromAPI (ByVal strRequestJson As String , ByVal strEndPoint As String , Optional ByVal strLocalPath As String = DEFAULT_LOCAL_LOCATION ) As clsOpenAIResponse
126138'Purpose: This handles the request to OpenAI's API URL
127139
128140 Dim strResponseJson As String
@@ -142,37 +154,43 @@ On Error GoTo ERR_HANDLER:
142154 .Open "POST" , strEndPoint, False
143155 .SetRequestHeader "Content-Type" , "application/json"
144156 .SetRequestHeader "Authorization" , "Bearer " & mstrAPI_KEY
145- .send (strRequestJson)
157+ .Send (strRequestJson)
146158 End With
147159
148- ' completed
160+ ' unblock other processes if still querying OpenAI
149161 Do While mobjHttpRequest.readyState <> HTTP_REQUEST_COMPLETED
150162 DoEvents
151163 Loop
152-
153- Log "Response code from OpenAI API is: " & mobjHttpRequest.Status
154164
155165 If mobjHttpRequest.Status = HTTP_STATUS_OK Then
156166
157167 'get the json result from the successful request
158- strResponseJson = Trim(mobjHttpRequest.responseText )
168+ strResponseJson = Trim(mobjHttpRequest.ResponseText )
159169 Log strResponseJson
160- Set oResponse = New clsOpenAIResponse
161170
162171 'format the json result according to which api endpoint used
163172 If strEndPoint = API_ENDPOINT_CHAT Then
164173
165174 'ChatGPT and GPT4
175+ Set oResponse = New clsOpenAIResponse
166176 oResponse.ParseChatJSON (strResponseJson)
167177 Set GetResponseFromAPI = oResponse
168178
169179 ElseIf strEndPoint = API_ENDPOINT_COMPLETIONS Then
170180
171181 'GPT3 and earlier
182+ Set oResponse = New clsOpenAIResponse
172183 oResponse.ParseTextCompletionJSON (strResponseJson)
173184 Set GetResponseFromAPI = oResponse
174185
186+ ElseIf strEndPoint = API_ENDPOINT_IMAGE_CREATION Then
187+
188+ 'DALL-E image generator
189+ Set GetResponseFromAPI = GetResponseObjectForImageParse(strResponseJson)
190+
175191 End If
192+ Else
193+ mobjLogger.PrintCriticalMessage ("Failed to retrieve data from OpenAI. Response code is " & mobjHttpRequest.Status)
176194 End If
177195
178196EXIT_HERE:
@@ -185,6 +203,56 @@ ERR_HANDLER:
185203End Function
186204
187205
206+ Private Function GetResponseObjectForImageParse (ByVal strResponseJson As String )
207+
208+ Set GetResponseObjectForImageParse = Nothing
209+
210+ Dim oResponse As clsOpenAIResponse
211+ Dim strImageUrl As String
212+
213+ Set oResponse = New clsOpenAIResponse
214+ strImageUrl = oResponse.GetImageURLFromImageCreationJSON(strResponseJson)
215+
216+ If Len(strImageUrl) > 0 Then
217+
218+ Dim strFileName As String
219+ strFileName = oResponse.GetFileNameFromImageURL(strImageUrl)
220+
221+ If Len(strFileName) > 0 Then
222+
223+ Dim strFullName As String
224+ strFullName = mstrFolderToSave & strFileName
225+
226+ If Not mobjHttpRequest Is Nothing Then
227+
228+ mobjHttpRequest.Open "GET" , strImageUrl, False
229+ mobjHttpRequest.Send
230+
231+ 'convert the byte array to a saved image file
232+
233+ Dim objStream As Object
234+ Set objStream = CreateObject("ADODB.Stream" )
235+
236+ If Not objStream Is Nothing Then
237+ objStream.Open
238+ objStream.Type = 1
239+ objStream.write mobjHttpRequest.ResponseBody
240+ objStream.SaveToFile strFullName
241+ objStream.Close
242+ Set objStream = Nothing
243+
244+ If Len(Dir(strFullName)) > 0 Then
245+ oResponse.SavedLocalFile = strFullName
246+ End If
247+ Set GetResponseObjectForImageParse = oResponse
248+ End If
249+ End If
250+ End If
251+ End If
252+ Set oResponse = Nothing
253+ End Function
254+
255+
188256Private Function IsAPIKeyValid () As Boolean
189257'Purpose: Check a valid API key has been assigned
190258
@@ -264,6 +332,8 @@ Private Sub Class_Initialize()
264332 mobjLogger.IsMessageRequired = False
265333 mobjLogger.SetClass Me
266334
335+ mstrFolderToSave = DEFAULT_LOCAL_LOCATION
336+
267337 mstrAPI_KEY = Empty
268338
269339End Sub
@@ -289,6 +359,8 @@ Private Function GetDefaultRequestSettings() As clsOpenAIRequest
289359 .Temperature = 0.5
290360 .FrequencyPenalty = 0
291361 .PresencePenalty = 0
362+ .ImageHeight = 256
363+ .ImageWidth = 256
292364 End With
293365 Set GetDefaultRequestSettings = oRequest
294366
@@ -303,6 +375,7 @@ Public Sub ClearSettings()
303375
304376End Sub
305377
378+
306379Public Function GetReadAPIKeyFromFolder (ByVal strfolderPath As String ) As String
307380'Purpose: Allows retrieval of an API KEY saved in an external file (possibly stored on a drive only the current user can access)
308381
@@ -328,3 +401,27 @@ Public Function GetReadAPIKeyFromFolder(ByVal strfolderPath As String) As String
328401End Function
329402
330403
404+ Public Function CreateImageFromText (ByVal strPrompt As String , ByVal lngWidth As Long , ByVal lngHeight As Long ) As clsOpenAIResponse
405+ 'Purpose: This is for OpenAI's image creation
406+
407+ Set CreateImageFromText = Nothing
408+
409+ If Not IsAPIKeyValid Then
410+ mobjLogger.PrintCriticalMessage MESSAGE_INVALID_API_KEY, True
411+ Exit Function
412+ End If
413+
414+ If mobjHttpRequest Is Nothing Or strPrompt = Empty Then
415+ Exit Function
416+ End If
417+
418+ mobjRequest.prompt = strPrompt
419+ mobjRequest.ImageHeight = lngHeight
420+ mobjRequest.ImageWidth = lngWidth
421+
422+ Log mobjRequest.GetDalleImageSendToAPIJsonString
423+
424+ Set CreateImageFromText = GetResponseFromAPI(mobjRequest.GetDalleImageSendToAPIJsonString, API_ENDPOINT_IMAGE_CREATION, DEFAULT_LOCAL_LOCATION)
425+
426+ End Function
427+
0 commit comments