Skip to content

Commit a06e59c

Browse files
authored
Add files via upload
1 parent ca63e78 commit a06e59c

5 files changed

+155
-19
lines changed

OpenAIFrameworkDemo.xlsm

28.2 KB
Binary file not shown.

clsOpenAI.cls

+58-8
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Private mobjHttpRequest As Object
4242
Private mobjLogger As clsOpenAILogger
4343
Private mobjRequest As clsOpenAIRequest
4444

45-
'Open AI defined constants
45+
'OpenAI API Endpoints
4646
Private Const API_ENDPOINT_CHAT As String = "https://api.openai.com/v1/chat/completions"
4747
Private Const API_ENDPOINT_COMPLETIONS As String = "https://api.openai.com/v1/completions"
4848
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"
5757
Private Const DEFAULT_CHAT_TOKENS_COUNT As Integer = 512
5858
Private Const DEFAULT_TEXT_TOKENS_COUNT As Integer = 1024
5959

60+
'Project constants
6061
Private Const UNASSIGNED_VALUE As Integer = -1
6162
Private Const MESSAGE_INVALID_API_KEY As String = "An OpenAI API key is either invalid or has not been specified!"
6263
Private Const HTTP_STATUS_OK As Long = 200 ' OK
6364
Private 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

6672
Private 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
8086
End 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+
82113
Public Property Let Model(ByVal value As String)
83114
mobjRequest.Model = value
84115
End 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

326362
Private 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
368408
End 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+
371421
Public 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

clsOpenAIRequest.cls

+38
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ Private mstrPrompt As String
4848
Private mlngImageWidth As Long
4949
Private mlngImageHeight As Long
5050

51+
'Resolve, Connect, Send and Receive
52+
Private mlngTimeoutResolve As Long
53+
Private mlngTimeoutConnect As Long
54+
Private mlngTimeoutSend As Long
55+
Private mlngTimeoutReceive As Long
56+
5157

5258
Private Function IOpenAINameProvider_GetClassName() As String
5359
IOpenAINameProvider_GetClassName = "clsOpenAIRequest"
@@ -125,6 +131,38 @@ Public Property Let ImageWidth(ByVal value As Long)
125131
mlngImageWidth = value
126132
End Property
127133

134+
Public Property Let TimeoutResolve(ByVal value As Long)
135+
mlngTimeoutResolve = value
136+
End Property
137+
138+
Public Property Get TimeoutResolve() As Long
139+
TimeoutResolve = mlngTimeoutResolve
140+
End Property
141+
142+
Public Property Let TimeoutConnect(ByVal value As Long)
143+
mlngTimeoutConnect = value
144+
End Property
145+
146+
Public Property Get TimeoutConnect() As Long
147+
TimeoutConnect = mlngTimeoutConnect
148+
End Property
149+
150+
Public Property Let TimeoutSend(ByVal value As Long)
151+
mlngTimeoutSend = value
152+
End Property
153+
154+
Public Property Get TimeoutSend() As Long
155+
TimeoutSend = mlngTimeoutSend
156+
End Property
157+
158+
Public Property Let TimeoutReceive(ByVal value As Long)
159+
mlngTimeoutReceive = value
160+
End Property
161+
162+
Public Property Get TimeoutReceive() As Long
163+
TimeoutReceive = mlngTimeoutReceive
164+
End Property
165+
128166

