Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
zq99 authored May 17, 2023
0 parents commit adefacd
Show file tree
Hide file tree
Showing 12 changed files with 1,614 additions and 0 deletions.
42 changes: 42 additions & 0 deletions IOpenAINameProvider.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "IOpenAINameProvider"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'-----------------------------------------------------------------------------
' Project: OpenAI VBA Framework
' Class: IOpenAINameProvider
' Description: All classes in the framework need to implement these methods
'
' 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.
'
' Copyright (c) 2023 Zaid Qureshi
'-----------------------------------------------------------------------------

Option Explicit

Public Function GetClassName() As String
End Function

Public Function ToString() As String
End Function
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Zaid Qureshi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Binary file added OpenAIFrameworkDemo.xlsm
Binary file not shown.
155 changes: 155 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# OpenAI-VBA-Framework

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/zq99/openai-vba-framework/blob/main/LICENSE)

OpenAI-VBA-Framework is a toolkit for developers looking to create applications in VBA that interact with OpenAI's large language models such as GPT-4 and ChatGPT. This framework provides a suite of classes to facilitate smooth integration with OpenAI's API.

## Main Classes
1. `clsOpenAI` - Main class to interact with OpenAI
2. `clsOpenAILogger` - Logging class for debugging and tracking
3. `clsOpenAIMessage` - Class to handle individual messages
4. `clsOpenAIMessages` - Class to handle collections of messages
5. `clsOpenAIRequest` - Class for making requests to OpenAI
6. `clsOpenAIResponse` - Class for handling responses from OpenAI
7. `IOpenAINameProvider` - Interface class for name provision

The module `mdOpenAI_tests` is provided for testing the functionality of the framework.

`OpenAIFrameworkDemo.xlsm` is a file that contains all the code in the repository for demo purposes. Other files are also included in the repository for versioning.

## Prerequisites
- You will need to sign up for an OpenAI account and create an API_KEY. You can do this at the following location: [OpenAI API Keys](https://platform.openai.com/account/api-keys)

## Installation
1. Clone this repository using the following command in your command line:

```bash
git clone https://github.com/zq99/OpenAI-VBA-Framework.git
```

2. Open Excel and press ALT + F11 to open the VBA editor.
3. From the toolbar select File -> Import File....
4. Navigate to the location of the cloned repository and select all the .cls and .bas files then click Open.
5. Save the Excel file as a macro-enabled workbook .xlsm.

## Usage

Here are some examples of using the framework:

### Chat Completion API

```
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 know how to 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
```

### Text Completion API

```
Public Sub TestTextCompletionSimpleOpenAI()
Dim oOpenAI As clsOpenAI
Dim oResponse As clsOpenAIResponse
Set oOpenAI = New clsOpenAI
oOpenAI.API_KEY = "<API_KEY>"
Set oResponse = oOpenAI.TextCompletion("Write a Haiku about a Dinosaur that loves to code!")
If Not oResponse Is Nothing Then
Debug.Print (oResponse.TextContent)
End If
Set oResponse = Nothing
Set oOpenAI = Nothing
End Sub
```

### Excel User Defined function

```
Public Function GETTEXTFROMOPENAI(prompt As String, apiKey As String, Optional ByVal Model as String) As String
Dim oOpenAI As clsOpenAI
Dim oResponse As clsOpenAIResponse
Set oOpenAI = New clsOpenAI
oOpenAI.API_KEY = apiKey
If Not IsEmpty(Model) Then
oOpenAI.Model = Model
End If
Set oResponse = oOpenAI.TextCompletion(prompt)
If Not oResponse Is Nothing Then
GETTEXTFROMOPENAI = oResponse.TextContent
Else
GETTEXTFROMOPENAI = ""
End If
End Function
```

## Configuration

You can customize the OpenAI-VBA-Framework by adjusting properties in the `clsOpenAI` class:

```vba
' Specify the model
oOpenAI.Model = "gpt-3.5-turbo"
' Set the maximum number of tokens
oOpenAI.MaxTokens = 512
' Control the diversity of generated text
oOpenAI.TopP = 0.9
' Influence the randomness of generated text
oOpenAI.Temperature = 0.7
' Control preference for frequent phrases
oOpenAI.FrequencyPenalty = 0.5
' Control preference for including the prompt in the output
oOpenAI.PresencePenalty = 0.5
' Control logging of messages to the Immediate Window
oOpenAI.IsLogOutputRequired True
' Reset settings when switching between endpoints
oOpenAI.ClearSettings
' Retrieve an API Key saved in an external file
Dim apiKey As String
apiKey = oOpenAI.GetReadAPIKeyFromFolder("<FolderPath>")
```

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

## License
This project is licensed under the terms of the MIT license.

Loading

0 comments on commit adefacd

Please sign in to comment.