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

monads.ai

v2.0.51

Published

Functional programming. A bridge to Openai.

Downloads

5

Readme

The way we've structured our Monad class gives you a flexible setup where you can choose to interact directly with the OpenAI API through the openai client object, or you can intercept and pre-handle requests by defining custom methods within the Monad class itself.

Direct Access vs. Managed Access

  1. Direct Access:

    • When using monad.openai, you are directly accessing the OpenAI client without any intermediary logic. This means you cannot manipulate or pre-process the request before it is sent or handle the response before it is returned to the caller.
    • This is straightforward and ensures that you're using the OpenAI client as intended without modifications, but it also means you miss out on opportunities to add custom logic such as logging, error handling, transformations, or validations specific to your application's needs.
  2. Managed Access:

    • By defining custom methods in the Monad class, you can add a layer of abstraction where you manage or manipulate the data before sending it to OpenAI or after receiving a response from OpenAI.
    • This allows you to integrate business logic directly related to how your application needs to interact with OpenAI, providing a centralized point to add functionalities like input validation, error handling, logging, or even caching responses if necessary.

Example of Managed Access

Here’s how you can add a managed method to pre-handle requests or post-process responses:

import OpenAI from "openai";

class Monad {
    constructor(apiKey) {
        this.openai = new OpenAI({
            apiKey: apiKey
        });
        console.log("Hello monads!");
    }

    // Custom method to handle requests with additional logic
    async query(promptText, model = 'text-davinci-003', max_tokens = 150) {
        console.log("Preparing to query OpenAI with:", promptText);  // Pre-handle logging

        try {
            const completion = await this.openai.createCompletion({
                model: model,
                prompt: promptText,
                max_tokens: max_tokens
            });
            console.log("Received response from OpenAI");  // Post-process logging
            return completion.choices[0].text;
        } catch (error) {
            console.error("Error during OpenAI query:", error);
            throw error;  // Enhanced error handling
        }
    }
}

export default Monad;

Using Managed Access in Your Application

You can now use this managed method, which provides additional logging and error handling, in your application:

import Monad from './monads.openai';

const monad = new Monad(process.env.OPENAI_API_KEY);

async function runQuery() {
    try {
        const response = await monad.query("How does the stock market work?");
        console.log("Query response:", response);
    } catch (error) {
        console.error("Query failed:", error);
    }
}

runQuery();

Or you can straight use the openai through the monad.openai:

async function listAssistants() { const myAssistants = await monad.openai.beta.assistants.list({ order: "desc", limit: "20", });

console.log(myAssistants.data); }

listAssistants();

Conclusion

By creating a layer of abstraction within your Monad class, you gain the flexibility to add custom functionality and control over the requests and responses involving OpenAI's API. This setup not only allows for direct API interactions when needed but also provides the capability to inject application-specific logic as required, enhancing the integration of OpenAI services within your application's architecture.