129167
Public Function GetChatSendToAPIJsonString() As String
130168
GetChatSendToAPIJsonString = "{""model"": """ & mstrModel & """, " & mobjMessages.GetAllMessages & ", ""max_tokens"": " & mlngMaxTokens & ", ""top_p"": " & mdblTopP & ", ""temperature"": " & mdblTemperature & ", ""frequency_penalty"": " & mdblFrequencyPenalty & ", ""presence_penalty"": " & mdlPresencePenalty & "}"

mdOpenAI_Examples.bas

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Public Sub TestTextCompletionOpenAI()
108108

109109
oOpenAI.API_KEY = API_KEY
110110

111-
sMsg = "Write a Haiku about a Dinosaur that loves to code!"
111+
sMsg = "Write a Haiku about a dinosaur that loves to code!"
112112
Set oResponse = oOpenAI.TextCompletion(sMsg)
113113

114114
If Not oResponse Is Nothing Then
@@ -140,7 +140,7 @@ Public Sub TestTextCompletionSimpleOpenAI()
140140

141141
oOpenAI.API_KEY = API_KEY
142142

143-
Set oResponse = oOpenAI.TextCompletion("Write a Haiku about a Dinosaur that loves to code!")
143+
Set oResponse = oOpenAI.TextCompletion("Write a Haiku about a dinosaur that loves to code!")
144144

145145
If Not oResponse Is Nothing Then
146146
Debug.Print (oResponse.TextContent)

mdOpenAI_Tests.bas

+57-9
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,60 @@ Attribute VB_Name = "mdOpenAI_TESTS"
2727

2828
Option Explicit
2929

30+
#If VBA7 Then
31+
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
32+
#Else
33+
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
34+
#End If
35+
3036
'******************************************************
3137
' GET YOUR API KEY: https://openai.com/api/
3238
Public Const API_KEY As String = "<API_KEY>"
3339
'******************************************************
3440

3541

36-
Public Sub TestOpenAI()
42+
Public Sub RunAllTests()
43+
'********************************************************************************
3744
'Purpose: This tests all endpoints are being queried correctly and returning data
45+
'********************************************************************************
46+
47+
Dim arrMSXMLTypes(1 To 3) As String
48+
Dim oOpenAI As New clsOpenAI
49+
50+
oOpenAI.IsLogOutputRequired True
51+
oOpenAI.API_KEY = API_KEY
52+
53+
' Assign all posssible MSXML types
54+
arrMSXMLTypes(1) = Empty
55+
arrMSXMLTypes(2) = oOpenAI.MSXML_XML_VALUE
56+
arrMSXMLTypes(3) = oOpenAI.MSXML_SERVER_XML_VALUE
57+
58+
' Declare a variable for the loop index
59+
Dim i As Integer
60+
61+
' Loop through each item in the array
62+
For i = LBound(arrMSXMLTypes) To UBound(arrMSXMLTypes)
63+
DoEvents
64+
oOpenAI.Log arrMSXMLTypes(i)
65+
Call TestOpenAI(oOpenAI, arrMSXMLTypes(i))
66+
Sleep 1000
67+
Next i
68+
69+
Set oOpenAI = Nothing
70+
71+
End Sub
72+
73+
74+
Private Sub TestOpenAI(ByVal oOpenAI As clsOpenAI, Optional ByVal strRequestXMLType As String)
3875

39-
Dim oOpenAI As clsOpenAI
4076
Dim oMessages As New clsOpenAIMessages
4177
Dim oResponse As clsOpenAIResponse
42-
43-
Set oOpenAI = New clsOpenAI
78+
79+
If strRequestXMLType <> Empty Then
80+
oOpenAI.MSXMLType = oOpenAI.MSXML_SERVER_XML_VALUE
81+
End If
4482

4583
'All output to sent to immediate window
46-
oOpenAI.IsLogOutputRequired True
47-
oOpenAI.API_KEY = API_KEY
4884
oOpenAI.Temperature = 0
4985

5086
'*********************************************
@@ -70,7 +106,20 @@ Public Sub TestOpenAI()
70106
'*********************************************
71107

72108
oMessages.AddUserMessage "write a string of digits in order up to 9"
109+
oOpenAI.Temperature = 0.9
110+
Set oResponse = oOpenAI.ChatCompletion(oMessages)
111+
112+
Debug.Assert Not oResponse Is Nothing
113+
Debug.Assert Len(oResponse.MessageContent) > 0
114+
Debug.Assert oResponse.MessageContent = "123456789"
115+
Debug.Assert oResponse.MessageRole = "assistant"
116+
117+
'*********************************************
118+
'(3) Change timeouts
119+
'*********************************************
73120

121+
oMessages.AddUserMessage "write a string of digits in order up to 9"
122+
oOpenAI.SetTimeOutDefaults 5000, 5000, 5000, 5000
74123
Set oResponse = oOpenAI.ChatCompletion(oMessages)
75124

76125
Debug.Assert Not oResponse Is Nothing
@@ -79,7 +128,7 @@ Public Sub TestOpenAI()
79128
Debug.Assert oResponse.MessageRole = "assistant"
80129

81130
'*********************************************
82-
'(3) Text completion test
131+
'(4) Text completion test
83132
'*********************************************
84133

85134
Dim strMsg As String
@@ -95,7 +144,7 @@ Public Sub TestOpenAI()
95144
oOpenAI.Log (oResponse.TextContent)
96145

97146
'*********************************************
98-
'(4) Image creation from prompt test
147+
'(5) Image creation from prompt test
99148
'*********************************************
100149

101150
oOpenAI.ClearSettings
@@ -106,7 +155,6 @@ Public Sub TestOpenAI()
106155
Debug.Assert Len(Dir(oResponse.SavedLocalFile)) > 0
107156

108157
Set oResponse = Nothing
109-
Set oOpenAI = Nothing
110158
Set oMessages = Nothing
111159

112160
End Sub

0 commit comments

Comments
 (0)