Feature

OpenAI API Service Plugin

Marketplace Product

In the fast-paced world of technology, conversational AI has emerged as a powerful tool to engage with users, provide support, and automate various processes. OpenAI’s ChatGPT has been at the forefront of this revolution, showcasing its remarkable language capabilities and natural conversation flow.

Now, taking it a step further, we are thrilled to unveil the OpenAI API Service – an innovative API service that puts the power of ChatGPT directly into the hands of developers, enabling you to programmatically interact with this cutting-edge language model.

ChatGPT Logo

Unleashing the Potential of ChatGPT: A Programmable Interface

The OpenAI API service offers a seamless and structured approach to harnessing the capabilities of ChatGPT. It opens up exciting possibilities for you to integrate conversational AI into your applications, products, and services with ease. By providing a well-defined API, you can now interact with ChatGPT programmatically and unleash its full potential across various domains.

Key Features and Functionality

The core functionality of the OpenAI API service centers around creating dynamic ChatGPT sessions and conducting conversations through prompts. This allows you to simulate back-and-forths with the model, creating engaging dialogues in a user-friendly and interactive manner. The process is intuitive, and responses are generated with impressive fluency and coherence.

Seamless Conversation Management

With the OpenAI API service, managing conversations is as simple as it gets. You can easily create, maintain, and extend dialogue sessions. Each session accumulates prompts and responses, transforming the interaction into a dynamic and evolving exchange. Retrieving the entire dialogue history, including prompts and model-generated responses, is effortlessly achievable through a dedicated method.

Use Cases

The potential use cases for the OpenAI API service are diverse and far-reaching. From building chatbots that provide personalized customer support to creating interactive virtual assistants that assist users in real-time, the possibilities are limited only by your imagination.

Integrate, Innovate, and Inspire

We invite developers from all domains to explore the OpenAI API service and unlock new dimensions of creativity. By integrating this powerful API into your projects, you can harness the full potential of ChatGPT and deliver unparalleled conversational experiences to your users.

Installation Instructions

  1. Login to Backendless Console and select your app. Open the Marketplace screen, select the API Services section and install the OpenAI API plugin from the Backendless Marketplace.
  2. During the installation, you are prompted to enter your OpenAI Secret API key and an OpenAI model. The model used by default is gpt-3.5-turbo. When you use the API, you can override the default model. Click here for the instructions on how to get your OpenAI Secret API key. Enter the key and click the INSTALL button:
    Enter OpenAI API key in Backendless Console
  3. To verify the installation, click the Cloud Code icon in Backendless Console and confirm that the OpenAI API service appears in the list of services.
    OpenAI API service successfully installed in Backendless Cloud Code

Using the OpenAI API Service

To utilize the service, begin by invoking the “createSession” method. This function establishes a session capable of storing prompts and ChatGPT responses. While creating the session, you have the option to customize the GPT model by specifying a preferred model name. Moreover, the method allows the inclusion of a boolean argument “persistInDB,” which, when set to “true,” saves the chat session messages in the database. By default, the argument value is “false,” ensuring messages are not persisted unless specified otherwise.

To interact with ChatGPT and receive responses, you can utilize the “sendMessage” method. This method requires two essential arguments:

  1. Session ID: This ID is obtained from the “createSession” method, which was described in the preceding section.
  2. GPT Prompt: The prompt to be sent to the GPT engine, initiating the conversation.

Upon invoking the “sendMessage” method with the specified arguments, the response from ChatGPT will be automatically generated, added to the ongoing session dialogue, and returned by the method. This allows for a seamless and dynamic exchange with the language model, fostering an interactive and engaging conversational experience.

For developers seeking to access the complete message history of a specific session, the “getSessionMessages” method is available. This method necessitates a single argument, namely the “sessionId” that serves to uniquely identify the session of interest.

Upon successful invocation, the “getSessionMessages” method returns a structured data representation encompassing the following components:

  • Message Collection: This comprises a comprehensive compilation of messages exchanged during the session, encompassing both the prompts provided and the corresponding responses generated by ChatGPT.
  • GPT Model Information: The data structure includes details about the specific GPT model employed throughout the session, providing insights into the underlying AI model used for the conversation.

By leveraging the “getSessionMessages” method, you can seamlessly retrieve and analyze the entire dialogue history of a session, enabling you to gain valuable insights and further enhance your conversational AI applications.

API Service Methods

createSession

Method

POST

Endpoint URL

The xxxx.backendless.app is a subdomain assigned to your application. For more information, see the Client-side Setup section of the Backendless documentation.

https://xxxx.backendless.app/api/services/OpenAI/session

Request Headers

Content-Type:application/json

Request Body

{
  "aiModel":"value",
  "persistInDB": true or false
}

Request Body Parameters

  • aiModel – Optional argument. Must be a string value. If provided, overrides the default model set in the API Service configuration. Acceptable model values are documented at https://platform.openai.com/docs/models/overview
  • persistInDB – Optional argument. Must be a boolean value, false by default. When set to true, session messages are persisted in the database. In that case, the service creates two data tables: OpenAISession and OpenAIMessage. The tables contain data about created sessions and messages respectively. When the value is false (the default value), the session’s messages are stored in Backendless Cache.

Response Body

{
    "sessionId": "value"
}

