@themaximalist/ai.js
Version:
AI Toolkit for Node.js (LLM, Image Generation, Embeddings, Vector Search)
314 lines (312 loc) • 23 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<meta name="description" content="Simple interfaces to LLMs, image generators, embeddings, vector databases and more" />
<title>AI.js — AI Toolkit for Node.js</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
/* The extra [class] is a hack that increases specificity enough to
override a similar rule in reveal.js */
ul.task-list[class]{list-style: none;}
ul.task-list li input[type="checkbox"] {
font-size: inherit;
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
.display.math{display: block; text-align: center; margin: 0.5rem auto;}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
background-color: #2a211c;
color: #bdae9d;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #bdae9d; padding-left: 4px; }
div.sourceCode
{ color: #bdae9d; background-color: #2a211c; }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ffff00; } /* Alert */
code span.an { color: #0066ff; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { } /* Attribute */
code span.bn { color: #44aa43; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #43a8ed; font-weight: bold; } /* ControlFlow */
code span.ch { color: #049b0a; } /* Char */
code span.cn { } /* Constant */
code span.co { color: #888888; font-style: italic; } /* Comment */
code span.do { color: #0066ff; font-style: italic; } /* Documentation */
code span.dt { text-decoration: underline; } /* DataType */
code span.dv { color: #44aa43; } /* DecVal */
code span.er { color: #ffff00; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #44aa43; } /* Float */
code span.fu { color: #ff9358; font-weight: bold; } /* Function */
code span.im { } /* Import */
code span.in { color: #0066ff; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #43a8ed; font-weight: bold; } /* Keyword */
code span.op { } /* Operator */
code span.pp { font-weight: bold; } /* Preprocessor */
code span.sc { color: #049b0a; } /* SpecialChar */
code span.ss { color: #049b0a; } /* SpecialString */
code span.st { color: #049b0a; } /* String */
code span.va { } /* Variable */
code span.vs { color: #049b0a; } /* VerbatimString */
code span.wa { color: #ffff00; font-weight: bold; } /* Warning */
</style>
<link rel="stylesheet" href="style.css" />
<script defer data-domain="aijs.themaximalist.com" src="https://s.cac.app/js/script.outbound-links.js"></script>
<meta property="og:url" content="https://aijs.themaximalist.com/">
<meta property="og:type" content="website">
<meta property="og:title" content="AI.js — AI Toolkit for Node.js">
<meta property="og:description" content="Simple interfaces to LLMs, image generators, embeddings, vector databases and more">
<meta property="og:image" content="https://aijs.themaximalist.com/social.png">
<meta name="twitter:card" content="summary_large_image">
<meta property="twitter:domain" content="aijs.themaximalist.com">
<meta property="twitter:url" content="https://aijs.themaximalist.com/">
<meta name="twitter:title" content="AI.js — AI Toolkit for Node.js">
<meta name="twitter:description" content="Simple interfaces to LLMs, image generators, embeddings, vector databases and more">
<meta name="twitter:image" content="https://aijs.themaximalist.com/social.png">
</head>
<body>
<a class="fork-me" href="https://github.com/themaximal1st/ai.js"><img decoding="async" width="149" height="149" src="https://github.blog/wp-content/uploads/2008/12/forkme_right_darkblue_121621.png?resize=149%2C149" class="attachment-full size-full" alt="Fork me on GitHub" loading="lazy" data-recalc-dims="1"></a>
<div id="content" class="gap-md">
<header id="title-block-header">
<h1 class="title">AI.js — AI Toolkit for Node.js</h1>
</header>
<nav id="TOC" role="doc-toc">
<ul>
<li><a href="#ai.js" id="toc-ai.js">AI.js</a></li>
<li><a href="#features" id="toc-features">Features</a></li>
<li><a href="#installation" id="toc-installation">Installation</a></li>
<li><a href="#llm" id="toc-llm">LLM</a></li>
<li><a href="#images" id="toc-images">Images</a>
<ul>
<li><a href="#imagine-concepts" id="toc-imagine-concepts">Imagine
Concepts</a></li>
</ul></li>
<li><a href="#embeddings" id="toc-embeddings">Embeddings</a></li>
<li><a href="#vector-search" id="toc-vector-search">Vector
Search</a></li>
<li><a href="#api" id="toc-api">API</a></li>
<li><a href="#debug" id="toc-debug">Debug</a></li>
<li><a href="#projects" id="toc-projects">Projects</a></li>
<li><a href="#license" id="toc-license">License</a></li>
<li><a href="#author" id="toc-author">Author</a></li>
</ul>
</nav>
<h2 id="ai.js">AI.js</h2>
<p><img src="logo.png" alt="AI.js — AI Toolkit for Node.js" class="logo" style="max-width: 300px;" /></p>
<div class="badges" style="text-align: center; margin-top: 0px;">
<p><a href="https://github.com/themaximal1st/ai.js"><img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/themaximal1st/ai.js"></a>
<a href="https://www.npmjs.com/package/@themaximalist/ai.js"><img alt="NPM Downloads" src="https://img.shields.io/npm/dt/%40themaximalist%2Fai.js"></a>
<a href="https://github.com/themaximal1st/ai.js"><img alt="GitHub code size in bytes" src="https://img.shields.io/github/languages/code-size/themaximal1st/ai.js"></a>
<a href="https://github.com/themaximal1st/ai.js"><img alt="GitHub License" src="https://img.shields.io/github/license/themaximal1st/ai.js"></a></p>
</div>
<p><br /></p>
<p><code>AI.js</code> is the easiest way to add AI text, images,
embeddings and vector search to your Node.js app.</p>
<div class="sourceCode" id="cb1"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="cf">await</span> <span class="fu">AI</span>(<span class="st">"the color of the sky is"</span>)<span class="op">;</span> <span class="co">// blue</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="cf">await</span> AI<span class="op">.</span><span class="fu">Image</span>(<span class="st">"a red rose"</span>)<span class="op">;</span> <span class="co">// <image buffer: red rose></span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="cf">await</span> AI<span class="op">.</span><span class="at">Image</span><span class="op">.</span><span class="fu">Concept</span>(<span class="st">"a red rose"</span>)<span class="op">;</span> <span class="co">// {prompt: a red rose in realist style, watercolor ...", <image buffer>}</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="cf">await</span> AI<span class="op">.</span><span class="fu">Embeddings</span>(<span class="st">"hello world"</span>)<span class="op">;</span> <span class="co">// Array(384)</span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> db <span class="op">=</span> <span class="kw">new</span> AI<span class="op">.</span><span class="fu">VectorDB</span>()<span class="op">;</span></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="cf">await</span> db<span class="op">.</span><span class="fu">add</span>(<span class="st">"red"</span>)<span class="op">;</span></span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="cf">await</span> db<span class="op">.</span><span class="fu">add</span>(<span class="st">"blue"</span>)<span class="op">;</span></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="cf">await</span> db<span class="op">.</span><span class="fu">search</span>(<span class="st">"redish"</span>) <span class="co">// red</span></span></code></pre></div>
<p>Under the hood <code>AI.js</code> seamlessly integrates easy to use
local and remote APIs</p>
<ul>
<li><strong>Text:</strong> <a
href="https://llmjs.themaximalist.com">LLM.js</a> use
<code>GPT-4</code>, <code>Gemini</code>, <code>Claude</code>,
<code>Mistral</code> or <code>LLaMa</code> in same simple interface</li>
<li><strong>Image:</strong> <a
href="https://imaginejs.themaximalist.com/">Imagine.js</a> supports
local <code>Stable Diffusion</code> and remote services like
<code>Replicate</code> and <code>Stability AI</code></li>
<li><strong>Embeddings:</strong> <a
href="https://embeddingsjs.themaximalist.com/">Embeddings.js</a> create
<code>local</code>, <code>OpenAI</code> or <code>Mistral</code>
embeddings</li>
<li><strong>VectorDB:</strong> <a
href="https://vectordbjs.themaximalist.com/">VectorDB.js</a> searches
similar embeddings in memory</li>
<li><strong>Audio:</strong> <em>Coming Soon</em></li>
<li><strong>Video:</strong> <em>Coming Soon</em></li>
</ul>
<h2 id="features">Features</h2>
<ul>
<li>Easy to use</li>
<li>Same simple interface for hundreds of models (OpenAI, Google,
Anthropic, Mistral, LLaMa, Replicate, Stability AI, Hugging Face and
more)</li>
<li>Works locally and offline by default!</li>
<li>Offers best-in-class options through remote APIs</li>
<li>Advanced LLM features like one-shot prompts, chat history, streaming
and JSON schema and more</li>
<li>Image generation using the best open and paid models</li>
<li>Image concepts to easily combine LLMs with Image generators for
impressive results</li>
<li>Easy to use text embeddings and in-memory vector search</li>
<li>MIT license</li>
</ul>
<h2 id="installation">Installation</h2>
<p>Install <code>AI.js</code> via npm</p>
<div class="sourceCode" id="cb2"><pre
class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ex">npm</span> install @themaximalist/ai.js</span></code></pre></div>
<p>Enable at least one service by setting its environment
<code>API_KEY</code></p>
<div class="sourceCode" id="cb3"><pre
class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="bu">export</span> <span class="va">OPENAI_API_KEY</span><span class="op">=</span>sk-...</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="bu">export</span> <span class="va">ANTHROPIC_API_KEY</span><span class="op">=</span>sk-ant-...</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="bu">export</span> <span class="va">GOOGLE_API_KEY</span><span class="op">=</span>sk-ant-...</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="bu">export</span> <span class="va">STABILITY_API_KEY</span><span class="op">=</span>sk-...</span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="bu">export</span> <span class="va">REPLICATE_API_KEY</span><span class="op">=</span>sk-....</span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="bu">export</span> <span class="va">MISTRAL_API_KEY</span><span class="op">=</span>...</span></code></pre></div>
<h2 id="llm">LLM</h2>
<p>The default interface is text. <code>AI.js</code> lets you send
one-off requests or build up complex message histories with the Large
Language Model (LLM).</p>
<div class="sourceCode" id="cb4"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> AI <span class="op">=</span> <span class="pp">require</span>(<span class="st">"@themaximalist/ai.js"</span>)<span class="op">;</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="cf">await</span> <span class="fu">AI</span>(<span class="st">"what is the codeword?"</span>)<span class="op">;</span> <span class="co">// i don't know any codewords</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> ai <span class="op">=</span> <span class="kw">new</span> <span class="fu">AI</span>(<span class="st">"the codeword is blue"</span>)<span class="op">;</span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="cf">await</span> ai<span class="op">.</span><span class="fu">chat</span>(<span class="st">"what is the codeword?"</span>)<span class="op">;</span> <span class="co">// blue</span></span></code></pre></div>
<p>The default <code>AI.js</code> mode is <code>LLM</code>, and is
running on top of <a href="https://llmjs.themaximalist.com">LLM.js</a>.
Please see that site for full documentation, that also applies to
<code>AI.js</code>.</p>
<p>There are many features supported across dozens of popular models,
like streaming, JSON support, max_tokens, temperature, seed and
more.</p>
<h2 id="images">Images</h2>
<p><code>AI.js</code> provides powerful image generation functions
through <code>Automatic1111</code>, <code>StabilityAI</code> and
<code>Replicate</code>. Make sure you have each service setup as needed,
either running locally or a valid environment variable.</p>
<div class="sourceCode" id="cb5"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> image <span class="op">=</span> <span class="cf">await</span> AI<span class="op">.</span><span class="fu">Image</span>(<span class="st">"a red rose"</span>)<span class="op">;</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>fs<span class="op">.</span><span class="fu">writeFileSync</span>(<span class="st">"rose.png"</span><span class="op">,</span> image)<span class="op">;</span></span></code></pre></div>
<p>See <a href="https://imaginejs.themaximalist.com/">Imagine.js</a> for
all image generator documentation.</p>
<h3 id="imagine-concepts">Imagine Concepts</h3>
<p><code>AI.js</code> also provides a concept generator—a way of using
LLMs together with image generators.</p>
<div class="sourceCode" id="cb6"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> { prompt<span class="op">,</span> buffer } <span class="op">=</span> <span class="cf">await</span> AI<span class="op">.</span><span class="at">Image</span><span class="op">.</span><span class="fu">Concept</span>(<span class="st">"a red rose"</span>)<span class="op">;</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(prompt)<span class="op">;</span> <span class="co">// a red rose in realist style, watercolor ..."</span></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a>fs<span class="op">.</span><span class="fu">writeFileSync</span>(<span class="st">"complex-rose.png"</span><span class="op">,</span> buffer)<span class="op">;</span></span></code></pre></div>
<p>This hits your <code>LLM</code> provider and generates a complex
image prompt before sending it off to the image generation service.</p>
<h2 id="embeddings">Embeddings</h2>
<p><code>AI.js</code> let’s you easily generate local or remote
embeddings using <code>Embeddings.js</code>.</p>
<div class="sourceCode" id="cb7"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> embeddings <span class="op">=</span> <span class="cf">await</span> AI<span class="op">.</span><span class="fu">Embeddings</span>(<span class="st">"hello world"</span>)<span class="op">;</span> <span class="co">// embedding array</span></span></code></pre></div>
<p>It works with local embeddings, OpenAI and Mistral.</p>
<p>Embeddings can be used in any vector database like Pinecone, Chroma,
PG Vector, etc…</p>
<p>See <a
href="https://embeddingsjs.themaximalist.com/">Embeddings.js</a> for all
options.</p>
<h2 id="vector-search">Vector Search</h2>
<p><code>AI.js</code> let’s you quickly find similar text strings using
a vector database.</p>
<p>It runs in-memory and can use embeddings from <code>AI.js</code>.</p>
<p>To find similar strings, add a few to the database, and then
search.</p>
<div class="sourceCode" id="cb8"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> db <span class="op">=</span> <span class="kw">new</span> AI<span class="op">.</span><span class="fu">VectorDB</span>()<span class="op">;</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="cf">await</span> db<span class="op">.</span><span class="fu">add</span>(<span class="st">"orange"</span>)<span class="op">;</span></span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a><span class="cf">await</span> db<span class="op">.</span><span class="fu">add</span>(<span class="st">"blue"</span>)<span class="op">;</span></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> result <span class="op">=</span> <span class="cf">await</span> db<span class="op">.</span><span class="fu">search</span>(<span class="st">"light orange"</span>)<span class="op">;</span></span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a><span class="co">// [ { input: 'orange', distance: 0.3109036684036255 } ]</span></span></code></pre></div>
<p>See <a href="https://vectordbjs.themaximalist.com/">VectorDB.js</a>
for full documentation.</p>
<h2 id="api">API</h2>
<p><code>AI.js</code> includes these sub-projects:</p>
<ul>
<li><a href="https://llmjs.themaximalist.com">LLM.js</a> — Simple
interface to dozens of Large Language Models</li>
<li><a href="https://imaginejs.themaximalist.com">Imagine.js</a> — Local
and remote image generation library</li>
<li><a href="https://embeddingsjs.themaximalist.com">Embeddings.js</a>
— Simple local or OpenAI text embeddings</li>
<li><a href="https://vectordbjs.themaximal1st.com">VectorDB.js</a>
— Local text similarity search</li>
</ul>
<p>Check out each individual project for full API documentation.</p>
<p>They can be used individually or together like in
<code>AI.js</code>.</p>
<h2 id="debug">Debug</h2>
<p><code>AI.js</code> uses the <code>debug</code> npm module across all
of it’s sub-projects.</p>
<p>The namespace is the lowercase version of the project name.</p>
<p>View debug logs by setting the <code>DEBUG</code> environment
variable.</p>
<div class="sourceCode" id="cb9"><pre
class="sourceCode bash"><code class="sourceCode bash"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="op">></span> DEBUG=llm.js<span class="pp">*</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="op">></span> node <span class="ex">src/run_ai.js</span></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a><span class="co"># debug logs</span></span></code></pre></div>
<p>You can combine multiple logs with a comma.</p>
<div class="sourceCode" id="cb10"><pre
class="sourceCode bash"><code class="sourceCode bash"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="op">></span> DEBUG=llm.js<span class="pp">*</span>,imagine.js<span class="pp">*</span>,embeddings.js<span class="pp">*</span>,vectordb.js<span class="pp">*</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="op">></span> node <span class="ex">src/run_ai.js</span></span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="co"># debug all logs</span></span></code></pre></div>
<h2 id="projects">Projects</h2>
<p><code>AI.js</code> is currently used in the following projects:</p>
<ul>
<li><a href="https://infinityarcade.com">Infinity Arcade</a> — Play any
text adventure game</li>
</ul>
<h2 id="license">License</h2>
<p>MIT</p>
<h2 id="author">Author</h2>
<p>Created by <a href="https://twitter.com/themaximal1st">The
Maximalist</a>, see our <a
href="https://themaximalist.com/products">open-source projects</a>.</p>
<script src="/app.js"></script>
</div>
</body>
</html>