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.
// 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?`
}
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 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;
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
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'
}
});
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();
NLx currently supports two query types and more coming soon!
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}?`
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
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.
// 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?`
// helpers.ts
import { does } from './query';
export const doesComment = does(comment);
// ...
await doesComment`meet the community guidelines?`;