Skip to content

Rocket-PSStore/gemini-easy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

# Gemini Easy

license version

A lightweight, easy-to-use wrapper for the Google Gemini API, designed to get you started with conversational AI quickly.

Features

  • Simple setup with a single API key function.
  • Persistent conversation history within a session.
  • Ability to set a custom persona with a system message.
  • Lightweight and minimal dependencies.

Installation

This package is hosted on GitHub Packages. To install it, you first need to configure npm to use the GitHub registry for the @Rocket-PSStore scope.

  1. Authenticate with GitHub Packages: Create a Personal Access Token (PAT) with the read:packages scope. You can create one here.

  2. Configure npm: Create or edit a .npmrc file in the root of your project and add the following line:

    @Rocket-PSStore:registry=https://npm.pkg.github.com
    
  3. Install the package:

    npm install @Rocket-PSStore/gemini-easy

Usage

Here is a basic example of how to use the package. It's recommended to use environment variables for your API key.

// Import the package
const gemini = require('@Rocket-PSStore/gemini-easy');
require('dotenv').config(); // Use dotenv to load environment variables

async function main() {
  try {
    // 1. Get your API key from Google AI Studio and set it as an environment variable
    const apiKey = process.env.GEMINI_API_KEY;
    if (!apiKey) {
      throw new Error("GEMINI_API_KEY environment variable not set!");
    }
    gemini.setApiKey(apiKey);

    // 2. (Optional) Set a persona for the AI
    gemini.setSystemMessage("You are a friendly pirate who loves talking about treasure.");

    // 3. Start the conversation
    console.log("User: Hello! How are you?");
    let response = await gemini.talk("Hello! How are you?");
    console.log("Gemini:", response);

    console.log("\nUser: What's the best kind of treasure?");
    response = await gemini.talk("What's the best kind of treasure?");
    console.log("Gemini:", response);

    // 4. (Optional) View the conversation history
    console.log("\n--- Conversation History ---");
    const history = await gemini.getHistory();
    console.log(history);

  } catch (error) {
    console.error("An error occurred:", error.message);
  }
}

main();

API Reference

setApiKey(key)

Initializes the Gemini client with your API key. This must be called before any other function.

  • key (string): Your Google Gemini API key.

talk(prompt)

Sends a message to the Gemini model and returns its response.

  • prompt (string): The message to send to the model.
  • Returns: Promise<string> - The model's text response.

setSystemMessage(message)

Sets a system instruction to define the model's persona or behavior for the entire session.

  • message (string): The instruction for the model (e.g., "You are a helpful assistant.").

getHistory()

Retrieves the full conversation history for the current session.

  • Returns: Promise<Array<object>> - An array of message objects from both the user and the model.

clearHistory()

Clears the current conversation history and starts a new chat session.

Licensing

This project is licensed under the GNU Lesser General Public License v3.0. See the LICENSE file for more details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published