Response Body Parameters

  • sessionId – Session ID value which must be used in the subsequent operations such as sendMessage and getSessionMessages 

Codeless Reference

The Codeless logic below creates a new ChatGPT session with the default model and memory-based persistence of the session’s messages:

Codeless createSession method for OpenAI API Service

sendMessage

Method

POST

Endpoint URL

The xxxx.backendless.app is a subdomain assigned to your application. For more information, see the Client-side Setup section of the Backendless documentation.

https://xxxx.backendless.app/api/services/OpenAI/message

Request Headers

Content-Type:application/json

Request Body

{
    "message": "string",
    "sessionId": "string"
}

Request Body Parameters

  • message – Require argument. Must be a string value. A prompt for the AI.
  • sessionId – Required argument. Must be a string value. Session ID value received in the response of the createSession operation.

Response Body

{
    "sessionId": "value",
    "responseMessage":"value"
}

Response Body Parameters

  • sessionId – Session ID value for which the response is provided.
  • responseMessage – AI response for the prompt specified in the request.

Codeless Reference

The Codeless logic below sends a prompt to OpenAI and receives a response. For simplicity’s sake, it is assumed that the prompt is available through data binding via Page Data. The actual response is available through the responseMessage property of the returned value.

Codeless sendMessage method for OpenAI API Service

getSessionMessages

Method

GET

Endpoint URL

The xxxx.backendless.app is a subdomain assigned to your application. For more information, see the Client-side Setup section of the Backendless documentation.

https://xxxx.backendless.app/api/services/OpenAI/session-messages?sessionId=value

Request URL Parameter

  • sessionId – Required argument. Must be a string value. Session ID value received in the response of the createSession operation.

Request Headers

Content-Type:application/json

Request Body

N/A

Response Body

{
  "messages": [
    {
      "role": "user",
      "content": "prompt1"
    },
    {
      "role": "assistant",
      "content": "response1"
    },
    {
      "role": "user",
      "content": "prompt2"
    },
    {
      "role": "assistant",
      "content": "response2"
    }
  ],
  "aiModel": "value",
  "id": "value",
  "persistInDB": true or false
}

Sample response:

{
  "messages": [
    {
      "role": "user",
      "content": "Write a description for a product that can grind coffee beans and brew coffee"
    },
    {
      "role": "assistant",
      "content": "Introducing the CoffeeGrind Brewmaster, your ultimate companion for indulging in freshly ground coffee right at home. This ingenious product seamlessly combines the functionalities of a coffee grinder and a coffee brewer, ensuring that every cup you brew is bursting with the rich aroma and robust flavors of freshly ground beans.\n\nThe CoffeeGrind Brewmaster boasts a powerful grinder, equipped with stainless steel blades that effortlessly crush coffee beans to perfection. With adjustable settings, you can easily customize the grind size, from coarse for a satisfying French press to fine for a meticulously brewed espresso. Say goodbye to the hassle of manually grinding beans or compromising on flavor with pre-ground coffee.\n\nOnce your coffee beans are ground to your desired texture, the CoffeeGrind Brewmaster seamlessly transitions into a state-of-the-art coffee brewer. Its advanced filtration system ensures that there are no impurities or residue in your cup, delivering a seamless brewing process every time. The brewer features a programmable timer, allowing you to wake up to the tantalizing aroma of freshly brewed coffee or relax with a perfectly brewed cup after a long day.\n\nDesigned with user-friendliness in mind, the CoffeeGrind Brewmaster boasts an intuitive LED control panel, where you can easily adjust settings, choose from various brewing options, and even monitor the water level to ensure an optimal brewing experience. Its sleek and modern design seamlessly complements any kitchen décor, making it an elegant addition to your countertop.\n\nSafety is also paramount, and the CoffeeGrind Brewmaster is equipped with automatic shut-off and overheating protection features, giving you peace of mind while enjoying your coffee. The removable parts are dishwasher safe, offering effortless cleaning and maintenance.\n\nIndulge in the incredible flavor and aroma of freshly ground coffee beans with the CoffeeGrind Brewmaster. Experience the true joy of having a professional-grade coffee grinder and brewer in one remarkable product. Elevate your coffee brewing experience to new heights and delight in perfect, aromatic cups of coffee every day."
    },
    {
      "role": "user",
      "content": "What is the capital of India?"
    },
    {
      "role": "assistant",
      "content": "The capital of India is New Delhi."
    }
  ],
  "aiModel": "gpt-3.5-turbo",
  "id": "258cd588-422b-4b7f-b705-d9132e9e1226",
  "persistInDB": false
}

Response Body Parameters

  • messages – An array of objects containing prompts and responses.
  • role – A string value, specifies the role of the party in the session. Can be either “user” indicating the party that sends prompts or “assistant” indicating the party (AI) providing a response.
  • content – A string value containing either a prompt or a response.
  • aiModel – A string value containing an OpenAI model name used in the session.
  • id – A string value of the session ID
  • persistInDB – A boolean value indicating whether the session messages are stored in the Backendless database.

Codeless Reference

The Codeless logic below retrieves a collection of messages for a session. The sessionMessagesData is the complete response object which contains the messages property containing a collection of the actual prompts and responses.

Codeless getSessionMessages method for OpenAI API Service