Node Documentation for Google Gemini

Below is the documentation for integrating Reconify with Google Gemini via Node NPM module.

Currently we support Generate with text, Generate with text and images, and Chat actions on Google Gemini.

Get started - Create an account

The first step is to create an account at app.reconify.com.

Generate API and APP Keys

In the Reconify console, add an Application to your account. This will generate both an API_KEY and an APP_KEY which will be used in the code below to send data to Reconify.

Node Integration

The easiest way to integrate in Node.js is with the NPM module.

Install the NPM module

npm install reconify --save

Import the module

import {reconifyGeminiHandler} from 'reconify';

Initialize the module

Prior to initializing the Reconify module, make sure to import and initialize the Gemini module.

import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);

Initialize the module passing in the Google Gemini object and the keys created above.

const reconify = reconifyGeminiHandler(genAI, {
appKey: process.env.RECONIFY_APP_KEY,
apiKey: process.env.RECONIFY_API_KEY,
})

This is all that is needed, and the NPM takes care of the rest.

Optional initialization parameters

You can optionally turn on "debug" mode by passing in "debug: true" in the JSON above. This will print debug messages to the console.

const reconify = reconifyGeminiHandler(genAI, {
appKey: process.env.RECONIFY_APP_KEY,
apiKey: process.env.RECONIFY_API_KEY,
debug: true,
})

Optional methods

You can optionally pass in a user object or session ID to be used in the analytics reporting. The session ID will be used to group interactions together in the same session transcript.

The user object should include a unique userId, the other fields are optional.

reconify.setUser ({
  "userId": "123",
  "isAuthenticated": 1,
  "firstName": "Francis",
  "lastName": "Smith",
  "email": "",
  "phone": "",
  "gender": "female"
});

The session ID is a simple string.

reconify.setSession('MySessionId');

Example: Generate with Text

import { GoogleGenerativeAI } from '@google/generative-ai';
import { reconifyGeminiHandler } from 'reconify';

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);

const reconify = reconifyCohereHandler(cohere, {
  appKey: process.env.RECONIFY_APP_KEY,
  apiKey: process.env.RECONIFY_API_KEY,
});

reconify.setUser({
  userId: "12345",
  firstName: "Jim",
  lastName: "Smith",
});

const model = genAI.getGenerativeModel({ model: "gemini-pro" });
const result = await model.generateContent("Tell me a cat joke");
const response = await result.response;
const text = response.text();

Example: Generate with Text and Images

import { GoogleGenerativeAI } from '@google/generative-ai';
import { reconifyGeminiHandler } from 'reconify';
import * as fs from 'fs';

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);

const reconify = reconifyCohereHandler(genAI, {
  appKey: process.env.RECONIFY_APP_KEY,
  apiKey: process.env.RECONIFY_API_KEY,
});

reconify.setUser({
  userId: "12345",
  firstName: "Jim",
  lastName: "Smith",
});

const fileToGenerativePart = (path, mimeType) => {
  return {
     inlineData: {
        data: Buffer.from(fs.readFileSync(path)).toString("base64"),
        mimeType
     },
  };
}
const model = genAI.getGenerativeModel({ model: "gemini-pro-vision" });
const imageParts = [fileToGenerativePart("holiday-cat.jpg", "image/jpeg")];
const result = await model.generateContent(["Describe this photo", ...imageParts]);
const response = await result.response;
const text = response.text();

Example: Chat

import { GoogleGenerativeAI } from '@google/generative-ai';
import { reconifyGeminiHandler } from 'reconify';

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);

const reconify = reconifyCohereHandler(cohere, {
  appKey: process.env.RECONIFY_APP_KEY,
  apiKey: process.env.RECONIFY_API_KEY,
});

reconify.setUser({
  userId: "12345",
  firstName: "Jim",
  lastName: "Smith",
});

const model = genAI.getGenerativeModel({ model: "gemini-pro" });

const chat = model.startChat({
  history: [
        { role: "user", parts: "I have a tuxedo cat and a calico cat"},
        { role: "model", parts: "How can I help?"}
  ],
  generationConfig: {
        maxOutputTokens: 200
  }
})
const result = await chat.sendMessage("How many colors are my cats?");
const response = await result.response;
const text = response.text();