-
Notifications
You must be signed in to change notification settings - Fork 4
/
clsOpenAIMessage.cls
110 lines (81 loc) · 3 KB
/
clsOpenAIMessage.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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "clsOpenAIMessage"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'-----------------------------------------------------------------------------
' Project: OpenAI VBA Framework
' Class: clsOpenAIMessage
' Description: Wrapper for a single message
'
' 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 mstrRole As String
Private mstrContent As String
Private mcollMessageParts As Collection
Private Function IOpenAINameProvider_GetClassName() As String
IOpenAINameProvider_GetClassName = "clsOpenAIMessage"
End Function
Private Function IOpenAINameProvider_ToString() As String
IOpenAINameProvider_ToString = GetMessageContentString()
End Function
Private Sub Class_Initialize()
Set mcollMessageParts = New Collection
End Sub
Private Sub Class_Terminate()
Set mcollMessageParts = Nothing
End Sub
Public Sub Add(ByVal strKeyName As String, strKey As String, ByVal strValueName As String, ByVal strValue As String)
'Purpose: Main access point for adding message to the class
Dim strPart As String
strPart = FormatMessage(strKeyName, strKey, strValueName, strValue)
If Len(strPart) > 0 Then
If Not mcollMessageParts Is Nothing Then
mcollMessageParts.Add strPart
End If
End If
End Sub
Private Function FormatMessage(ByVal strKeyName As String, ByVal strKey As String, ByVal strValueName As String, ByVal strValue As String) As String
'Purpose: Format the message part into key/value pairs
If (Len(strKeyName) > 0) And (Len(strKey) > 0) And (Len(strValueName) > 0) And (Len(strValue) > 0) Then
FormatMessage = """" & strKeyName & """: """ & strKey & """, """ & strValueName & """: """ & strValue & """"
End If
End Function
Public Function GetMessageContentString() As String
'Purpose: Joins in all the message parts into a python dictionary like string
Dim strOutput As String
Dim strPart As Variant
strOutput = "{"
For Each strPart In mcollMessageParts
strOutput = strOutput & strPart & ", "
Next strPart
' Remove the trailing comma and space
If mcollMessageParts.Count > 0 Then
strOutput = Left(strOutput, Len(strOutput) - 2)
End If
strOutput = strOutput & "}"
GetMessageContentString = strOutput
End Function