How to create Plugins for PulsarChat

A basic understanding of Javascript is required to create PulsarChat plugins.

Design

Plugins in PulsarChat made possbible by the OpenAI function call API. In PulsarChat, a plugin is nothing more than a collection of Javascript functions that will be executed in the browser when the AI decides to call it with the generated parameters.

If you understand the structure of plugins, you can easily create it using our built-in plugin creator. With Plugin Creator, all you need to know is the JS function code that performs the action and the function specification for that function, which follows the OpenAI function call API specs. Everything else is generated automatically.

Refer to the below image after reading the complete post to understand the post better.

Plugin Creator

It's already easier to create Plugin Creator, but there's a Plugin Creator System Prompt you can use to speed up the process.

Plugin Creator System Prompt

Now lets dive into the raw details.

Plugin JSON Structure

{
    "pluginName": "",
    "pluginDesc": "",
    "logo": "",
    "isEnabled": true,
    "functionsDataObject": {},
    "configs": {},
    "key": ""
}

Here,

  • pluginName: (String) Name of the Plugin
  • pluginDesc: (String) Description of the Plugin
  • logo: (String) URL to the Plugin Logo
  • isEnabled: (Boolean) Whether or not the plugin should be enabled by default
  • functionsDataObject: (JavaScript Object) Read further to know more
  • configs: (JavaScript Object) Read further to know more
  • key: (UUID) Unique UUID v4 string (Automatically generated if Plugin Creator is used)

Every property listed above is required.

functionDataObject

This is the most important part of the plugin because it contains both the actual code (functionDefinition) that will be executed when the GPT calls the function and specifications (functionSpecification) that describe the structure of the function in JSON schema structure, as OpenAI function API expects.

{
    [functionName]: {
        "functionName": "",
        "functionSpecification": {
            "name": "",
            "description": "",
            "parameters": {
                "type": "",
                "properties": {},
                "required": []
            }
        },
        "functionDefinition": "",
        "isEnabled": true
    }
}

Here,

  • [functionName]: (String) Replace this with a your javascript function name. .
  • functionName: (String) Should be same as the functionName.
  • functionSpecification: (JS Object) JSON Schema of the function according to OpenAI function call API specs. A JSON with "name", "description" and "parameters" keys are required.
  • functionDefinition: (String) This is where you define the actual JavaScript code that implements the functionality of the function. You can use this section to write the logic that processes the input parameters and performs the desired action. The function should be defined as an asynchronous function if necessary. The function should take only 2 inputs, JS Objects called params and configs

Where, params is all the arguements that the javascript function normally would recieve as inputs put inside a js ocject. and configs are use to send user configs like api keys that the function needs to function.

This configs is would recieve the configs object defined in the top level, you saw this above.

  • isEnabled: (Boolean) Set this property to true if you want the function to be enabled by default. If set to false, users will need to enable it manually to use it in their PulsarChat instance.

This structure allows you to define multiple functions within a PulsarChat plugin, each with its own set of parameters, descriptions, and implementation logic. These functions can provide various features and actions within your plugin, enhancing its functionality for users.

Example Plugin

An example plugin file would make this way simpler to understand.

Example:

{
  "pluginName": "Weather",
  "pluginDesc": "Allows users to fetch current and forecasted weather information based on location. You MUST ALWAYS convert the plugin response to the units that are most useful to your user, when in doubt assume USA/Enlish units.",
  "logo": "https://weather--vicentescode.repl.co/logo.png",
  "isEnabled": true,
  "functionsDataObject": {
    "getWeatherForecast": {
      "functionName": "getWeatherForecast",
      "functionSpecification": {
        "name": "getWeatherForecast",
        "description": "Get up to 16 day weather forecast (first day is always the current date) based on city, state, and country. THINK STEP BY STEP TO ADJUST THE NUMBER OF DAYS IN THE QUERY CNT TO COVER THE DAYS THE USER WANTS.",
        "parameters": {
          "type": "object",
          "properties": {
            "cnt": {
              "type": "string",
              "description": "Number of days up to 16 for weather forecast (optional). First day is always the current date. THINK STEP BY STEP TO ADJUST THE NUMBER OF DAYS IN THE QUERY CNT TO COVER THE DAYS THE USER WANTS."
            },
            "city": {
              "type": "string",
              "description": "The city name."
            },
            "state": {
              "type": "string",
              "description": "The state code (optional)."
            },
            "country": {
              "type": "string",
              "description": "The country code."
            }
          },
          "required": [
            "city",
            "country"
          ]
        }
      },
      "functionDefinition": "async function getWeatherForecast(params, configs) {\n  const url = 'https://weather--vicentescode.repl.co/weatherforecast';\n  const { cnt, city, state, country } = params;\n  let query = `?city=${city}&country=${country}`;\n  if (state) query += `&state=${state}`;\n  if (cnt) query += `&cnt=${cnt}`;\n  try {\n    const response = await fetchOnCloud(url + query);\n    if (!response.ok) return `Request failed with status ${response.status}`;\n    const data = await response.json();\n    return data;\n  } catch (error) {\n    return `Error: ${error.message}`;\n  }\n}",
      "isEnabled": true
    },
  "configs": {},
  "key": "078bd051-ec9d-427d-8b90-ab9e8ec10133"
}

You might be wondering what's fetchOnCloud function? Well, its just plain old javascript fetch that will make the request through PulsarChat's cloudflare worker to avoid CORS errors.