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

@polymath-ai/client

v2.0.3

Published

A JavaScript client library to run Polymath

Downloads

18

Readme

@polymath-ai/client

polymath is a utility that uses AI to intelligently answer free-form questions based on a particular library of content.

This is an ESM JavaScript client that you can use to ask a Polymath or series of them a couple types of questions.

Get me embedding results for a given query

This is the heart of what a Polymath does. It has a library of content, and given a query, it will return a subset of the library bits that represent good context for that given query.

let polymathResults = await client.ask("How long is a piece of string?");

polymathResults is an object that contains:

  • bits: a function that returns an array of all of the bits that matched. Each bit contains:

    • text: string;
    • embedding: Vector;
    • similarity?: number;
    • token_count: number;
  • context(tokenLength): A function that returns a helpful string of all of the text to easily be used in a completion request. Can limit the context by passing in the tokenLength limit.

  • maxBits(tokenLength): A function that returns the bits that fit into a given token length

Get me a completion from a given query

This isn't the main job for a Polymath, but often you want to ask for a completion, we want to make it simple to do so in one fell swoop.

let completionResult = await client.completion(
  "How long is a piece of string?"
);

completionResult is an object that contains:

  • bits: an array of all of the bits that matched. Same as above from polymathResults

  • infos: An array with all of the info entries (without duplicates). They contain:

    • url: pointing to the source
    • title?: content title
    • description?: content description
  • completion: text of the completion (answer) from the model

Setting up the Polymath

When you setup a polymath object you have to pass in some options.

You have to pass in an Open AI API key, and then you need to tell it where you want to ask for information.

This can be locally via library files. Place an array of files and/or file patterns that point to individual files, file globs (e.g. ./libraries/*.json), or directories (.json files will be loaded).

For example, with a specific file:

let client = new Polymath({
  apiKey: process.env.OPENAI_API_KEY,
  libraryFiles: ["./libraries/knowledge-string.json"],
  promptTemplate:
    "Answer using the context below please.\n\nContext: {context}\n\nQuestion: {prompt}\n\nAnswer:",
});

And/or you can point to a remote server URL for a Polymath:

let client = new Polymath({
  apiKey: process.env.OPENAI_API_KEY,
  servers: ["https://polymath.almaer.com/"],
});

And why not ask multiple polymath servers?

let client = new Polymath({
  apiKey: process.env.OPENAI_API_KEY,
  servers: ["https://remix.polymath.chat/", "https://preact.polymath.chat/"],
});

let results = await polymath.completion("Can you use Remix with Preact?");

If you have stored your library in Pinecone then you can tell Polymath to look there.

You will want to setup your environment variables, either directly or via a .env file.

let client = new Polymath({
  apiKey: process.env.OPENAI_API_KEY,
  pinecone: {
    apiKey: process.env.PINECONE_API_KEY,
    baseUrl: process.env.PINECONE_BASE_URL,
    namespace: process.env.PINECONE_NAMESPACE,
  },
});

let r = await client.ask("How long is a piece of string?");

You can also pass in other optional info such as overriding the prompt that you wish to use:

let polymath = new Polymath({
  apiKey: process.env.OPENAI_API_KEY,
  libraryFiles: ["./libraries/knowledge-string.json"],
  promptTemplate:
    "Answer using the context below please.\n\nContext: {context}\n\nQuestion: {prompt}\n\nAnswer:",
});

And if you want some debugging info, just pass in debug: true:

let polymath = new Polymath({
  apiKey: process.env.OPENAI_API_KEY,
  libraryFiles: ["./libraries/knowledge-string.json"],
  debug: true,
});

You may want to process a completion as it streams to you. To do so you need to setup a streamProcessor:

let client = new Polymath({
  apiKey: process.env.OPENAI_API_KEY,
  libraryFiles: ["./libraries/knowledge-string.json"],
});

let completionOptions = {
  model: "text-davinci-003",
  stream: true,
};

let streamProcessor = {
  processResults: (r) => {
    resolve();
    t.pass();
  },
  processDelta: (delta) => {
    // console.log("STREAMING DELTA:", delta);
  },
};

client.completion(
  "How long is a piece of string?",
  undefined, // PolymathResults
  undefined, // askOptions
  completionOptions,
  streamProcessor
);

Tests

Tests are located in tests/. We use ava for a simple test library.

To run them you will want to set your OpenAI API Key environment variable first, and some of the tests will use the simple library libraries/knowledge-string.json that you can poke at too.