@@ -42,7 +42,7 @@ Private mobjHttpRequest As Object
42
42
Private mobjLogger As clsOpenAILogger
43
43
Private mobjRequest As clsOpenAIRequest
44
44
45
- 'Open AI defined constants
45
+ 'OpenAI API Endpoints
46
46
Private Const API_ENDPOINT_CHAT As String = "https://api.openai.com/v1/chat/completions"
47
47
Private Const API_ENDPOINT_COMPLETIONS As String = "https://api.openai.com/v1/completions"
48
48
Private Const API_ENDPOINT_IMAGE_CREATION As String = "https://api.openai.com/v1/images/generations"
@@ -57,11 +57,17 @@ Private Const DEFAULT_TEXT_COMPLETION_MODEL As String = "text-davinci-003"
57
57
Private Const DEFAULT_CHAT_TOKENS_COUNT As Integer = 512
58
58
Private Const DEFAULT_TEXT_TOKENS_COUNT As Integer = 1024
59
59
60
+ 'Project constants
60
61
Private Const UNASSIGNED_VALUE As Integer = -1
61
62
Private Const MESSAGE_INVALID_API_KEY As String = "An OpenAI API key is either invalid or has not been specified!"
62
63
Private Const HTTP_STATUS_OK As Long = 200 ' OK
63
64
Private Const HTTP_REQUEST_COMPLETED As Integer = 4
64
65
66
+ 'This allows configuration of different HHTP Requests
67
+ Private Const MSXML_XML As String = "MSXML2.XMLHTTP"
68
+ Private Const MSXML_SERVER_XML As String = "MSXML2.ServerXMLHTTP"
69
+ Private Const MSXML_DEFAULT As String = MSXML_XML
70
+ Private mstrMSXMLType As String
65
71
66
72
Private Function IOpenAINameProvider_GetClassName () As String
67
73
IOpenAINameProvider_GetClassName = "clsOpenAI"
@@ -79,6 +85,31 @@ Public Property Get API_KEY() As String
79
85
API_KEY = mstrAPI_KEY
80
86
End Property
81
87
88
+ Public Property Let MSXMLType(ByVal value As String )
89
+ 'This allows calling proceedures to change the default type of XML HTTP Request
90
+
91
+ 'These are the only values allowed for this
92
+ If (value <> Me.MSXML_SERVER_XML_VALUE) And (value <> Me.MSXML_XML_VALUE) Then
93
+ Call mobjLogger .PrintCriticalMessage ("Invalid MSXML type specified!" )
94
+ Else
95
+ mstrMSXMLType = value
96
+ End If
97
+ End Property
98
+
99
+ Public Property Get MSXMLType() As String
100
+ MSXMLType = mstrMSXMLType
101
+ End Property
102
+
103
+ 'This method allows for the MSXML_XML constant to be accessible outside of the class
104
+ Public Property Get MSXML_XML_VALUE() As String
105
+ MSXML_XML_VALUE = MSXML_XML
106
+ End Property
107
+
108
+ 'This method allows for the MSXML_SERVER_XML constant to be accessible outside of the class
109
+ Public Property Get MSXML_SERVER_XML_VALUE() As String
110
+ MSXML_SERVER_XML_VALUE = MSXML_SERVER_XML
111
+ End Property
112
+
82
113
Public Property Let Model(ByVal value As String )
83
114
mobjRequest.Model = value
84
115
End Property
@@ -145,16 +176,21 @@ On Error GoTo ERR_HANDLER:
145
176
'default return value
146
177
Set GetResponseFromAPI = Nothing
147
178
148
- If mobjHttpRequest Is Nothing Then
149
- GoTo EXIT_HERE
150
- End If
179
+ Set mobjHttpRequest = CreateObject(mstrMSXMLType)
151
180
152
181
'talk to OpenAI
153
182
With mobjHttpRequest
183
+
184
+ If mstrMSXMLType = MSXML_SERVER_XML Then
185
+ .setTimeouts mobjRequest.TimeoutResolve, mobjRequest.TimeoutConnect, _
186
+ mobjRequest.TimeoutSend, mobjRequest.TimeoutReceive
187
+ End If
188
+
154
189
.Open "POST" , strEndPoint, False
155
190
.SetRequestHeader "Content-Type" , "application/json"
156
191
.SetRequestHeader "Authorization" , "Bearer " & mstrAPI_KEY
157
192
.Send (strRequestJson)
193
+
158
194
End With
159
195
160
196
' unblock other processes if still querying OpenAI
@@ -271,7 +307,7 @@ Public Function ChatCompletion(ByVal oMessages As clsOpenAIMessages) As clsOpenA
271
307
Exit Function
272
308
End If
273
309
274
- If mobjHttpRequest Is Nothing Or oMessages Is Nothing Then
310
+ If oMessages Is Nothing Then
275
311
Exit Function
276
312
End If
277
313
@@ -302,7 +338,7 @@ Public Function TextCompletion(ByVal strPrompt As String) As clsOpenAIResponse
302
338
Exit Function
303
339
End If
304
340
305
- If mobjHttpRequest Is Nothing Or strPrompt = Empty Then
341
+ If strPrompt = Empty Then
306
342
Exit Function
307
343
End If
308
344
@@ -325,7 +361,7 @@ End Function
325
361
326
362
Private Sub Class_Initialize ()
327
363
328
- Set mobjHttpRequest = CreateObject( "MSXML2.XMLHTTP" )
364
+ mstrMSXMLType = MSXML_DEFAULT
329
365
Set mobjRequest = GetDefaultRequestSettings
330
366
331
367
Set mobjLogger = New clsOpenAILogger
@@ -361,13 +397,27 @@ Private Function GetDefaultRequestSettings() As clsOpenAIRequest
361
397
.PresencePenalty = 0
362
398
.ImageHeight = 256
363
399
.ImageWidth = 256
400
+ .TimeoutConnect = 30000
401
+ .TimeoutReceive = 30000
402
+ .TimeoutResolve = 30000
403
+ .TimeoutSend = 60000
364
404
End With
365
405
Set GetDefaultRequestSettings = oRequest
366
406
367
407
Set oRequest = Nothing
368
408
End Function
369
409
370
410
411
+ Public Sub SetTimeOutDefaults (ByVal lngConnect As Long , ByVal lngReceive As Long , ByVal lngResolve As Long , ByVal lngSend As Long )
412
+ If Not mobjRequest Is Nothing Then
413
+ mobjRequest.TimeoutConnect = lngConnect
414
+ mobjRequest.TimeoutReceive = lngReceive
415
+ mobjRequest.TimeoutResolve = lngResolve
416
+ mobjRequest.TimeoutSend = lngSend
417
+ End If
418
+ End Sub
419
+
420
+
371
421
Public Sub ClearSettings ()
372
422
'Purpose: Reset the settings if switching between endpoints
373
423
@@ -411,7 +461,7 @@ Public Function CreateImageFromText(ByVal strPrompt As String, ByVal lngWidth As
411
461
Exit Function
412
462
End If
413
463
414
- If mobjHttpRequest Is Nothing Or strPrompt = Empty Then
464
+ If strPrompt = Empty Then
415
465
Exit Function
416
466
End If
417
467
0 commit comments