llm-primitives v1.0.0
LLM Primitives
Basic software building blocks using LLMs.
Use LLMs to get things like Boolean, Enum selection, Int, Float, and Date responses. And of course strings.
It also includes built in streaming, caching, and cost breakdowns grouped by arbitrary identifiers.
Why does this exist?
Because when developing things like "agents" or when LLMs are part of complex workflows, sometimes we need the basics. The most useful operation tend to be booleans and enum choice selection for branching logic, the others exist because they are also occasionally useful.
I have been using and evolving this library successfully since 2023 in production, and decided to open source it. The original used Postgres for caching, this uses SQLite
Note: This only works with OpenAI right now, and you need an API Key.
How to use it
Install the module:
npm install llm-primitives
Then import it (ESM only) and instantiate the llm class.
import LLM from 'llm-primitives'
const llm = new LLM({
apiKey:process.env.OPENAI_API_KEY,
model:"gpt-4o-mini"
});
With the class, you can then use the methods in the examples below. Each will return a properly casted object, or will return null
and log an exception if there was a problem.
bool
const answer = await llm.bool("On a clear day, the sky is blue.");
//answer == true
enum
const options = ["blue","green","red"]
const answer = await llm.enum("On a clear day, the sky is the following color.",options);
//answer == 'blue'
int
const answer = await llm.int("What is 2+2?");
//answer == 4
float
const answer = await llm.float("What is Pi to the 5th decimal place?");
//answer==3.14159
date
const answer = await llm.date("When did Niel Armstrong walk on the moon?");
//answer==1969-07-20T00:00:00.000Z
string
const answer = await llm.string("In markdown, write a bulleted list of the four bending elements from Avatar: The Last Airbender.");
/*answer == `Sure! Here’s a bulleted list of the four bending elements from *Avatar: The Last Airbender*:
- Water
- Earth
- Fire
- Air`
*/
Made with ❤️ by Max Irwin
4 months ago