-
Notifications
You must be signed in to change notification settings - Fork 4
/
clsOpenAIRequest.cls
199 lines (152 loc) · 5.52 KB
/
clsOpenAIRequest.cls
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "clsOpenAIRequest"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'-----------------------------------------------------------------------------
' Project: OpenAI VBA Framework
' Class: clsOpenAIRequest
' Description: Builds the request json to send to OpenAI API
'
' 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
Implements IOpenAINameProvider
Private mstrModel As String
Private mobjMessages As clsOpenAIMessages
Private mlngMaxTokens As Long
Private mdblTopP As Double
Private mdblTemperature As Double
Private mdblFrequencyPenalty As Double
Private mdblPresencePenalty As Double
Private mstrPrompt As String
Private mlngImageWidth As Long
Private mlngImageHeight As Long
'Resolve, Connect, Send and Receive
Private mlngTimeoutResolve As Long
Private mlngTimeoutConnect As Long
Private mlngTimeoutSend As Long
Private mlngTimeoutReceive As Long
Private Function IOpenAINameProvider_GetClassName() As String
IOpenAINameProvider_GetClassName = "clsOpenAIRequest"
End Function
Private Function IOpenAINameProvider_ToString() As String
IOpenAINameProvider_ToString = "chat json=" & Me.GetChatSendToAPIJsonString
End Function
Private Sub Class_Initialize()
Set mobjMessages = New clsOpenAIMessages
End Sub
Private Sub Class_Terminate()
Set mobjMessages = Nothing
End Sub
Public Property Let prompt(ByVal value As String)
mstrPrompt = value
End Property
Public Property Get Model() As String
Model = mstrModel
End Property
Public Property Let Model(ByVal value As String)
mstrModel = value
End Property
Public Property Set messages(ByVal value As clsOpenAIMessages)
Set mobjMessages = value
End Property
Public Property Get MaxTokens() As Long
MaxTokens = mlngMaxTokens
End Property
Public Property Let MaxTokens(ByVal value As Long)
mlngMaxTokens = value
End Property
Public Property Let TopP(ByVal value As Double)
mdblTopP = value
End Property
Public Property Let Temperature(ByVal value As Double)
mdblTemperature = value
End Property
Public Property Get Temperature() As Double
Temperature = mdblTemperature
End Property
Public Property Let FrequencyPenalty(ByVal value As Double)
mdblFrequencyPenalty = value
End Property
Public Property Let PresencePenalty(ByVal value As Double)
mdblPresencePenalty = value
End Property
Public Property Get ImageHeight() As Long
ImageHeight = mlngImageHeight
End Property
Public Property Let ImageHeight(ByVal value As Long)
mlngImageHeight = value
End Property
Public Property Get ImageWidth() As Long
ImageWidth = mlngImageHeight
End Property
Public Property Let ImageWidth(ByVal value As Long)
mlngImageWidth = value
End Property
Public Property Let TimeoutResolve(ByVal value As Long)
mlngTimeoutResolve = value
End Property
Public Property Get TimeoutResolve() As Long
TimeoutResolve = mlngTimeoutResolve
End Property
Public Property Let TimeoutConnect(ByVal value As Long)
mlngTimeoutConnect = value
End Property
Public Property Get TimeoutConnect() As Long
TimeoutConnect = mlngTimeoutConnect
End Property
Public Property Let TimeoutSend(ByVal value As Long)
mlngTimeoutSend = value
End Property
Public Property Get TimeoutSend() As Long
TimeoutSend = mlngTimeoutSend
End Property
Public Property Let TimeoutReceive(ByVal value As Long)
mlngTimeoutReceive = value
End Property
Public Property Get TimeoutReceive() As Long
TimeoutReceive = mlngTimeoutReceive
End Property
Public Function GetChatSendToAPIJsonString() As String
GetChatSendToAPIJsonString = "{""model"": """ & mstrModel & """, " & mobjMessages.GetAllMessages & ", ""max_tokens"": " & mlngMaxTokens & ", ""top_p"": " & DblToEnglishStr(mdblTopP) & ", ""temperature"": " & DblToEnglishStr(mdblTemperature) & ", ""frequency_penalty"": " & DblToEnglishStr(mdblFrequencyPenalty) & ", ""presence_penalty"": " & mdblPresencePenalty & "}"
End Function
Public Function GetDalleImageSendToAPIJsonString() As String
Dim strImageSize As String
strImageSize = Me.GetImageDimensionLabel()
GetDalleImageSendToAPIJsonString = "{""prompt"": """ & mstrPrompt & """, ""size"": " & strImageSize & "}"
End Function
Public Function GetImageDimensionLabel() As String
GetImageDimensionLabel = Chr(34) & CStr(mlngImageWidth) & "x" & CStr(mlngImageHeight) & Chr(34)
End Function
Private Function DblToEnglishStr(dblNumber As Double) As String
'Purpose: Normalize formatting of number with decimals to English format
' Convert the double to string using system's regional settings
Dim strNumber As String
strNumber = CStr(dblNumber)
' Replace comma with period if system uses comma as decimal separator (eg. Italian format)
strNumber = Replace(strNumber, ",", ".")
' Return the formatted string
DblToEnglishStr = strNumber
End Function