Skip to content

Commit c13bfe2

Browse files
authored
Add files via upload
1 parent 28d3910 commit c13bfe2

File tree

7 files changed

+112
-36
lines changed

7 files changed

+112
-36
lines changed

OpenAIFrameworkDemo.xlsm

5.83 KB
Binary file not shown.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ For coding issues, use the following line, to go through the code.
155155
oOpenAI.IsLogOutputRequired True
156156
```
157157

158+
The entire framework can be tested for breaking changes using the mdTest_OpenAI module. Simply run the proceedure "RunAllTests".
159+
158160
## Contributing
159161
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
160162

clsOpenAI.cls

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,19 @@ Attribute VB_Exposed = False
3434

3535
Option Explicit
3636

37+
#If VBA7 Then
38+
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
39+
#Else
40+
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
41+
#End If
42+
3743
Implements IOpenAINameProvider
3844

3945
Private mstrAPI_KEY As String
4046
Private mobjHttpRequest As Object
4147
Private mobjLogger As clsOpenAILogger
4248
Private mobjRequest As clsOpenAIRequest
49+
Private mlngCallsToAPICount As Long
4350

4451
'OpenAI API Endpoints
4552
Private Const API_ENDPOINT_CHAT As String = "https://api.openai.com/v1/chat/completions"
@@ -113,6 +120,14 @@ Public Property Let Model(ByVal value As String)
113120
mobjRequest.Model = value
114121
End Property
115122

123+
Public Property Get Model() As String
124+
Model = mobjRequest.Model
125+
End Property
126+
127+
Public Property Get CallsToAPICount() As Long
128+
CallsToAPICount = mlngCallsToAPICount
129+
End Property
130+
116131
Public Property Let MaxTokens(ByVal value As Long)
117132
mobjRequest.MaxTokens = value
118133
End Property
@@ -128,6 +143,10 @@ Public Property Let Temperature(ByVal value As Double)
128143
mobjRequest.Temperature = value
129144
End Property
130145

146+
Public Property Get Temperature() As Double
147+
Temperature = mobjRequest.Temperature
148+
End Property
149+
131150
Public Property Let FrequencyPenalty(ByVal value As Double)
132151
mobjRequest.FrequencyPenalty = value
133152
End Property
@@ -144,6 +163,10 @@ Public Property Get FolderToSaveTo() As String
144163
FolderToSaveTo = mstrFolderToSave
145164
End Property
146165

166+
Public Sub Pause(Optional ByVal lngMilliSeconds As Long = 1000)
167+
mobjLogger.PrintMessage ("Pausing for " & CStr(lngMilliSeconds) & " milliseconds")
168+
Sleep lngMilliSeconds
169+
End Sub
147170

148171
Public Sub IsLogOutputRequired(ByVal value As Boolean)
149172
'Purpose: Calling routines can switch off messages in this framework from appearing in the Immediate window
@@ -199,6 +222,8 @@ On Error GoTo ERR_HANDLER:
199222

200223
If mobjHttpRequest.Status = HTTP_STATUS_OK Then
201224

225+
mlngCallsToAPICount = mlngCallsToAPICount + 1
226+
202227
'get the json result from the successful request
203228
strResponseJson = Trim(mobjHttpRequest.ResponseText)
204229
Log strResponseJson
@@ -239,6 +264,8 @@ End Function
239264

240265

241266
Private Function GetResponseObjectForImageParse(ByVal strResponseJson As String)
267+
'Purpose: this takes the response Json from the OpenAI api, and saves the information as a picture
268+
' on the local PC, it then returns a response object with a reference to the picture created
242269

243270
Set GetResponseObjectForImageParse = Nothing
244271

@@ -368,6 +395,7 @@ Private Sub Class_Initialize()
368395
mobjLogger.SetClass Me
369396

370397
mstrFolderToSave = DEFAULT_LOCAL_LOCATION
398+
mlngCallsToAPICount = 0
371399

372400
mstrAPI_KEY = Empty
373401

@@ -402,6 +430,7 @@ Private Function GetDefaultRequestSettings() As clsOpenAIRequest
402430
.TimeoutSend = 60000
403431
End With
404432
Set GetDefaultRequestSettings = oRequest
433+
mlngCallsToAPICount = 0
405434

406435
Set oRequest = Nothing
407436
End Function

clsOpenAIRequest.cls

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ Public Property Let Temperature(ByVal value As Double)
106106
mdblTemperature = value
107107
End Property
108108

109+
Public Property Get Temperature() As Double
110+
Temperature = mdblTemperature
111+
End Property
112+
109113
Public Property Let FrequencyPenalty(ByVal value As Double)
110114
mdblFrequencyPenalty = value
111115
End Property

clsOpenAIResponse.cls

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,26 @@ Public Property Let SavedLocalFile(ByVal value As String)
128128
mstrSavedFile = value
129129
End Property
130130

131+
Public Function IsExistSavedLocalFile() As Boolean
132+
IsExistSavedLocalFile = IIf(Len(Dir(mstrSavedFile)) > 0, True, False)
133+
End Function
131134

132135
Private Sub Class_Initialize()
133136
' Initialize the variables
134-
mstrId = ""
135-
mstrObject = ""
137+
mstrId = Empty
138+
mstrObject = Empty
136139
mstrCreated = 0
137-
mstrModel = ""
140+
mstrModel = Empty
138141
mintPromptTokens = 0
139142
mintCompletionTokens = 0
140143
mintTotalTokens = 0
141-
mstrMessageRole = ""
142-
mstrMessageContent = ""
143-
mstrFinishReason = ""
144+
mstrMessageRole = Empty
145+
mstrMessageContent = Empty
146+
mstrFinishReason = Empty
144147
mintIndex = 0
145-
mstrText = ""
146-
mstrLogprobs = ""
147-
mstrSavedFile = ""
148+
mstrText = Empty
149+
mstrLogprobs = Empty
150+
mstrSavedFile = Empty
148151
End Sub
149152

150153

mdOpenAI_Examples.bas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Attribute VB_Name = "mdOpenAI_Examples"
77
' Author: Zaid Qureshi
88
' GitHub: https://github.com/zq99
99
'
10-
' Modules in the Framework:
10+
' Classes / Modules in the Framework:
1111
' - clsOpenAI
1212
' - clsOpenAILogger
1313
' - clsOpenAIMessage

mdOpenAI_Tests.bas

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Attribute VB_Name = "mdOpenAI_TESTS"
77
' Author: Zaid Qureshi
88
' GitHub: https://github.com/zq99
99
'
10-
' Modules in the Framework:
10+
' Classes / Modules in the Framework:
1111
' - clsOpenAI
1212
' - clsOpenAILogger
1313
' - clsOpenAIMessage
@@ -26,12 +26,6 @@ Attribute VB_Name = "mdOpenAI_TESTS"
2626

2727
Option Explicit
2828

29-
#If VBA7 Then
30-
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
31-
#Else
32-
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
33-
#End If
34-
3529
'******************************************************
3630
' GET YOUR API KEY: https://openai.com/api/
3731
Public Const API_KEY As String = "<API_KEY>"
@@ -48,23 +42,26 @@ Public Sub RunAllTests()
4842

4943
oOpenAI.IsLogOutputRequired True
5044
oOpenAI.API_KEY = API_KEY
45+
46+
Debug.Assert oOpenAI.API_KEY = API_KEY
47+
Debug.Assert oOpenAI.CallsToAPICount = 0
5148

5249
' Assign all posssible MSXML types
5350
arrMSXMLTypes(1) = Empty
5451
arrMSXMLTypes(2) = oOpenAI.MSXML_XML_VALUE
5552
arrMSXMLTypes(3) = oOpenAI.MSXML_SERVER_XML_VALUE
5653

57-
' Declare a variable for the loop index
5854
Dim i As Integer
5955

6056
' Loop through each item in the array
6157
For i = LBound(arrMSXMLTypes) To UBound(arrMSXMLTypes)
6258
DoEvents
6359
oOpenAI.Log arrMSXMLTypes(i)
6460
Call TestOpenAI(oOpenAI, arrMSXMLTypes(i))
65-
Sleep 1000
61+
oOpenAI.Pause
6662
Next i
67-
63+
64+
Debug.Assert oOpenAI.CallsToAPICount > 0
6865
Set oOpenAI = Nothing
6966

7067
End Sub
@@ -76,43 +73,72 @@ Private Sub TestOpenAI(ByVal oOpenAI As clsOpenAI, Optional ByVal strRequestXMLT
7673
Dim oResponse As clsOpenAIResponse
7774

7875
If strRequestXMLType <> Empty Then
79-
oOpenAI.MSXMLType = oOpenAI.MSXML_SERVER_XML_VALUE
76+
oOpenAI.MSXMLType = strRequestXMLType
77+
Debug.Assert oOpenAI.MSXMLType = strRequestXMLType
8078
End If
8179

82-
'All output to sent to immediate window
80+
'Test temperature can be changed
81+
oOpenAI.Temperature = 0.9
82+
Debug.Assert oOpenAI.Temperature = 0.9
83+
84+
'Set least amount of variation for testing
8385
oOpenAI.Temperature = 0
86+
Debug.Assert oOpenAI.Temperature = 0
8487

8588
'*********************************************
8689
'(1) Simple chat test
8790
'*********************************************
8891

89-
oMessages.AddSystemMessage "Every answer should only contain alphanumeric characters, and every letter should be capitalized"
90-
oMessages.AddUserMessage "What is the capital of France?"
91-
92-
Set oResponse = oOpenAI.ChatCompletion(oMessages)
92+
'Test with different models
93+
Dim arrModels(1 To 2) As String
94+
Dim i As Integer
9395

94-
Debug.Assert Not oResponse Is Nothing
95-
Debug.Assert Len(oResponse.MessageContent) > 0
96-
Debug.Assert oResponse.MessageContent = "PARIS"
97-
Debug.Assert oResponse.MessageRole = "assistant"
96+
arrModels(1) = Empty
97+
arrModels(2) = "gpt-4"
98+
99+
For i = LBound(arrModels) To UBound(arrModels)
100+
101+
DoEvents
98102

99-
oOpenAI.Log oMessages.GetAllMessages
100-
oOpenAI.Log oResponse.MessageContent
101-
oOpenAI.Log oResponse.MessageRole
103+
If arrModels(i) <> Empty Then
104+
oOpenAI.Model = arrModels(i)
105+
Debug.Assert oOpenAI.Model = arrModels(i)
106+
End If
107+
108+
oOpenAI.Log "Testing with model: " & IIf(oOpenAI.Model = Empty, "[None]", oOpenAI.Model)
109+
110+
oMessages.AddSystemMessage "Every answer should only contain alphanumeric characters, and every letter should be capitalized"
111+
oMessages.AddUserMessage "What is the capital of France?"
112+
113+
Set oResponse = oOpenAI.ChatCompletion(oMessages)
114+
115+
Debug.Assert Not oResponse Is Nothing
116+
Debug.Assert Len(oResponse.MessageContent) > 0
117+
Debug.Assert oResponse.MessageContent = "PARIS"
118+
Debug.Assert oResponse.MessageRole = "assistant"
119+
120+
oOpenAI.Log oMessages.GetAllMessages
121+
oOpenAI.Log oResponse.MessageContent
122+
oOpenAI.Log oResponse.MessageRole
123+
124+
oOpenAI.Pause
125+
126+
Next i
102127

103128
'*********************************************
104129
'(2) Simple chat test with temperature change
105130
'*********************************************
106131

107-
oMessages.AddUserMessage "write a string of digits in order up to 9"
108-
oOpenAI.Temperature = 0.9
132+
oMessages.AddUserMessage "write a string of digits in order up to 9 starting with 1 and ending with 9"
109133
Set oResponse = oOpenAI.ChatCompletion(oMessages)
110134

111135
Debug.Assert Not oResponse Is Nothing
112136
Debug.Assert Len(oResponse.MessageContent) > 0
113137
Debug.Assert oResponse.MessageContent = "123456789"
114138
Debug.Assert oResponse.MessageRole = "assistant"
115139

140+
oOpenAI.Pause
141+
116142
'*********************************************
117143
'(3) Change timeouts
118144
'*********************************************
@@ -126,6 +152,8 @@ Private Sub TestOpenAI(ByVal oOpenAI As clsOpenAI, Optional ByVal strRequestXMLT
126152
Debug.Assert oResponse.MessageContent = "123456789"
127153
Debug.Assert oResponse.MessageRole = "assistant"
128154

155+
oOpenAI.Pause
156+
129157
'*********************************************
130158
'(4) Text completion test
131159
'*********************************************
@@ -134,6 +162,7 @@ Private Sub TestOpenAI(ByVal oOpenAI As clsOpenAI, Optional ByVal strRequestXMLT
134162

135163
'reset to default
136164
oOpenAI.ClearSettings
165+
Debug.Assert oOpenAI.CallsToAPICount = 0
137166

138167
strMsg = "Write a Haiku about a dinosaur that loves to code in VBA"
139168
Set oResponse = oOpenAI.TextCompletion(strMsg)
@@ -142,16 +171,25 @@ Private Sub TestOpenAI(ByVal oOpenAI As clsOpenAI, Optional ByVal strRequestXMLT
142171
Debug.Assert Len(oResponse.TextContent) > 0
143172
oOpenAI.Log (oResponse.TextContent)
144173

174+
oOpenAI.Pause
175+
145176
'*********************************************
146177
'(5) Image creation from prompt test
147178
'*********************************************
148179

149180
oOpenAI.ClearSettings
150-
Set oResponse = oOpenAI.CreateImageFromText("A cat playing a banjo on a surfboard", 256, 256)
181+
182+
strMsg = "A cat playing a banjo on a surfboard"
183+
Set oResponse = oOpenAI.CreateImageFromText(strMsg, 256, 256)
151184

152185
Debug.Assert Not oResponse Is Nothing
153186
Debug.Assert Len(oResponse.SavedLocalFile) > 0
154187
Debug.Assert Len(Dir(oResponse.SavedLocalFile)) > 0
188+
Debug.Assert oResponse.IsExistSavedLocalFile = True
189+
190+
oOpenAI.Log ("Prompt=" & strMsg)
191+
oOpenAI.Log ("Image saved to: " & oResponse.SavedLocalFile)
192+
oOpenAI.Pause
155193

156194
Set oResponse = Nothing
157195
Set oMessages = Nothing

0 commit comments

Comments
 (0)