Use ONLYOFFICE macro to generate an essay with ChatGPT

21 February 2023By Serge

AI technology has significantly improved over several years and become an essential part of our daily lives. Today we will incorporate this high-tech functionality into our document writing routine. In this blog post, we will show you how to build a macro that will generate an entire essay with ChatGPT API.

Use ONLYOFFICE macro to generate an essay with ChatGPT

About ChatGPT

ChatGPT is an innovative AI chatbot developed by OpenAI that can converse naturally. Its dialogue design allows it to respond to follow-up questions, recognize its errors, generate text, and dispute incorrect statements. The model is trained to follow the instruction in a prompt and provide a detailed response.

OpenAI provides a free trial API that facilitates integrating into various applications. For convenience purposes, we will get access to OpenAI API through the RapidAPI platform. To connect to the  OpenAI API directly, visit the official documentation page.

About RapidAPI

RapidAPI is a comprehensive platform for developers to discover, connect to, and manage APIs. It contains a directory of over 10,000 APIs, including popular ones like Google Maps, Twilio, Stripe, and YouTube. Users can search for APIs, try out free trials, and subscribe to APIs and services. RapidAPI also offers a dashboard to monitor API subscriptions, usage, and performance, as well as access support. Additionally, RapidAPI enables developers to create their APIs and monetize them.

Forming the API request

In our project, we incorporated the You Chat GPT API by FB/florianbreut. Currently, it’s offers a freemium plan that starts with a basic package of 100 requests a month. It will be sufficient to test our macro.

The interface of the RapidAPI platform generates request code snippets in various languages. In our case choices will be JavaScriot and a fetch function:

Use ONLYOFFICE macro to generate an essay with ChatGPT

Also, we can tweak the request parameters. In particular, we can change the request body. The API offers 4 request options, depending on the OpenAI model:

  1. Write Code
  2. Explain Anything
  3. Time Response Constraint
  4. Formatted Answer

The Formatted Answer option is perfectly suited for essay generation. So we pick it and change the max_response_time parameter to 20 seconds. This will give the OpenAI more time to generate the answer:

Use ONLYOFFICE macro to generate an essay with ChatGPT

Building the macro

Now let’s incorporate this fetch request into our macro!

First, we create a global variable that will store the response:

let essay;

Then we add the request options and set the topic of the essay by changing the question parameter in the request body:

const options = {
  method: 'POST',
  headers: {
    'content-type': 'application/json',
    'X-RapidAPI-Key': '<yourRapidAPIkey>',
    'X-RapidAPI-Host': 'you-chat-gpt.p.rapidapi.com'
  },
  body: '{"question":"write an essay on the benefits of using ONLYOFFICE products","max_response_time":20}'
};

After that, we define the insertEssay function. This function contains ONLYOFFICE API methods that insert the generated essay into the document.

First, we target the active document and create a paragraph where the text will be added:

function insertEssay() {
  const oDocument = Api.GetDocument();
  const oParagraph = Api.CreateParagraph();
  
}

Then we add the generated essay to the paragraph and insert it with the InsertContent method.  We also pass in the “KeepTextOnly” parameter to prevent any possible formatting issues:

 function insertEssay() {
  const oDocument = Api.GetDocument();
  const oParagraph = Api.CreateParagraph();
  oParagraph.AddText(essay);
  oDocument.InsertContent([oParagraph], { "KeepTextOnly": true })
  

We also incorporate the handleFetchResponse function. It extracts the answer from the response and fires the insertEssay function to insert it into the document:

function handleFetchResponse(response) {
  return response.json().then(json => {
    essay = json.answer;
    insertEssay();
  });
}

And we make a fetch request to the server:

fetch('https://you-chat-gpt.p.rapidapi.com/', options)
  .then(handleFetchResponse);

The entire macro code is the following:

(function()
{ 
let essay;
const options = {
  method: 'POST',
  headers: {
    'content-type': 'application/json',
    'X-RapidAPI-Key': '<yourRapidAPIkey>',
    'X-RapidAPI-Host': 'you-chat-gpt.p.rapidapi.com'
  },
  body: '{"question":"write an essay on the benefits of using ONLYOFFICE products","max_response_time":20}'
};

function insertEssay() {
    const oDocument = Api.GetDocument();
    const oParagraph = Api.CreateParagraph();
    oParagraph.AddText(essay);
    oDocument.InsertContent([oParagraph], { "KeepTextOnly": true });
}

function handleFetchResponse(response) {
  return response.json().then(json => {
    essay = json.answer;
    insertEssay();
  });
}

fetch('https://you-chat-gpt.p.rapidapi.com/', options)
  .then(handleFetchResponse);
})();

Now let’s run our macro and see how it works!

Note!

  • This macro is only operational in the online version of the editors.
  • To get 100 free requests, subscribe to You Chat GPT API and use your RapidAPI key.
  • It takes approximately 30 seconds to process the request before inserting it into the document.
  • Press the space to insert the text.

We hope that this macro will bring new fascinating AI technologies into your workflow and alleviate tedious routine tasks. We urge developers to take advantage of the possibilities that our API methods offer and build their macros. Our macros are written in JavaScript, making them highly functional and adaptive.

If you have any inquiries or suggestions, please don’t hesitate to reach out. We are open to hearing your ideas and are eager to collaborate with you. Best of luck in your exploratory endeavors!

 

Create your free ONLYOFFICE account

View, edit and collaborate on docs, sheets, slides, forms, and PDF files online.