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

ask-gemini

v6.0.0

Published

Lightweight package to easily interact with Google's Generative AI (Gemini).

Downloads

45

Readme

NPM NodeJS JavaScript

ask-gemini

A simple npm package to easily interact with Google's Generative AI (Gemini) in a Node.js project.

Table of Contents

Installation

To install the package, use npm:

npm install ask-gemini

Usage

First, you need to import the package and set your API key. Then you can use the askGemini function to get responses from the AI. Visit Google AI Studio to get your API key.

import { setGeminiApiKey, askGemini } from 'ask-gemini';

// Set your API key
setGeminiApiKey('YOUR_GOOGLE_API_KEY');

// Ask Gemini AI something
askGemini('Generate a paragraph about the history of the United States.')
  .then(response => {
    console.log(response);
  })
  .catch(err => {
    console.error(err);
  });

Using Images

Below is an example if you wanted to add images/video/audio to your prompt:

import { setGeminiApiKey, askGemini } from 'ask-gemini';

setGeminiApiKey('YOUR_GOOGLE_API_KEY');

// Ask Gemini AI with prompt and video/audio
askGemini("Can you summarize what is in this audio and what is in this picture?", [
  { path: 'video.mp3', type: 'audio/mp3' },
  { path: 'image.jpg', type: 'image/jpg' },
])
  .then(response => console.log(response))
  .catch(error => console.error(error));

Using Multi-Turn conversations

The askGeminiWithHistory function prompts the Gemini AI model with a message and a history of previous interactions, enabling multi-turn conversations.

import { setGeminiApiKey, askGeminiWithHistory } from 'ask-gemini';

// Set the API key
setGeminiApiKey('YOUR_GOOGLE_API_KEY');

// Define the conversation history
const history = [
  {
    role: 'user',
    parts: [{ text: 'Hello, I have 2 dogs in my house.' }],
  },
  {
    role: 'model',
    parts: [{ text: 'Great to meet you. What would you like to know?' }],
  },
];

// Send a follow-up message and get the response
askGeminiWithHistory('How many paws are in my house?', history)
  .then(response => console.log(response))
  .catch(error => console.error(error));

Documentation

setGeminiApiKey(apiKey)

Sets the API key for the Google Generative AI service.

  • Parameters:
    • apiKey (string required): Your Google Generative AI API key.

setGeminiApiKeyFromEnv()

Sets the API key for the Google Generative AI service from process.env.GEMINI_API_KEY variable.


askGemini(prompt, imageArray)

Sends a prompt to the AI and returns the response.

  • Parameters:

    • prompt (string required): The text prompt to send to the AI.
    • imageArray (Array optional): An array of objects with image file path and type.
  • Returns:

    • A promise that resolves to the AI's response.

askGeminiWithHistory(prompt, historyArray)

Sends a prompt to the AI and returns the response.

  • Parameters:

    • prompt (string, required): This is the new message you want to send to the AI. It should be a string containing your question or statement.
    • history (Array, optional): An array of previous messages in the conversation. Each entry in the array should be an object with a role and parts array. The role indicates whether the message was sent by the "user" or the "model", and parts is an array where each element has a text property containing the message text.
  • Returns:

    • A promise that resolves to the AI's response.

setGeminiModel(modelStringParam)

This function allows the user to set the default model type for Gemini. By calling this function and passing a modelStringParam, the user can specify which model type should be set as the default for Gemini.

  • Parameters:
    • modelStringParam (string, required): A string parameter representing the model type that the user wants to set as the default for Gemini.

License

This package is licensed under the ISC License. See the LICENSE file for details.