├── .gitattributes
├── .github
├── assets
│ └── images
│ │ ├── add-env-var-step-1.png
│ │ ├── add-env-var-step-2.png
│ │ ├── add-env-var-step-3.png
│ │ ├── add-env-var-step-4.png
│ │ ├── add-the-macro-into-new-group.png
│ │ ├── chatgpt-results-1.png
│ │ ├── chatgpt-results-2.png
│ │ ├── choose-commands-from-macros.png
│ │ ├── developer-tab-visual-basic.png
│ │ ├── enable-developer-tab-highlighted.png
│ │ ├── options-customize-ribbon-new-tab.png
│ │ ├── options-customize-ribbon-rename-tab.png
│ │ ├── rename-button-to-text-completion.png
│ │ ├── rename-new-group.png
│ │ ├── screen-after-chatgpt-button-added.png
│ │ ├── visual-basic-add-apikey-section.png
│ │ └── visual-basic-file-import-section.png
└── workflows
│ └── blank.yml
├── CHANGELOG.md
├── CODE_OF_CONDUCT.md
├── LICENSE.md
├── README.md
└── src
└── windows
└── ChatgptTextCompletion.bas
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.bas linguist-language=VBA
2 |
--------------------------------------------------------------------------------
/.github/assets/images/add-env-var-step-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/add-env-var-step-1.png
--------------------------------------------------------------------------------
/.github/assets/images/add-env-var-step-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/add-env-var-step-2.png
--------------------------------------------------------------------------------
/.github/assets/images/add-env-var-step-3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/add-env-var-step-3.png
--------------------------------------------------------------------------------
/.github/assets/images/add-env-var-step-4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/add-env-var-step-4.png
--------------------------------------------------------------------------------
/.github/assets/images/add-the-macro-into-new-group.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/add-the-macro-into-new-group.png
--------------------------------------------------------------------------------
/.github/assets/images/chatgpt-results-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/chatgpt-results-1.png
--------------------------------------------------------------------------------
/.github/assets/images/chatgpt-results-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/chatgpt-results-2.png
--------------------------------------------------------------------------------
/.github/assets/images/choose-commands-from-macros.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/choose-commands-from-macros.png
--------------------------------------------------------------------------------
/.github/assets/images/developer-tab-visual-basic.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/developer-tab-visual-basic.png
--------------------------------------------------------------------------------
/.github/assets/images/enable-developer-tab-highlighted.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/enable-developer-tab-highlighted.png
--------------------------------------------------------------------------------
/.github/assets/images/options-customize-ribbon-new-tab.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/options-customize-ribbon-new-tab.png
--------------------------------------------------------------------------------
/.github/assets/images/options-customize-ribbon-rename-tab.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/options-customize-ribbon-rename-tab.png
--------------------------------------------------------------------------------
/.github/assets/images/rename-button-to-text-completion.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/rename-button-to-text-completion.png
--------------------------------------------------------------------------------
/.github/assets/images/rename-new-group.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/rename-new-group.png
--------------------------------------------------------------------------------
/.github/assets/images/screen-after-chatgpt-button-added.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/screen-after-chatgpt-button-added.png
--------------------------------------------------------------------------------
/.github/assets/images/visual-basic-add-apikey-section.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/visual-basic-add-apikey-section.png
--------------------------------------------------------------------------------
/.github/assets/images/visual-basic-file-import-section.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/analyticsinmotion/add-chatgpt-to-microsoft-word/afdac4f8c370b9afa59c59438bd5f78452bfc9ac/.github/assets/images/visual-basic-file-import-section.png
--------------------------------------------------------------------------------
/.github/workflows/blank.yml:
--------------------------------------------------------------------------------
1 | # This is a basic workflow to help you get started with Actions
2 |
3 | name: CI
4 |
5 | # Controls when the workflow will run
6 | on:
7 | # Triggers the workflow on push or pull request events but only for the "main" branch
8 | push:
9 | branches: [ "main" ]
10 | pull_request:
11 | branches: [ "main" ]
12 |
13 | # Allows you to run this workflow manually from the Actions tab
14 | workflow_dispatch:
15 |
16 | # A workflow run is made up of one or more jobs that can run sequentially or in parallel
17 | jobs:
18 | # This workflow contains a single job called "build"
19 | build:
20 | # The type of runner that the job will run on
21 | runs-on: ubuntu-latest
22 |
23 | # Steps represent a sequence of tasks that will be executed as part of the job
24 | steps:
25 | # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
26 | - uses: actions/checkout@v3
27 |
28 | # Runs a single command using the runners shell
29 | - name: Run a one-line script
30 | run: echo Hello, world!
31 |
32 | # Runs a set of commands using the runners shell
33 | - name: Run a multi-line script
34 | run: |
35 | echo Add other actions to build,
36 | echo test, and deploy your project.
37 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # CHANGELOG
2 | This changelog file outlines a chronologically ordered list of the changes made on this project.
3 | It is organized by version and release date followed by a list of Enhancements, New Features, Bug Fixes, and/or Breaking Changes.
4 |
5 |
6 | ## Version 0.1.4 (Latest)
7 | **Released:** March 22, 2023
8 | **Tag:** v0.1.4
9 |
10 |
11 | ### Moved
12 |
13 | - The latest ChatgptTextCompletion.bas file has been moved to the src/windows directory and will no longer be stored in the repository root location main.
14 |
15 | ### Bug Fix
16 |
17 | - Added new error handling for http request timeouts. This occurs when a outage occurs on the OpenAI Chat channel. The Alert message will also point users to OpenAI's status page status.openai.com to check for any types of service outages.
18 |
19 |
20 |
21 | ## Version 0.1.3
22 | **Released:** February 12, 2023
23 | **Tag:** v0.1.3
24 |
25 | ### Bug Fix
26 |
27 | - Added additional error handling for when ChatGPT is at capacity. When this occurs an Alert Message will display on the screen rather than a Run-time Error 5 message from Microsoft Visual Basic. See Issues #2 for more details.
28 |
29 |
30 | ## Version 0.1.2
31 | **Releasd:** February 10, 2023
32 | **Tag:** v0.1.2
33 |
34 | ### Breaking Changes
35 |
36 | - Moved the location of the OpenAI API Key from source to a Windows Environment variable.
37 |
38 |
39 | ## Version 0.1.1
40 | **Releasd:** February 8, 2023
41 | **Tag:** v0.1.1
42 |
43 | ### Bug Fix
44 |
45 | - Added additional error handling for ChatGPT error responses
46 | - Responses from ChatGPT often contain '/n' in the returned text strings. We have replaced cases of /n in the text string with a carriage return. This makes output in Word much more readable.
47 |
48 |
49 | ## Version 0.1.0 (Initial Release)
50 | **Releasd:** February 6, 2023
51 | **Tag:** v0.1.0
52 |
53 | This is the initial release
54 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | We as members, contributors, and leaders pledge to make participation in our
6 | community a harassment-free experience for everyone, regardless of age, body
7 | size, visible or invisible disability, ethnicity, sex characteristics, gender
8 | identity and expression, level of experience, education, socio-economic status,
9 | nationality, personal appearance, race, religion, or sexual identity
10 | and orientation.
11 |
12 | We pledge to act and interact in ways that contribute to an open, welcoming,
13 | diverse, inclusive, and healthy community.
14 |
15 | ## Our Standards
16 |
17 | Examples of behavior that contributes to a positive environment for our
18 | community include:
19 |
20 | * Demonstrating empathy and kindness toward other people
21 | * Being respectful of differing opinions, viewpoints, and experiences
22 | * Giving and gracefully accepting constructive feedback
23 | * Accepting responsibility and apologizing to those affected by our mistakes,
24 | and learning from the experience
25 | * Focusing on what is best not just for us as individuals, but for the
26 | overall community
27 |
28 | Examples of unacceptable behavior include:
29 |
30 | * The use of sexualized language or imagery, and sexual attention or
31 | advances of any kind
32 | * Trolling, insulting or derogatory comments, and personal or political attacks
33 | * Public or private harassment
34 | * Publishing others' private information, such as a physical or email
35 | address, without their explicit permission
36 | * Other conduct which could reasonably be considered inappropriate in a
37 | professional setting
38 |
39 | ## Enforcement Responsibilities
40 |
41 | Community leaders are responsible for clarifying and enforcing our standards of
42 | acceptable behavior and will take appropriate and fair corrective action in
43 | response to any behavior that they deem inappropriate, threatening, offensive,
44 | or harmful.
45 |
46 | Community leaders have the right and responsibility to remove, edit, or reject
47 | comments, commits, code, wiki edits, issues, and other contributions that are
48 | not aligned to this Code of Conduct, and will communicate reasons for moderation
49 | decisions when appropriate.
50 |
51 | ## Scope
52 |
53 | This Code of Conduct applies within all community spaces, and also applies when
54 | an individual is officially representing the community in public spaces.
55 | Examples of representing our community include using an official e-mail address,
56 | posting via an official social media account, or acting as an appointed
57 | representative at an online or offline event.
58 |
59 | ## Enforcement
60 |
61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be
62 | reported to the community leaders responsible for enforcement at
63 | pi@analyticsinmotion.com.
64 | All complaints will be reviewed and investigated promptly and fairly.
65 |
66 | All community leaders are obligated to respect the privacy and security of the
67 | reporter of any incident.
68 |
69 | ## Enforcement Guidelines
70 |
71 | Community leaders will follow these Community Impact Guidelines in determining
72 | the consequences for any action they deem in violation of this Code of Conduct:
73 |
74 | ### 1. Correction
75 |
76 | **Community Impact**: Use of inappropriate language or other behavior deemed
77 | unprofessional or unwelcome in the community.
78 |
79 | **Consequence**: A private, written warning from community leaders, providing
80 | clarity around the nature of the violation and an explanation of why the
81 | behavior was inappropriate. A public apology may be requested.
82 |
83 | ### 2. Warning
84 |
85 | **Community Impact**: A violation through a single incident or series
86 | of actions.
87 |
88 | **Consequence**: A warning with consequences for continued behavior. No
89 | interaction with the people involved, including unsolicited interaction with
90 | those enforcing the Code of Conduct, for a specified period of time. This
91 | includes avoiding interactions in community spaces as well as external channels
92 | like social media. Violating these terms may lead to a temporary or
93 | permanent ban.
94 |
95 | ### 3. Temporary Ban
96 |
97 | **Community Impact**: A serious violation of community standards, including
98 | sustained inappropriate behavior.
99 |
100 | **Consequence**: A temporary ban from any sort of interaction or public
101 | communication with the community for a specified period of time. No public or
102 | private interaction with the people involved, including unsolicited interaction
103 | with those enforcing the Code of Conduct, is allowed during this period.
104 | Violating these terms may lead to a permanent ban.
105 |
106 | ### 4. Permanent Ban
107 |
108 | **Community Impact**: Demonstrating a pattern of violation of community
109 | standards, including sustained inappropriate behavior, harassment of an
110 | individual, or aggression toward or disparagement of classes of individuals.
111 |
112 | **Consequence**: A permanent ban from any sort of public interaction within
113 | the community.
114 |
115 | ## Attribution
116 |
117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118 | version 2.0, available at
119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
120 |
121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct
122 | enforcement ladder](https://github.com/mozilla/diversity).
123 |
124 | [homepage]: https://www.contributor-covenant.org
125 |
126 | For answers to common questions about this code of conduct, see the FAQ at
127 | https://www.contributor-covenant.org/faq. Translations are available at
128 | https://www.contributor-covenant.org/translations.
129 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 Analytics in Motion
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # :dark_sunglasses: Add ChatGPT to Microsoft Word [](https://twitter.com/intent/tweet?text=How%20to%20add%20ChatGPT's%20Text%20Completion%20functionality%20to%20Microsoft%20Word.&url=https://github.com/analyticsinmotion/add-chatgpt-to-microsoft-word&via=analyticsmotion&hashtags=chatgpt,openai,Microsoft365,davinci)
2 |
3 | How to add ChatGPT's Text Completion functionality to Microsoft Word.
4 |
5 |
6 |
7 |
18 |
19 |
20 |
21 | | | |
22 | | --- | --- |
23 | | Powered by | [](https://chat.openai.com)  |
24 | | Meta | [](https://github.com/analyticsinmotion/chatgpt-images-r-shiny/blob/main/LICENSE.md)    |
25 | | Testing | [](https://github.com/analyticsinmotion/add-chatgpt-to-microsoft-word/actions/workflows/blank.yml) |
26 | | Orgs | [](https://www.microsoft.com) [](https://openai.com) [](https://www.analyticsinmotion.com) |
27 |
28 |
29 | ## 1. Description
30 |
31 | With the click of a single button you can access ChatGPT's awesome natural language capabilies in Microsoft Word
32 |
33 |
34 |
35 |
36 | https://user-images.githubusercontent.com/52817125/218468803-5cdfbda3-4a5f-4d74-aaec-2a136e9b7956.mp4
37 |
38 |
39 |
40 |
41 |
42 |
43 | ## 2. Getting Started
44 | ### 2.1 Dependencies
45 | - Requires an OpenAI API Key (create an account and get API Key at https://chat.openai.com)
46 | - Requires Microsoft Windows 10/11 (https://www.microsoft.com/en-au/windows)
47 | - Requires Microsoft Word 365 (https://www.microsoft.com/en-us)
48 |
49 | Please be aware of the [costs](https://openai.com/pricing) associated with using the OpenAI API when utilizing this project.
50 |
51 |
52 |
53 | ## 3. Instructions
54 |
55 | - Write the text you want to send to ChatGPT anywhere in Microsoft Word
56 | - Click the Text Completion button in the AI Assistant tab
57 | - Wait a few seconds for ChatGPT to respond
58 | - The text completion results will appear under initial request text
59 |
60 |
61 | ### 3.1 Text Completion Example 1
62 |
63 | User Input
64 | ```
65 | What are the benefits of generative AI models?
66 | ```
67 |
68 | ChatGPT Output
69 |
70 |
71 |
72 |
73 | ### 3.2 Text Completion Example 2
74 |
75 | User Input
76 | ```
77 | Are aliens real?
78 | ```
79 |
80 | ChatGPT Output
81 |
82 |
83 |
84 |
85 |
86 | ## 4. Installation
87 |
88 | There are 4 basic steps in order to add a ChatGPT button into Microsoft Word:
89 | 1. Enable the Developer Tab
90 | 2. Import the VBA script file
91 | 3. Create the ChatGPT button
92 | 4. Add your OpenAI APIKey
93 |
94 | Each of these steps are fully outlined below.
95 |
96 |
97 | ### 4.1 Enable the Developer Tab
98 |
99 | The Developer tab isn't displayed by default, but you can add it to the ribbon.
100 |
101 | **Step 1** - On the File tab, go to Options > Customize Ribbon.
102 |
103 | **Step 2** - Under Customize the Ribbon and under Main Tabs, select the Developer check box.
104 |
105 |
106 |
107 |
108 | The latest instructions to enable the Developer Tab from Microsoft can be found here:
109 | https://support.microsoft.com/en-us/office/show-the-developer-tab-in-word-e356706f-1891-4bb8-8d72-f57a51146792
110 |
111 |
112 | ### 4.2 Import the ChatgptTextCompletion.bas file
113 |
114 | **Step 1** - Download and Save the latest ChatgptTextCompletion.bas file from this repository.
115 |
116 |
117 | Keep the location of where the file is saved as you will need it later.
118 |
119 |
120 | **Step 2** - On the Developer tab, click the Visual Basic button.
121 |
122 |
123 |
124 |
125 | **Step 3** - On the File tab, go to Import File...
126 |
127 |
128 |
129 |
130 | **Step 4** - Select the ChatgptTextCompletion.bas file and click Open
131 |
132 |
133 | ### 4.3 Add your ChatGPT button into the Microsoft Word Ribbon
134 |
135 | **Step 1** - Add a new tab
136 |
137 | - On the File tab, go to Options > Customize Ribbon
138 | - Click New Tab
139 |
140 |
141 |
142 |
143 |
144 | **Step 2** - Rename the New Tab to **AI Assistant**
145 |
146 |
147 |
148 |
149 | **Step 3** - Rename New Group (Custom) to **ChatGPT**
150 |
151 |
152 |
153 |
154 | **Step 4** - Select Macros in the Choose Commands from dropdown box
155 |
156 |
157 |
158 |
159 | **Step 5** - Select the ChatgptTextCompletion Macro and click Add >>
160 |
161 |
162 |
163 |
164 |
165 | **Step 6** - Rename button to **Text Completion**, select a Symbol and click OK
166 |
167 |
168 |
169 |
170 | After the preceding steps have been completed the Microsoft Word screen should look like the following:
171 |
172 |
173 |
174 |
175 | ### 4.4 Add your OpenAI APIKey into Windows
176 |
177 | **Step 1** - Open the Start menu and start typing "environment variables". When the best match appears click "Edit the system environment variables" result.
178 |
179 |
180 |
181 |
182 | **Step 2** - Click the "Environment variables" button under the "Advanced" tab.
183 |
184 |
185 |
186 |
187 | **Step 3** - Create a new user variable by clicking "New" under the "User Variables" section.
188 |
189 |
190 |
191 |
192 | **Step 4** - Type the variable name **OPENAI_API_KEY** in the first field and your OpenAI APIKEY in the variable value field. Then click OK.
193 |
194 |
195 |
196 |
197 | **Step 5** - **IMPORTANT** You must restart Windows to apply the new environment variable
198 |
199 |
200 |
201 |
202 |
203 |
204 | ## 5. Best Practices for API Key Safety
205 |
206 | Your OpenAI APIKEY key/s should be kept secure and private at all times.
207 |
208 | Please follow the best practices guide for API security from OpenAI
209 |
210 | https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 | ## 6. Related Projects
220 |
221 | This repository closely relates to two of our other projects:
222 | - **Add GPT Chat to Microsoft Word** - Create a powerful chatbot in Microsoft Word using ChatGPT
223 | More information on this project can be found at https://github.com/analyticsinmotion/add-gpt-chatbot-to-microsoft-word
224 |
225 | - **Create Images with DALL·E in Microsoft Word** - How to add OpenAI's Image Generation capability (DALL·E 2) to Microsoft Word
226 | More information on this project can be found at https://github.com/analyticsinmotion/create-images-with-dall-e-in-microsoft-word
227 |
228 |
229 |
230 |
--------------------------------------------------------------------------------
/src/windows/ChatgptTextCompletion.bas:
--------------------------------------------------------------------------------
1 | Attribute VB_Name = "ChatgptTextCompletion"
2 | Option Explicit
3 | Sub TextCompletion()
4 | '
5 | ' Text Completion Macro - Version 0.1.4
6 | '
7 | '
8 | If Selection.Type = wdSelectionIP Then
9 | Exit Sub
10 | End If
11 |
12 | If Selection.Text = ChrW$(13) Then
13 | Exit Sub
14 | End If
15 |
16 | Dim strAPIKey As String
17 | Dim strURL As String
18 | Dim strPrompt As String
19 | Dim strModel As String
20 | Dim intMaxTokens As Integer
21 | Dim strResponse As String
22 | Dim objCurlHttp As Object
23 | Dim strJSONdata As String
24 |
25 | strAPIKey = Environ("OPENAI_API_KEY")
26 |
27 | strURL = "https://api.openai.com/v1/completions"
28 | strModel = "text-davinci-003"
29 | intMaxTokens = 2048
30 | strPrompt = Replace(Selection, ChrW$(13), "")
31 | strJSONdata = "{""model"":""" & strModel & """,""prompt"":""" & strPrompt & """,""max_tokens"":2048}"
32 |
33 | Set objCurlHttp = CreateObject("MSXML2.serverXMLHTTP")
34 |
35 | With objCurlHttp
36 | .Open "POST", strURL, False
37 | .SetRequestHeader "Content-type", "application/json"
38 | .SetRequestHeader "Authorization", "Bearer " + strAPIKey
39 | .Send (strJSONdata)
40 |
41 | Dim strStatus As Integer
42 | strStatus = .Status
43 | Dim strStatusText As String
44 | strStatusText = .StatusText
45 |
46 | If strStatus <> 200 Then
47 | MsgBox Prompt:="The OpenAI servers have experienced an error while processing your request! Please try again shortly, or for continued downtime please check the Chat status at: https://status.openai.com/"
48 | Exit Sub
49 | End If
50 |
51 | strResponse = .ResponseText
52 |
53 |
54 | If Mid(strResponse, 8, 5) = "error" Then
55 | MsgBox Prompt:="The server had an error while processing your request. Sorry about that! Please try again shortly."
56 | Exit Sub
57 | End If
58 |
59 |
60 | Dim intStartPos As Integer
61 | intStartPos = InStr(1, strResponse, Chr(34) & "text" & Chr(34)) + 12
62 |
63 | If intStartPos = 12 Then
64 | MsgBox Prompt:="ChatGPT is at capacity right now. Please wait a minute and try again."
65 | Exit Sub
66 | End If
67 |
68 | Dim intEndPos As Integer
69 | intEndPos = InStr(1, strResponse, Chr(34) & "index" & Chr(34)) - 2
70 |
71 | Dim intLength As Integer
72 | intLength = intEndPos - intStartPos
73 |
74 | Dim strOutput As String
75 | strOutput = Mid(strResponse, intStartPos, intLength)
76 |
77 |
78 | Dim strOutputFormatted As String, strOutputFormatted1 As String, strOutputFormatted2 As String
79 | strOutputFormatted1 = Replace(strOutput, "\n\n", vbCrLf)
80 | strOutputFormatted2 = Replace(strOutputFormatted1, "\n", vbCrLf)
81 | strOutputFormatted = strOutputFormatted2
82 |
83 | Selection.Collapse Direction:=wdCollapseEnd
84 | Selection.InsertAfter vbCr & strOutputFormatted
85 | Selection.Font.Name = "Courier New"
86 | Selection.Font.Size = 9
87 | Selection.Font.ColorIndex = wdViolet
88 | Selection.Paragraphs.Alignment = wdAlignParagraphJustify
89 | Selection.InsertAfter vbCr
90 | Selection.Collapse Direction:=wdCollapseEnd
91 |
92 |
93 | End With
94 |
95 | Set objCurlHttp = Nothing
96 |
97 | End Sub
98 |
99 |
--------------------------------------------------------------------------------