Make ChatGPT requests with Basic

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
psilocybe
Posts: 108
Joined: Thu Jun 15, 2017 5:33 am

Make ChatGPT requests with Basic

Post by psilocybe »

Hi all,

It is possible to make ChatGPT requests with Basic.

Restrictions apply to the use of OpenOffice under Windows. Please see the documentation for more information.

You must first install the OAuth2OOo extension version 1.0.0

You also need your ChatGPT API key available in your ChatGPT account under View API Key

and here is an example of Basic code:

Code: Select all

Rem  *****  BASIC  *****

Sub Main

Rem First we need to create the OAuth2Service
oauth2 = CreateUnoService("io.github.prrvchr.OAuth2OOo.OAuth2Service")

Rem To execute an HTTP request we first need a HTTP Request parameter
parameter = oauth2.getRequestParameter("ChatGPT")

Rem The HTTP method needed is POST
parameter.Method = "POST"

parameter.Url = "https://api.openai.com/v1/chat/completions"

Rem Default Authentication is OAuth2 but can be disabled with
parameter.NoAuth = True

Rem You need to put your API key in the HTTP Request Headers Authorization
parameter.setHeader("Authorization", "Bearer " & "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

Rem We need to create the Request Json Body needed by ChatGPT API call: https://platform.openai.com/docs/guides/gpt/chat-completions-api
builder = parameter.getJsonBuilder()
json = builder.createObject()_
              .addString("model", "gpt-3.5-turbo")_
              .addArray("messages", builder.createArray()_
                                           .addObject(builder.createObject()_
                                                             .addString("role", "system")_
                                                             .addString("content", "You are a helpful assistant."))_
                                           .addObject(builder.createObject()_
                                                             .addString("role", "user")_
                                                             .addString("content", "How to make HTTP requests in Basic in LibreOffice?")))_
              .build()

Rem We assign the Json data to the HTTP Request
parameter.setJsonStructure(json)

Rem To obtain the HTTP response we use the execute() method
Rem of the OAuth2Service service with the HTTP parameter as argument
response = oauth2.execute(parameter)

If response.Ok Then
    Msgbox response.getJson().getStructure("choices").getStructure(0).getStructure("message").getString("content")
End If

Rem When it's finished we have to close the HTTP response
response.close()

End Sub
Seeing the answer we can say that ChatGPT is a big mythomaniac
ChatGPT Http request.png
ChatGPT Http request.png (36.25 KiB) Viewed 3686 times
oauth2 is defined in the idl file XOAuth2Service.idl
parameter is defined in the idl file XRequestParameter.idl
response is defined in the idl file XRequestResponse.idl
All Json interfaces (parser and builder) are defined in com.sun.star.json


Enjoy...
LibreOffice 5.3.3.2 - Lubuntu 16.10 - LxQt 0.11.0.3
Post Reply