A lightweight, easy-to-use wrapper for the Google Gemini API, designed to get you started with conversational AI quickly.
- 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.
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.
-
Authenticate with GitHub Packages: Create a Personal Access Token (PAT) with the
read:packagesscope. You can create one here. -
Configure npm: Create or edit a
.npmrcfile in the root of your project and add the following line:@Rocket-PSStore:registry=https://npm.pkg.github.com -
Install the package:
npm install @Rocket-PSStore/gemini-easy
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();Initializes the Gemini client with your API key. This must be called before any other function.
key(string): Your Google Gemini API key.
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.
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.").
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.
Clears the current conversation history and starts a new chat session.
This project is licensed under the GNU Lesser General Public License v3.0. See the LICENSE file for more details.