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

anything-gpt

v0.1.13

Published

Creates GPT instance that just works everywhere JavaScript works

Downloads

10

Readme

anything-gpt

npm version npm size

Creates GPT instance that just works everywhere JavaScript works 🤖

No dependencies. <2KB. Try now.


Introduction

"anything-gpt" is a minimalist, highly portable JavaScript package designed to integrate OpenAI's GPT models into any JavaScript environment.

From running in a browser's console to executing in AWS Lambda functions, "anything-gpt" stands out for its zero-dependency architecture and lightweight footprint, making it an ideal choice for a wide range of applications.

For more information check out the documentation.

Features

  • Universal JavaScript Compatibility: Works seamlessly in any JS environment (browsers, Node.js, AWS Lambda, Cloudflare Workers, etc.).
  • Zero Dependencies: No external dependencies, ensuring easy integration and minimal conflicts.
  • Ultra-Lightweight: Less than 2KB in size, making it incredibly efficient and fast to load.
  • Simple Interface: Offers a straightforward way to send network requests to OpenAI and handle text or stream responses.
  • Stream & Text Response Handling: Supports both streaming and traditional text responses, giving you flexibility in how you receive data.
  • Error Handling: Built-in error handling for robust and reliable performance.

Installation

npm install anything-gpt

or

yarn add anything-gpt

Usage

1. Use pre-build GPT instances

The easiest and quickest way to just get the GPT instance and start working with it.

import instance from "anything-gpt/dist/use/gpt4" // gpt3.5 (free model) is also available

const gpt = instance("YOUR_OPENAI_API_KEY")

await gpt`Enter your prompt. You can pass ${anything} here.`        // 1. Enter the prompt
  .then((stream) => stream((chunk: string) => console.log(chunk)))  // 2. Get GPT response by chunks
  .then((messages: ChatMessage[]) => console.log(messages))         // 3. Get all messages as a result
  .catch((error: Error) => console.error(error))                    // 4. Handle error

2. Use core package functions

Need more flexibility? Then this way is yours.

import {core, chunkify, useOptions, useEnvironment, ChatMessage, CreateInstanceOptions, AbstractEnvironmentContext} from "anything-gpt"

// Let's create GPT instance that would work for client-side application.
// Our goal now is to just log the streamed response from ChatGPT's Completions Chat API
// right to the browser's console. 

// Step 1.
// Set OpenAI API Key and Chat Completions API options
const options: CreateInstanceOptions = useOptions({
  gpt: {
    // these fields are set by default, override them if needed.
    // model: "gpt-3.5-turbo",
    // temperature: 0.7,
    // max_tokens: 4000,
    // stream: true,
    model: "gpt-4" 
  }, 
  instance: {
    // you define where the key comes from, depending on JS environment you work with.
    api_key: localStorage.getItem("your_api_key_here")
  }, 
})

// Step 2.
// Set up an environment for GPT:
//  - What is the role of this bot.
//  - How to handle text / stream response.
//  - How to handle errors.
const browser: Partial<AbstractEnvironmentContext> = useEnvironment({
  state: [{
    role: "system",
    content: "What is this bot for? You can add custom instructions here."
  }],
  stream: async (response: Response) => // handle streamed response as is,
    chunkify(response, (chunk: string) => console.log(chunk)), // or use built-in helper for getting message by chunk
  error: async (error: Error) => console.log(error)
})

// Last step is to create "gpt" function.
const gpt = core.bind(
  browser, // create context for anything, say, for "game-engine", "cli-terminal", "cloudfalre-worker", etc.
  options,
)

declare const anything: string | number | object | any[] | Function | Error // and so on

// That's it! Use tagged "gpt" function as you want!
const conversation: ChatMessage[] = await gpt`enter you prompt. you can also pass ${anything} here`