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

@instant.dev/vectors

v0.1.3

Published

Utility for batch creating vectors via API

Downloads

32

Readme

Simple vector creation with automatic batching

npm version Build Status

Batch create vectors without thinking about it

When you're creating a lot of vectors - for example, indexing a bunch of documents at once using OpenAI embeddings - you quickly run into IO-related performance issues. Your web requests will be throttled if you make too many parallel API requests, so OpenAI allows for batched requests via the OpenAI embeddings API. However, this API only allows for a maximum of 8,191 tokens per request: about 32,764 characters.

Solution: @instant.dev/vectors provides a simple VectorManager utility that performs automatic, efficient batch creation of vectors. It will automatically collect vector creation requests over a 100ms (configurable) timeframe and batch them to minimize web requests.

It is most useful in web server contexts where multiple user requests may be creating vectors at the same time. If you rely on the same VectorManager instance all of these disparate requests will be efficiently batched.

Installation and Importing

To use this library you'll need to also work with a vector creation tool, like OpenAI.

npm i @instant.dev/vectors --save # vector management
npm i openai --save # openai for the engine

CommonJS:

const { VectorManager } = require('@instant.dev/vectors');
const OpenAI = require('openai');

const openai = new OpenAI({apiKey: process.env.OPENAI_API_KEY});
const Vectors = new VectorManager();

ESM:

import { VectorManager } from '@instant.dev/vectors';
import { Configuration, OpenAIApi } from "openai";
const configuration = new Configuration({
    organization: "YOUR_ORG_ID",
    apiKey: process.env.OPENAI_API_KEY,
});

const openai = new OpenAIApi(configuration);
const Vectors = new VectorManager();

Usage

Once you've imported and instantiated the package, it's easy to use.

Set a batch engine

// values will automatically be batched appropriately
Vectors.setEngine(async (values) => {
  const embeddingResult = await openai.embeddings.create({
    model: 'text-embedding-ada-002',
    input: values,
  });
  return embeddingResult.data.map(entry => entry.embedding);
});

Create a vector

let vector = await Vectors.create(`Something to vectorize!`);

Create multiple vectors

Manually manage vector creation:

const myStrings = [
  `Some string!`,
  `Can also be a lot longer`,
  `W`.repeat(1000),
  // ...
];

let vectors = await Promise.all(myStrings.map(str => Vectors.create(str)));

Or create multiple vectors easily with the batchCreate utility:

const myStrings = [
  `Some string!`,
  `Can also be a lot longer`,
  `W`.repeat(1000),
  // ...
];

let vectors = await Vectors.batchCreate(myStrings);

Configuration

You can configure the following parameters:

const Vectors = new VectorManager();

// these are the defaults
Vectors.maximumBatchSize = 7168 * 4; // maximum size of a batch - for OpenAI, 4 tokens per word, estimated
Vectors.maximumParallelRequests = 10; // 10 web requests simultaneously max
Vectors.fastQueueTime = 10; // time to wait if no other entries are added
Vectors.waitQueueTime = 100; // time to wait to collect entries if 1+ entries are added

Acknowledgements

Special thank you to Scott Gamble who helps run all of the front-of-house work for instant.dev 💜!

| Destination | Link | | ----------- | ---- | | Home | instant.dev | | GitHub | github.com/instant-dev | | Discord | discord.gg/puVYgA7ZMh | | X / instant.dev | x.com/instantdevs | | X / Keith Horwood | x.com/keithwhor | | X / Scott Gamble | x.com/threesided |