@@ -42,7 +42,7 @@ Private mobjHttpRequest As Object
4242Private mobjLogger As clsOpenAILogger
4343Private mobjRequest As clsOpenAIRequest
4444
45- 'Open AI defined constants
45+ 'OpenAI API Endpoints
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"
4848Private 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"
5757Private Const DEFAULT_CHAT_TOKENS_COUNT As Integer = 512
5858Private Const DEFAULT_TEXT_TOKENS_COUNT As Integer = 1024
5959
60+ 'Project constants
6061Private Const UNASSIGNED_VALUE As Integer = -1
6162Private Const MESSAGE_INVALID_API_KEY As String = "An OpenAI API key is either invalid or has not been specified!"
6263Private Const HTTP_STATUS_OK As Long = 200 ' OK
6364Private Const HTTP_REQUEST_COMPLETED As Integer = 4
6465
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
6571
6672Private Function IOpenAINameProvider_GetClassName () As String
6773 IOpenAINameProvider_GetClassName = "clsOpenAI"
@@ -79,6 +85,31 @@ Public Property Get API_KEY() As String
7985 API_KEY = mstrAPI_KEY
8086End Property
8187
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+
82113Public Property Let Model(ByVal value As String )
83114 mobjRequest.Model = value
84115End Property
@@ -145,16 +176,21 @@ On Error GoTo ERR_HANDLER:
145176 'default return value
146177 Set GetResponseFromAPI = Nothing
147178
148- If mobjHttpRequest Is Nothing Then
149- GoTo EXIT_HERE
150- End If
179+ Set mobjHttpRequest = CreateObject(mstrMSXMLType)
151180
152181 'talk to OpenAI
153182 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+
154189 .Open "POST" , strEndPoint, False
155190 .SetRequestHeader "Content-Type" , "application/json"
156191 .SetRequestHeader "Authorization" , "Bearer " & mstrAPI_KEY
157192 .Send (strRequestJson)
193+
158194 End With
159195
160196 ' unblock other processes if still querying OpenAI
@@ -271,7 +307,7 @@ Public Function ChatCompletion(ByVal oMessages As clsOpenAIMessages) As clsOpenA
271307 Exit Function
272308 End If
273309
274- If mobjHttpRequest Is Nothing Or oMessages Is Nothing Then
310+ If oMessages Is Nothing Then
275311 Exit Function
276312 End If
277313
@@ -302,7 +338,7 @@ Public Function TextCompletion(ByVal strPrompt As String) As clsOpenAIResponse
302338 Exit Function
303339 End If
304340
305- If mobjHttpRequest Is Nothing Or strPrompt = Empty Then
341+ If strPrompt = Empty Then
306342 Exit Function
307343 End If
308344
@@ -325,7 +361,7 @@ End Function
325361
326362Private Sub Class_Initialize ()
327363
328- Set mobjHttpRequest = CreateObject( "MSXML2.XMLHTTP" )
364+ mstrMSXMLType = MSXML_DEFAULT
329365 Set mobjRequest = GetDefaultRequestSettings
330366
331367 Set mobjLogger = New clsOpenAILogger
@@ -361,13 +397,27 @@ Private Function GetDefaultRequestSettings() As clsOpenAIRequest
361397 .PresencePenalty = 0
362398 .ImageHeight = 256
363399 .ImageWidth = 256
400+ .TimeoutConnect = 30000
401+ .TimeoutReceive = 30000
402+ .TimeoutResolve = 30000
403+ .TimeoutSend = 60000
364404 End With
365405 Set GetDefaultRequestSettings = oRequest
366406
367407 Set oRequest = Nothing
368408End Function
369409
370410
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+
371421Public Sub ClearSettings ()
372422'Purpose: Reset the settings if switching between endpoints
373423
@@ -411,7 +461,7 @@ Public Function CreateImageFromText(ByVal strPrompt As String, ByVal lngWidth As
411461 Exit Function
412462 End If
413463
414- If mobjHttpRequest Is Nothing Or strPrompt = Empty Then
464+ If strPrompt = Empty Then
415465 Exit Function
416466 End If
417467
0 commit comments