New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

format-gpt

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

format-gpt

ChatGPT output formatter.

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
decreased by-54.55%
Maintainers
1
Weekly downloads
 
Created
Source
format-gpt Sanitize ChatGPT output and format it the way you want!

FormatGPT is a robust library designed to sanitize output provided by ChatGPT. It provides a consistent and well defined methods for retrieving the data from ChatGPT by acting as a middle man between your code and ChatGPT API. It supports various formats such as:

  • text
  • json
  • json-list
  • csv
  • html
  • markdown
  • arrays
  • tables
  • xml
  • yaml

Additionally each output format can be easily customized to suit your needs.

Table of Contents

  • Table of Contents
  • Installation
  • Usage
  • API Reference
  • Building
  • Tests
  • License

Installation

To install the library using npm run the command:

npm install format-gpt

To install the library using yarn run the command:

yarn add format-gpt

Usage

Here is quick example on how to format prompts for chat-gpt:

import {formatGptPrompt} from "format-gpt";

formatGpt.format(prompt, {
    format: "json-list",
    language: "en",
    options: {
        attributes: [
            {name: "name", type: "string"},
            {name: "description", type: "string", maxLength: 200},
            {name: "released", type: "date"},
            {name: "price", type: "decimal"},
        ],
    },
});

And another example on how communicate with chat-gpt through provided wrapper:

import FormatGPT from "format-gpt";
import {Configuration, OpenAIApi} from "openai";

// Create OpenAIApi instance
const configuration = new Configuration({
    organization: "YOUR_ORGANIZATION_ID",
    apiKey: "YOUR_API_KEY",
});
const openai = new OpenAIApi(configuration);

// Initialize GptFormatter providing OpenAIApi instance
const formatter = new GptFormatter(openai);

// Use provided FormatGPT wrapper methods instead of those from OpenAIApi.
// Below is an example using `createChatCompletion` method.
const result = await formatter.createChatCompletion(request, options, output);

Parameters request and options are the same ones as passed to createChatCompletion method from openai library.

Parameter output is where the magic happens.
You define how you would like the output to be structured and under the hood FormatGPT transforms your requests and prompts to achieve desired result.

API Reference

The default export is formatChatGptPrompt.

formatGptPrompt

formatGptPrompt(prompt: string, output: IOutputConfig): string;
  • prompt (string) - content of the prompt for chat-gpt
  • output (IOutputConfig)
    • format (Format | string) - format of data retrieved from chat-gpt (described below)
    • language (string) - language code (determines language of the output, i.e. "en", "de")
    • size (number)- expected tokens count (used for reporting progress when streaming)
    • options: (IFormatOptions)
      • attributes: (IAttribute[])- array with attribute definitions
      • columnSeparator (string)- column separator (for CSV format, default: ",")
      • rowSeparator (string)- row separator (for CSV format. default: \n)

formatGptMessages

formatGptMessages(messages: ChatCompletionRequestMessage[], output: IOutputConfig): ChatCompletionRequestMessage[];
  • messages (ChatCompletionRequestMessage[]) - messages sent to chat-gpt api
  • output (IOutputConfig) - output configuration

formatGptRequest

formatGptRequest(request: CreateChatCompletionRequest, output: IOutputConfig): CreateChatCompletionRequest;
  • request (CreateChatCompletionRequest) - request config for chat-gpt api
  • output (IOutputConfig) - output configuration

Format

type Format = "string"; // You can use one of the predefined formats

Available predefined formats:

  • text - plain text string
  • array - javascript array (["item1", "item2", "item3"])
  • csv - text in CSV format (configure column and line separator with IOutputConfig)
  • html - string containing html
  • html-table - string containing table in html format
  • markdown - string containing markdown
  • markdown-table - string containing table in markdown format
  • json - JSON object
  • json-list - list of JSON objects (e.g. [{ a: "A", b: "B"}, { a: "A", b: "B"}, ...])
  • xml - string containing xml
  • yaml - string containing yaml

IAttribute

interface IAttribute {
    name: string;
    type: "string" | "number" | "boolean" | "date" | "integer" | "decimal" | IAttribute[];
    minLength?: number;
    maxLength?: number;
    custom?: string;
}
  • name - display name of the attribute in output data
  • type - simple type or an array of attributes (IAttribute[]), also try experimenting with other types
  • minLength - minimum length of the attribute (applicable to text)
  • maxLength - maximum length of the attribute (applicable to text)
  • custom - additional attrribute description (e.g. "uppercase", "truncate to 2 decimal places")

For more information on ChatCompletionRequestMessage and CreateChatCompletionRequest please refer OpenAI documentation.

Building

To build format-gpt follow these steps:

  1. Clone this repository
  2. Install dependencies using npm install or yarn install command
  3. If using yarn with Visual Studio Code also run yarn dlx @yarnpkg/sdks vscode
  4. Run npm build or yarn build command

Tests

To run the test suite, first install the dependencies, then execute tests.

Using npm:

npm install
npm test

Using yarn:

yarn install
yarn test

License

This project is licensed under the MIT License.

FAQs

Package last updated on 02 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc