Computer >> Computer tutorials >  >> Software >> Office

Build a Powerful GPT‑3 Content Generator Inside Excel

Build a Powerful GPT‑3 Content Generator Inside Excel

OpenAI’s GPT-3 can enhance Excel’s capabilities by creating a content generation tool for various applications like function definition, data summaries, product descriptions, etc. You can use OpenAI’s GPT-3 or GPT-3.5 models to generate content. GPT-3 models like “text-davinci-003 “have been widely used for various text generation tasks and support details but it’s been depreciated since January 2024. However, OpenAI introduced GPT-3.5 (e.g., gpt-3.5-turbo), which is more optimized for conversational tasks and follows a chat-based format.

This tutorial will guide you through integrating GPT-3 in Excel with the model “gpt-3.5-turbo” to build a tool that generates content based on user prompts.

Step 1: Set Up Your OpenAI Account to Get API Key

To use OpenAI’s GPT-3 API you will need to copy it from your OpenAI account.

  • If you don’t have an account, sign up at OpenAI and get your key.
  • If you already have an account, log in to the OpenAI account.
  • From Dashboard >> select API Key >> click on Create new secret key.
  • In Create new secret key box;
    • Name the key >> select Permissions >> click on Create secret key.

Build a Powerful GPT‑3 Content Generator Inside Excel

  • In the Save your key dialog box;
    • Copy the generated API key and store it in a safe place to authenticate your API requests. It won’t be visible once you create an API key.
    • Click on Done.

Build a Powerful GPT‑3 Content Generator Inside Excel

Never share your API key publicly or expose it in client-side code.

Step 2: Insert the VBA Code in Excel

In this step, we will write VBA code to send prompts to GPT-3 and retrieve responses.

  • Go to the Developer tab >> select Visual Basic.
  • In the VBA editor, go to Insert >> select Module.
  • Insert the following VBA code in the Module to create a UDF Function.

Build a Powerful GPT‑3 Content Generator Inside Excel

VBA Code:

Private Const API_KEY As String = "Your_OpenAI_API_Key" 'Replace your OpenAI API key.
' Function to call OpenAI API
Function GenerateContent(Prompt As String) As String
 On Error GoTo ErrorHandler
 Dim Http As Object
 Dim response As String
 
 ' Create HTTP object
 Set Http = CreateObject("MSXML2.XMLHTTP.6.0")
 
 ' Set up request headers and endpoint
 Http.Open "POST", "https://api.openai.com/v1/chat/completions", False
 Http.setRequestHeader "Content-Type", "application/json"
 Http.setRequestHeader "Authorization", "Bearer " & API_KEY
 
 ' Prepare the JSON payload for GPT-3.5-turbo
 Dim jsonPayload As String
 jsonPayload = "{""model"": ""gpt-3.5-turbo"", ""messages"": [{""role"": ""user"", ""content"": """ & Prompt & """}], ""max_tokens"": 200}"
 
 ' Send the request
 Http.send jsonPayload
 
 ' Get the response
 response = Http.responseText
 
 ' Parse the JSON response
 Dim jsonResponse As Object
 Set jsonResponse = JsonConverter.ParseJson(response)
 
 ' Return the content of the response text
 GenerateContent = jsonResponse("choices")(1)("message")("content")
 
 Exit Function
ErrorHandler:
 GenerateContent = "Error: " & Err.Description
End Function
  • Replace “Your_OpenAI_API_Key” with your actual API key.

Explanation:

  • Function GenerateContent(prompt As String): This is the user-defined function that takes a user prompt as input.
  • HTTP Request Setup:
    • Creates HTTP Object: It will use MSXML2.XMLHTTP to send requests.
    • Set Request Headers: It defines Content-Type as JSON and includes the Authorization header with the API key.
  • JSON Payload: Creates a JSON object containing the model’s name, user prompt, and maximum token limit.
  • Send Request: Sends the HTTP POST request to the OpenAI API endpoint with the JSON payload.
  • Response Handling: Retrieves the response and parses it using a JSON parser (VBA-JSON). Extracts the generated content from the API response.
  • Return Output: The function returns the generated text for display in the Excel cell.

Step 3: Install JSON Parsing Library

As Excel does not natively support JSON parsing you will need to download VBA-JSON from GitHub to parse JSON responses from GPT-3.

  • Download JsonConverter.bas from GitHub.
  • In the VBA editor, go to File tab >> select Import File.

Build a Powerful GPT‑3 Content Generator Inside Excel

  • In the Import Box >> select JsonConverter.bas to add it to the project.

Build a Powerful GPT‑3 Content Generator Inside Excel

Enable References:

You will need to enable Microsoft Scripting Runtime reference it is necessary because the JsonConverter library relies on Dictionary objects to manage JSON data structures.

  • Go to Tools tab >> select References.
  • In the Available References box >> check Microsoft Scripting Runtime >> click OK.

Build a Powerful GPT‑3 Content Generator Inside Excel

Step 4: Run the Script to Use the Content Generation Tool

  • Save the code and go back to your worksheet.
  • Type a prompt in your sheet to use as input of GPT-3 to respond.
  • Insert the following formula in cell B2.

This formula will use the GPT-3.5 turbo to generate content based on the prompt of cell A2 and will return it to cell B2.

Output:

Build a Powerful GPT‑3 Content Generator Inside Excel

Conclusion

Following the above steps, you can create a content generation tool in Excel using GPT-3. It will save you time and streamline your workflow. Now use your functional content generation tool in Excel. It will help you to generate ideas, draft content, and add intelligent automation to your content creation process. But remember OpenAI’s GPT API is not free the usage through OpenAI’s API will incur costs based on token usage, so monitor your usage to stay within budget.

Get FREE Advanced Excel Exercises with Solutions!