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

cosinejs

v1.0.4

Published

This module provides a simple and efficient way to calculate the cosine similarity between a given vector and an array of vectors. It's particularly useful for tasks like recommendation systems or other machine learning applications where you need to dete

Downloads

2

Readme

Cosine Similarity Calculator

This module provides a simple and efficient way to calculate the cosine similarity between a given vector and an array of vectors. It's particularly useful for tasks like recommendation systems or other machine learning applications where you need to determine similarity between sets of items.

Installation

Install the package using npm:

npm install cosinejs

Usage

Here's an example of how you can use the cosine function from this module:

import { cosine } from 'cosinejs';

const vecA = [1, 2, 3];
const vecsB = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

const results = cosine(vecA, vecsB, 2);

console.log(results);

Release Notes

Version 1.0.4

  • Added vitest unit tests

Version 1.0.3

  • Added git repository to package.json

Version 1.0.2

Optimizations:

  • Improved the efficiency of the cosine function by computing the magnitude of the primary vector (vecA) just once, resulting in a significant performance boost especially for high-dimensional vectors.

API

cosine(vecA: number[], vecsB: number[][], n: number): { vector: number[], similarity: number }[]

Calculates the cosine similarity between vecA and each vector in vecsB, and returns the top n results.

  • vecA: A vector represented as an array of numbers.
  • vecsB: An array of vectors, each represented as an array of numbers.
  • n: The number of top results to return.

Note: Vectors must have the same dimensions, or an error will be thrown.

Storing Vectors with Cloudflare KV and Durable Objects

This module can also be used in conjunction with Cloudflare's KV and Durable Objects for efficient storage and retrieval of vectors. Here's how you can do that:

Using Cloudflare Durable Objects

Durable Objects provide a way to manage state within the Cloudflare Workers runtime. This can be useful for more complex scenarios where vectors are part of an object's state.

Here's an example of how you might use Durable Objects:

array-storage.ts

export class ArrayStorageDO {
    state: DurableObjectState;

    constructor(state: DurableObjectState, env: Env) {
        this.state = state;
    }

    async fetch(request: Request) {
        let url = new URL(request.url);

        switch (url.pathname) {
            case "/store":
                const array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
                await this.state.storage?.put("array", array);
                return new Response("Array stored successfully");
            case "/get":
                const storedArray: number[][] = await this.state.storage?.get("array") || [];
                return new Response(JSON.stringify(storedArray), {
                    headers: { "Content-Type": "application/json" }
                });
            default:
                return new Response("Not found", { status: 404 });
        }
    }

    async getStoredArray(): Promise<number[][]> {
        return await this.state.storage?.get("array") || [];
    }
}

interface Env {
    ARRAY_STORAGE: DurableObjectNamespace;
}

index.ts

import { ArrayStorageDO } from './array-storage';
import { cosine } from 'cosinejs';

export { ArrayStorageDO };

export default {
    async fetch(request: Request, env: Env) {
        try {
            return await handleRequest(request, env);
        } catch (e) {
            return new Response(`${e}`);
        }
    },
};

async function handleRequest(request: Request, env: Env) {
    let url = new URL(request.url);
    let name = url.searchParams.get("name");

    if (!name) {
        return new Response(
            "Select a Durable Object to contact by using the `name` URL query string parameter. e.g. ?name=ArrayStorage"
        );
    }

    let id = env.ARRAY_STORAGE.idFromName(name);
    let obj = env.ARRAY_STORAGE.get(id);

    if (url.pathname === "/cosine-search") {
        if (request.method !== "POST") {
            return new Response("Expected POST request", { status: 400 });
        }

        const inputData = await request.json();
        const userVector = inputData.vector;

        if (!userVector || !Array.isArray(userVector)) {
            return new Response("Invalid input vector", { status: 400 });
        }

        const storedVectors: number[][] = await obj.getStoredArray();
        const results = cosine(userVector, storedVectors, 2);

        return new Response(JSON.stringify(results), {
            headers: { "Content-Type": "application/json" }
        });
    }

    return obj.fetch(request);
}

Contributing

If you find any bugs or have suggestions for improvements, please submit an issue or pull request.

License

MIT