-
Notifications
You must be signed in to change notification settings - Fork 4
/
clsOpenAIMessages.cls
149 lines (106 loc) · 3.85 KB
/
clsOpenAIMessages.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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "clsOpenAIMessages"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'-----------------------------------------------------------------------------
' Project: OpenAI VBA Framework
' Class: clsOpenAIMessages
' Description: Holds the message objects (clsOpenAIMessage) to send to Chat 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 mcollMessages As Collection
Private Function IOpenAINameProvider_GetClassName() As String
IOpenAINameProvider_GetClassName = "clsOpenAIMessages"
End Function
Private Function IOpenAINameProvider_ToString() As String
IOpenAINameProvider_ToString = GetAllMessages
End Function
Private Sub Class_Initialize()
Set mcollMessages = New Collection
End Sub
Private Sub Class_Terminate()
Set mcollMessages = Nothing
End Sub
Public Sub AddMessage(ByVal objMsg As clsOpenAIMessage)
mcollMessages.Add objMsg
End Sub
Public Sub ClearMessages()
If Not mcollMessages Is Nothing Then
mcollMessages.Clear
End If
End Sub
Public Function GetAllMessages() As String
'Purpose: This builds a string of all messages that will be sent to OpenAI API
Dim objMsg As clsOpenAIMessage
Dim strOutput As String
If Me.GetTotal = 0 Then
GetAllMessages = ""
Exit Function
End If
strOutput = """messages"": ["
For Each objMsg In mcollMessages
strOutput = strOutput & objMsg.GetMessageContentString & ", "
Next objMsg
' Remove the trailing comma and space
If mcollMessages.Count > 0 Then
strOutput = Left(strOutput, Len(strOutput) - 2)
End If
strOutput = strOutput & "]"
GetAllMessages = strOutput
Set objMsg = Nothing
End Function
Private Function GetMessageObject(ByVal strKeyName As String, ByVal strKey As String, ByVal strValueName As String, ByVal strValue As String) As clsOpenAIMessage
'Purpose: Helper routine to construct known message object types
Dim objMsg As clsOpenAIMessage
Set objMsg = New clsOpenAIMessage
objMsg.Add strKeyName, strKey, strValueName, strValue
Set GetMessageObject = objMsg
Set objMsg = Nothing
End Function
Public Sub AddUserMessage(ByVal strContent As String)
Call Me.AddMessage(GetMessageObject("role", "user", "content", strContent))
End Sub
Public Sub AddSystemMessage(ByVal strContent As String)
Call Me.AddMessage(GetMessageObject("role", "system", "content", strContent))
End Sub
Public Sub AddAssistantMessage(ByVal strContent As String)
Call Me.AddMessage(GetMessageObject("role", "assistant", "content", strContent))
End Sub
Public Sub AddCustomMessage(ByVal strKeyName As String, ByVal strKeyValue As String, ByVal strValueName As String, ByVal strValue As String)
Call Me.AddMessage(GetMessageObject(strKeyName, strKeyValue, strValueName, strValue))
End Sub
Public Function GetTotal() As Integer
If Not mcollMessages Is Nothing Then
GetTotal = mcollMessages.Count
Else
GetTotal = 0
End If
End Function
Public Function IsPopulated() As Boolean
IsPopulated = IIf(Me.GetTotal > 0, True, False)
End Function