Intergrate with Gemini in nodejs.

Proxy

@google/generative-ai is not support proxy now, but i found a solution in this PR

so we can use this gist

Quick Start

  • npm i node-fetch https-proxy-agent @google/generative-ai
  • export http_proxy=http://127.0.0.1:1087 https_proxy=http://127.0.0.1:1087 to set your proxy
  • touch index.mjs

    The code is fellowing:

import { GoogleGenerativeAI } from '@google/generative-ai'
// const { GoogleGenerativeAI } = require("@google/generative-ai");

// Access your API key as an environment variable (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI(process.env.API_KEY)

// ...

// For text-only input, use the gemini-pro model
const model = genAI.getGenerativeModel({ model: 'gemini-pro' })

// ...

async function run () {
  const prompt = 'hi'

  const result = await model.generateContent(prompt)
  const response = await result.response
  const text = response.text()
  console.log(text)
}

run()

Run

  • API_KEY=xxx node index.mjs