-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmdOpenAI_Tests.bas
216 lines (158 loc) · 6.84 KB
/
mdOpenAI_Tests.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
Attribute VB_Name = "mdOpenAI_TESTS"
'-----------------------------------------------------------------------------
' Project: OpenAI VBA Framework
' Module: mdOpenAI_Tests
' Description: Tests the framework is retrieving data correctly from OpenAI
'
' Author: Zaid Qureshi
' GitHub: https://github.com/zq99
'
' Classes / Modules in the Framework:
' - clsOpenAI
' - clsOpenAILogger
' - clsOpenAIMessage
' - clsOpenAIMessages
' - clsOpenAIRequest
' - clsOpenAIResponse
' - IOpenAINameProvider
'
' - mdOpenAI_Tests
' - mdOpenAI_Examples
' This work is licensed under the MIT License. The full license text
' can be found in the LICENSE file in the root of this repository.
'
'-----------------------------------------------------------------------------
Option Explicit
'******************************************************
' GET YOUR API KEY: https://openai.com/api/
Public Const API_KEY As String = "<API_KEY>"
'******************************************************
Public Sub RunAllTests()
'********************************************************************************
'Purpose: This tests all endpoints are being queried correctly and returning data
'********************************************************************************
Dim arrMSXMLTypes(1 To 3) As String
Dim oOpenAI As New clsOpenAI
oOpenAI.IsLogOutputRequired True
oOpenAI.API_KEY = API_KEY
oOpenAI.Log "Starting to run all tests"
Debug.Assert oOpenAI.API_KEY = API_KEY
Debug.Assert oOpenAI.CallsToAPICount = 0
' Assign all posssible MSXML types
arrMSXMLTypes(1) = Empty
arrMSXMLTypes(2) = oOpenAI.MSXML_XML_VALUE
arrMSXMLTypes(3) = oOpenAI.MSXML_SERVER_XML_VALUE
Dim i As Integer
' Loop through each item in the array
For i = LBound(arrMSXMLTypes) To UBound(arrMSXMLTypes)
DoEvents
oOpenAI.Log arrMSXMLTypes(i)
Call TestOpenAI(oOpenAI, arrMSXMLTypes(i))
oOpenAI.Pause
Next i
Debug.Assert oOpenAI.CallsToAPICount > 0
'Test for function which can be used as UDF in Excel
Call Test_GETTEXTFROMOPENAI
oOpenAI.Log "Completed run of all tests"
Set oOpenAI = Nothing
End Sub
Private Sub TestOpenAI(ByVal oOpenAI As clsOpenAI, Optional ByVal strRequestXMLType As String)
Dim oMessages As New clsOpenAIMessages
Dim oResponse As clsOpenAIResponse
Dim strMsg As String
If strRequestXMLType <> Empty Then
oOpenAI.MSXMLType = strRequestXMLType
Debug.Assert oOpenAI.MSXMLType = strRequestXMLType
End If
'Test temperature can be changed
oOpenAI.Temperature = 0.9
Debug.Assert oOpenAI.Temperature = 0.9
'Set least amount of variation for testing
oOpenAI.Temperature = 0
Debug.Assert oOpenAI.Temperature = 0
'*********************************************
'(1) Simple chat test
'*********************************************
'Test with different models
Dim arrModels(1 To 2) As String
Dim i As Integer
arrModels(1) = Empty
arrModels(2) = "gpt-4"
For i = LBound(arrModels) To UBound(arrModels)
DoEvents
If arrModels(i) <> Empty Then
oOpenAI.Model = arrModels(i)
Debug.Assert oOpenAI.Model = arrModels(i)
End If
oOpenAI.Log "Testing with model: " & IIf(oOpenAI.Model = Empty, "[None]", oOpenAI.Model)
oMessages.AddSystemMessage "Every answer should only contain alphanumeric characters, and every letter should be capitalized"
oMessages.AddUserMessage "What is the capital of France?"
Set oResponse = oOpenAI.ChatCompletion(oMessages)
Debug.Assert Not oResponse Is Nothing
Debug.Assert Len(oResponse.MessageContent) > 0
Debug.Assert oResponse.MessageContent = "PARIS"
Debug.Assert oResponse.MessageRole = "assistant"
oOpenAI.Log oMessages.GetAllMessages
oOpenAI.Log oResponse.MessageContent
oOpenAI.Log oResponse.MessageRole
oOpenAI.Pause 5000
Next i
'*********************************************
'(2) Simple chat test with temperature change
'*********************************************
oMessages.AddUserMessage "write a string of digits in order up to 9 starting with 1 and ending with 9"
Set oResponse = oOpenAI.ChatCompletion(oMessages)
Debug.Assert Not oResponse Is Nothing
Debug.Assert Len(oResponse.MessageContent) > 0
Debug.Assert oResponse.MessageContent = "123456789"
Debug.Assert oResponse.MessageRole = "assistant"
oOpenAI.Pause 5000
'*********************************************
'(3) Change timeouts
'*********************************************
oMessages.AddUserMessage "write a string of digits in order up to 9 starting with 1 and ending with 9"
oOpenAI.SetTimeOutDefaults 5000, 5000, 5000, 5000
Set oResponse = oOpenAI.ChatCompletion(oMessages)
Debug.Assert Not oResponse Is Nothing
Debug.Assert Len(oResponse.MessageContent) > 0
Debug.Assert oResponse.MessageContent = "123456789"
Debug.Assert oResponse.MessageRole = "assistant"
oOpenAI.Pause 5000
'*********************************************
'(4) Image creation from prompt test
'*********************************************
oOpenAI.ClearSettings
strMsg = "A cat playing a banjo on a surfboard"
Set oResponse = oOpenAI.CreateImageFromText(strMsg, 256, 256)
Debug.Assert Not oResponse Is Nothing
Debug.Assert Len(oResponse.SavedLocalFile) > 0
Debug.Assert Len(Dir(oResponse.SavedLocalFile)) > 0
Debug.Assert oResponse.IsExistSavedLocalFile = True
oOpenAI.Log ("Prompt=" & strMsg)
oOpenAI.Log ("Image saved to: " & oResponse.SavedLocalFile)
oOpenAI.Pause 5000
'*********************************************
' Tidy up
'*********************************************
Set oResponse = Nothing
Set oMessages = Nothing
End Sub
Private Sub Test_GETTEXTFROMOPENAI()
Dim strPrompt As String
Dim strAPIKey As String
Dim strModel As String
Dim strResult As String
' Example values for testing
strPrompt = "Hello, world!"
strAPIKey = API_KEY
strModel = "gpt-3.5-turbo" '"gpt-4" ' You can use a specific model if needed
' Call the function
strResult = GETTEXTFROMOPENAI(strPrompt, strAPIKey, strModel)
Debug.Assert Len(strResult) > 0
' Check if the result is as expected
If strResult <> Empty Then
Debug.Print "GETTEXTFROMOPENAI Test passed, received response: " & strResult
Else
Debug.Print "GETTEXTFROMOPENAI Test failed, no response received."
End If
End Sub