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

@drobs/ai-api

v1.6.1

Published

Generic API to multiple Generative AI models

Downloads

77

Readme

AI API

This is a generic API for calling different Generative AI models (OpenAI, VertexAI, Google, ...). The API could be invoked with some text, images or video prompt to get text or strem. It implements a Node.js module to be imported in your project

Available models

Google

Vertex AI - Recommended

  • gemini-1.5-flash

Procédure

Get the following values from the Google credential JSON file by the procedure Get the complete procedure from here

Generative API

Simpler but only for test or non industrial projects

  • gemini-1.5-flash

Procédure

Get your API key following this procedure

Open AI

  • gpt-3.5
  • gpt-3.5-turbo
  • gpt-4o-mini

Installation

Add in your project

npm install @drobs/ai-api

Add the environement variables

Google API (Vertex API)

  • GOOGLE_API_PROJECT_ID=Your Project ID (ex : the-experience-xxxxx)
  • GOOGLE_API_PRIVATE_KEY_ID=Your Private Key ID (ex : 87adbdad1eb9b1540edf867bd8d79926eb2ad5c8)
  • GOOGLE_API_PRIVATE_KEY=Your Private Key (ex : -----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkq...oq4SQJbV\n-----END PRIVATE KEY-----\n)
  • GOOGLE_API_CLIENT_EMAIL=Your Google Account allowed to use VertexAI API (ex : [email protected])
  • GOOGLE_API_CLIENT_ID=Your Google Client ID (ex : 107696212857897829246)
  • GOOGLE_API_REGION=Your Google Region (ex : europe-west9)
  • GOOGLE_API_CLIENT_CERT_URL=https://www.googleapis.com/...

Google API (Generative API)

  • GOOGLE_API_PRIVATE_KEY=Your Private Key

Open API

  • OPENAI_API_KEY=Your Open API Key (ex : sk-tfB0mBpLPyQFeUzR7R48issEmHrjNbzCYsT3BlbkFJev3ZGj)

Usage

Procedure

Import functions from @drobs/ai-api module

import { generateContent, getModels } from './index.js';

Choose your model from getModels

(Optional) Choose options for your request

const options = {
   temperature: 0, //from 0 (very deterministic) to 2 (very creative)
   max_tokens: 100, //number of tken for the expected response
   topK: 64, //probability of tokens to consider for each candidate (1=most probable token) (only for Google models)
   topP: 1, //cumulative probability of tokens to consider for each candidate
   candidateCount: 1 //number of candidate responses that will be return by the model
   moderationModel:{} //soon available
}

Create your prompt request

const prompt = 'Tell me a story';

Invoke generateContent with the chosen model

const response = generateContent (prompt, model, false, options);
console.log (response);

Example

import { generateContent } from '@drobs/ai-api';

const prompt = "This is a test";
const model = 4;  //Gemini 1.5 Flash via Vertex AI
const stream = false;
const options = {
    temperature: 0,
    max_tokens: 100,
    topP: 1,
    candidateCount: 1
}

function main () {
    generateContent (prompt, model, stream, options)
    .then (result => {
        console.log(result);
    })
    .catch (error => {
        console.error(error);
    }); 
}

main();

Contribute

GitHub Repository

Please feel free to contribute or report any issue on GitHub

Compile

npm run build

Test

node run test