npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

remark-openai

v1.0.1

Published

A remark plugin to use code blocks as placeholder for generated content from openai

Downloads

14

Readme

remark-openai

What is it?

It's a remark plugin which transforms code blocks (marked with language openai) into generated content using the OpenAI api.

When should I use?

You want to quickly generate formatted content using openai, probably to be consumed later by a static site generator like Astro.

How to use it?

Simply add the plugin to the list of plugins you're using already in your remark(/rehype) pipeline. You'll need to pass your own api key which you can generate here

For example:

import { unified } from "unified";

import remarkParse from "remark-parse";
import remarkOpenAi from "remark-openai";
import remarkHtml from "remark-html";

const { value } = await unified()
  .use([
    remarkParse,
    [remarkOpenAi, { apiKey: '<YOUR API KEY HERE>' }],
    remarkHtml,
  ])
  .process();

console.log(value)

Testing locally

If you want to integrate this plugin into your pipeline but keep developing with it, you may want to avoid calling the OpenAI API all the time. For this, you can pass a mock function which will be called in place of the AI engine job.

Simply add the plugin to the list of plugins you're using already in your remark(/rehype) pipeline. You'll need to pass your own api key which you can generate here

For example:

import { unified } from "unified";

import remarkParse from "remark-parse";
import remarkOpenAi from "remark-openai";
import remarkHtml from "remark-html";

const { value } = await unified()
  .use([
    remarkParse,
    [remarkOpenAi, {
      apiKey: '<YOUR API KEY HERE>',
      mock: (prompt) => prompt,
    }],
    remarkHtml,
  ])
  .process();

console.log(value)

Advanced usage

If you require to pass some options to the OpenAI client (for debugging or else), you can omit the apiKey parameter and pass a client instance directly.

import { unified } from "unified";

import remarkParse from "remark-parse";
import remarkOpenAi from "remark-openai";
import remarkHtml from "remark-html";

import { Configuration, OpenAIApi } from "openai";

const { value } = await unified()
  .use([
    remarkParse,
    [remarkOpenAi, {
      client: new OpenAIApi(
        new Configuration({ apiKey: '<YOUR API KEY HERE>' })
      ),
    }],
    remarkHtml,
  ])
  .process();

console.log(value)

How to configure it?

Globally

Aside from apiKey and client, all the other parameters will be passed along as the plugin makes calls to the OpenAI Api. This gives you an opportunity to create a global configuration for all your generated content:

import { unified } from "unified";

import remarkParse from "remark-parse";
import remarkOpenAi from "remark-openai";
import remarkHtml from "remark-html";

const { value } = await unified()
  .use([
    remarkParse,
    [remarkOpenAi, {
      apiKey: '<YOUR API KEY HERE>',
      model: 'text-davinci-003',
      max_tokens: 1024,
    }],
    remarkHtml,
  ])
  .process();

console.log(value)

Per code block

In your markdown content, the metadata found next to the code block will be parsed using json5 and used as parameters for your api call. This gives you opportunity to try different sets of parameters concurrently on the same document.

    ## Using GPT3.5

    ```openai
    explain the benefits of ai generated content in less than 100 words
    ```

    ## Using custom model

    ```openai { model: 'text-davinci-003' }
    explain the benefits of ai generated content in less than 100 words
    ```

Creating adapters

If you're not happy with the existing presets, you can always override them. You can build your own using the adapters option such as:

import { unified } from "unified";

import remarkParse from "remark-parse";
import remarkOpenAi from "remark-openai";
import remarkHtml from "remark-html";

const { value } = await unified()
  .use([
    remarkParse,
    [remarkOpenAi, {
      apiKey: '<YOUR API KEY HERE>',
      adapters: {
        'this-new-adapter': {
          model: 'gpt-4-32k',
          call: 'createChatCompletion',
          prompt: (prompt) => ({ messages: [{ role: 'user', content: prompt }] }),
          parse: ({ choices }) => choices[0].message.content,
        }
      },
    }],
    remarkHtml,
  ])
  .process();

console.log(value)

The keyword this-new-adapter will be matched when used in your markdown content like this:

    ```openai { adapter: "this-new-adapter" }
    create a poem about generated content from ai
    ```