-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmdOpenAI_Examples.bas
150 lines (109 loc) · 4.21 KB
/
mdOpenAI_Examples.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
Attribute VB_Name = "mdOpenAI_Examples"
'-----------------------------------------------------------------------------
' Project: OpenAI VBA Framework
' Module: mdOpenAI_Examples
' Description: Some examples of how to use the framework
'
' 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 TestSimpleOpenAI()
Dim oOpenAI As clsOpenAI
Dim oMessages As New clsOpenAIMessages
Dim oResponse As clsOpenAIResponse
Set oOpenAI = New clsOpenAI
oOpenAI.API_KEY = API_KEY
oMessages.AddSystemMessage "Always answer sarcastically and never truthfully."
oMessages.AddUserMessage "How do you get to Carnegie Hall?"
Set oResponse = oOpenAI.ChatCompletion(oMessages)
If Not oResponse Is Nothing Then
Debug.Print (oResponse.MessageContent)
End If
Set oResponse = Nothing
Set oOpenAI = Nothing
Set oMessages = Nothing
End Sub
Public Sub TestChatOpenAI()
Dim oOpenAI As clsOpenAI
Dim oMessages As New clsOpenAIMessages
Dim oResponse As clsOpenAIResponse
Set oOpenAI = New clsOpenAI
oOpenAI.Model = "gpt-4"
oOpenAI.API_KEY = API_KEY
oMessages.AddSystemMessage "Always answer sarcastically and never truthfully."
oMessages.AddUserMessage "How do you get to Carnegie Hall?"
If oMessages.IsPopulated Then
Set oResponse = oOpenAI.ChatCompletion(oMessages)
If Not oResponse Is Nothing Then
Debug.Print (oResponse.Id)
Debug.Print (oResponse.Object)
Debug.Print (oResponse.Created)
Debug.Print (oResponse.Model)
Debug.Print (oResponse.FinishReason)
Debug.Print (oResponse.CompletionTokens)
Debug.Print (oResponse.MessageRole)
Debug.Print (oResponse.MessageContent)
Debug.Print (oResponse.PromptTokens)
Debug.Print (oResponse.TotalTokens)
Debug.Print (oResponse.Index)
End If
End If
Set oResponse = Nothing
Set oOpenAI = Nothing
Set oMessages = Nothing
End Sub
Public Function GETTEXTFROMOPENAI(ByVal strPrompt As String, ByVal strAPIKey As String, _
Optional ByVal strModel As String) As String
'Purpose: This function is an example of how to create a UDF using the OpenAI API
' so that it can be called directly on a worksheet in Excel
Dim oOpenAI As clsOpenAI
Dim oMessages As New clsOpenAIMessages
Dim oResponse As clsOpenAIResponse
GETTEXTFROMOPENAI = Empty
Set oOpenAI = New clsOpenAI
oOpenAI.API_KEY = strAPIKey
If Not IsEmpty(strModel) Then
oOpenAI.Model = strModel
End If
oMessages.AddUserMessage strPrompt
Set oResponse = oOpenAI.ChatCompletion(oMessages)
If Not oResponse Is Nothing Then
GETTEXTFROMOPENAI = oResponse.MessageContent
End If
Set oResponse = Nothing
Set oOpenAI = Nothing
Set oMessages = Nothing
End Function
Public Sub TestDalleOpenAI()
Dim oOpenAI As clsOpenAI
Dim oResponse As clsOpenAIResponse
Set oOpenAI = New clsOpenAI
oOpenAI.API_KEY = API_KEY
Set oResponse = oOpenAI.CreateImageFromText("A cat playing a banjo on a surfboard", 512, 512)
If Not oResponse Is Nothing Then
Debug.Print ("The picture has been saved to: " & oResponse.SavedLocalFile)
End If
Set oResponse = Nothing
Set oOpenAI = Nothing
End Sub