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

ai-complete

v0.0.2

Published

A toolkit that super-charges your workflow when working with openai.

Downloads

5

Readme

ai-complete

A toolkit that super-charges your workflow when working with openai.

Install

npm install ai-complete
# or
yarn add ai-complete
# or
pnpm add ai-complete

Note: You will need to get an API key from OpenAI.

Usage

In the following example we use ai-complete to translate a directory containing a bunch JSON language files. Our goal is to translate the files into another language and write the translated files to a new directory.

Note: You can run this example by cloning this repo and running yarn example.

import 'dotenv/config'

import AIComplete from 'ai-complete'
import { existsSync, mkdirSync, writeFileSync } from 'fs'

const inputDir = 'locales/en'
const outputDir = 'locales/ru'

await aic.createCompletion({
  // Where to read files from. All globby pattern and options are supported.
  globby: {
    patterns: ['example/locales/en/**/*.json']
  },
  // This function is called for each file and the results are used as arguments for the OpenAI API.
  input: async ({ args, filePath, fileContent }) => {
    return {
      // Describe what you want to do with the file.
      prompt:
        'Translate the JSON below into Russian but keep names of all keys and metadata in English.',
      createCompletionRequest: {
        // controls randomness, as value approaches 0 the output will be more deterministic
        temperature: 0
      }
    }
  },
  // This function is called with the results from each OpenAI API call.
  output: async ({ data, filePath }) => {
    // Parse the results from JSON to JS.
    const choice = JSON.parse(data.choices[0].text)

    // Calculate the new file path of the translated file.

    const outputFilePath = filePath.replace(inputDir, outputDir)

    const dir = outputFilePath.substring(0, outputFilePath.lastIndexOf('/'))

    if (!existsSync(dir)) {
      mkdirSync(dir, { recursive: true })
    }

    // Write the results to the file.

    const fileContents = JSON.stringify(choice, null, 2)

    try {
      writeFileSync(outputFilePath, fileContents, { flag: 'wx' })
      console.log('Wrote file: ' + outputFilePath)
    } catch (error) {
      console.error('Error writing file: ' + outputFilePath)
      console.error(error.message)
    }

    return {
      choice
    }
  }
})

Running examples and tests

  1. Create a .env file in the root of the project and add your OPENAI_API_KEY to it.
  2. Run yarn example to run the translate.js example.
  3. Run yarn test to run all the tests.