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

nlx

v0.0.11

Published

<p align="center"> <a href="https://fingerprint.com"> <picture> <source media="(prefers-color-scheme: dark)" srcset="resources/logo-light.svg" /> <source media="(prefers-color-scheme: light)" srcset="resources/logo-dark.svg" /> <im

Downloads

24

Readme

NLx is a lightweight, strictly typed query library built on top of GPT-4. By using a query interface, the LLM can be leveraged like any other JS function.

NLx can classify for you, pattern match, and perform complex logic with minimal configuration.

🪄 Demo

// client.ts

import NLx from 'nlx';
import guidelines from './guidelines.json';

// 1. Setup the client with your OpenAI API key
const client = new NLx({
  openAiConfig: {
    apiKey: 'sk-...'
  }
});

// 2. (optional) add context for use with queries
client.use('Community guidelines', guidelines);

export default client;
// example.ts

import client from './client';

/**
 * Add a new comment if it meets the community guidelines
 */
const addComment = async (comment: string) => {
  if (await client.does(comment)`meet the community guidelines?`) {
    await saveComment(comment);
  } else {
    await returnError('COMMENT_NOT_ALLOWED');
  }
}

/**
 * Basic pattern matching
 */
const includesGreeting = async () => {
  return await client.does('Hello world')`include a greeting?`
}

️✅ Getting Started

Install

With npm

npm install nlx

With pnpm

pnpm add nlx

With bun

bun install nlx

[!IMPORTANT]
Be aware that it's possible for outputs from NLx queries to change over time. Outputs are sensitive to changes OpenAI makes to GPT4.

Configuration

Configuration requires an openAiConfig object, which uses the same API as the OpenAI nodejs client.

It's recommended to store your apiKey in a .env file.

import NLx from 'nlx';

const apiKey = process.env.OPENAI_API_KEY;

// Initialize a new client
const client = new NLx({
  openAiConfig: { apiKey }
});

export default client;

Add context

Context strings can be added to the client instance from anywhere in the app. Context is passed to the LLM as a key value pair in each query.

// Add different context types to a client
import client from './client';
import document from './page.md';
import userInfo from './info.json';

client.use('Meaning of life', 42);          // Numbers
client.use('A document', document);         // Strings
client.use('User information', userInfo);   // Raw json
client.use('Is new user', true);            // Booleans

Cache

Results from OpenAI are cached based on the current context state, the query, and return type. If all three conditions are equal when a query is run, then the cached response will be returned instead.

The cache can be disabled entirely by passing cache: false to the config object:

// client.ts

const client = new NLx = ({
  cache: false,               // Disable cache for this client
  openAiConfig: {
    apiKey: 'your api key'
  }
});

Clear the cache manually

If you need to clear the cache, it can be done by calling the builtin clear method:

import client from './client';

// Clear the cache to ensure all future queries are re-run
client.cache.clear();

🌟 Query API

NLx currently supports two query types and more coming soon!

Template literals

All queries require a template literal to be passed that contains the query to be evaluated.

You can also pass variables to template literals to do things like:

await does('hello')`rhyme with ${word}?`

client.does(value)`...`: boolean

The does function takes a value string and a query string, and returns a boolean. does should be used to test for truthyness.

It's just like asking a question of your code, like "Does ABC include the letter X?"

await does('ABC')`include the letter X?`; // false

client.query(type)`...`

The query function exposes the raw query interface and can return any supported type. Use query to run less restrictive queries.

// Return a string
await client.query('string')`the first three letters of the alphabet`; // Ex: "abc"

// Return a number
await client.query('number')`return a prime number` // Ex: 3

// Return a boolean
await client.query('boolean')`is the sky blue?` // Ex: true

query always returns a response object consisting of answer, format, and confidence.

const result = await client.query('string')`the first three letters of the alphabet`;

// result = 
// {
//   "answer": "abc",       <- The query answer
//   "format": "string",    <- The requested answer format
//   "confidence": 1,       <- The LLM's confidence level
// }

Note that responses from query are not as predictable as other functions.

Tips & tricks

Alias variables

// query.ts
import client from './client';

export const does = client.does;
export const query = client.query;

// myFile.ts
import { does } from './query';

await does('a bird')`fly?`

Alias values

// helpers.ts
import { does } from './query';

export const doesComment = does(comment);

// ...

await doesComment`meet the community guidelines?`;