Introduction
The world has seen a lot of advancements in the AI world over the last several years. With artificial intelligence becoming more commonplace, developers must find ways to incorporate AI into their applications. Gemini offers an easy way for javasScript developers to build with AI through node's GoogleGenerativeAI package. Developers can access Gemini models, which are developed by Google's DeepMind, in order to create exciting features using AI. Other packages are available for those using Python or GO, and Gemini also has a RESTful API. This article will talk about the improvements Gemini's newest models bring to the table, and how to get started with the GoogleGenerativeAI package using node.
Major Developments
A major development the Gemini 1.5 Flash model brings is the amount of context tokens that can be handled in a single request. In the past, these types of models were limited by the amount of text, or tokens, that could be processed at a time. The generative models that have been created over the last few years only had the ability to process 8,000 tokens at once. While this number had improved with the advancements in AI technology, it still remained a limiting factor. Today, Gemini 1.5 Flash is capable of processing up to 1 million tokens at a time. The pro version (Gemini 1.5 Pro) can process up to 2 million. This makes Gemini capable of processing large amounts of information at once, while still maintaining a very high accuracy rate. You can read more about Gemini's advancements in AI world and what they mean here.
Getting Started
The first thing you will need to do in order to use the GoogleGenerativeAI package is crate a Gemini API key.
This is a quick and painless process.
- Go to Google AI Studio
- Click the 'Get API Key' button at the top left
- Click the 'Create API Key' button
Once you've accessed an API key you'll need to install the package with node.
npm install @google/generative-ai
Once you've done all this you're ready to start developing with AI!
Setup
Import the package into the file you wish to use it.
import { GoogleGenerativeAI } from '@google/generative-ai'
// OR
const { GoogleGenerativeAI } = require('@google/generative-ai');
Create an instance of GoogleGenerativeAI while passing in your API key.
const genAI = new GoogleGenerativeAI('YOUR_API_KEY');
Use the getGenerativeAI method and pass in and object for the model you want to use. There are a number of models available, this example uses the Gemini 1.5 Flash model. Gemini Models
const model = genAI.getGenerativeModel({ model: 'gemini-1.5-flash' });
Once the model is setup, you can use the AI to generate text, respond to images, extract information from videos, and more.
Configuration and System Instructions
You can optionally provide the model with a configuration and system instructions. The configuration is applied on a generationConfig property within the generateContent method invocation. Some configuration options include:
- responseSchema: Output schema of the generated text
- candidateCount: (integer) Number of responses to return
- temperature: (number) Controls the randomness of the output
See more generationConfig properties here.
Providing system instructions can help improve the response by providing additional context to the AI. In addition, the model will generate more custom responses, and be able to better suit the user's needs. The system instructions are provided when the model is initialized.
const model = genAI.getGenerativeModel({
model: 'gemini-1.5-flash',
systemInstruction: 'You are extremely friendly, always use your manners.',
});
Text Generation
Text can be generated in a number of ways using the package. The simplest way is to only provide the model with text, but there are more exciting and complex ways text can be generated. You can provide the model with an image along with the text in order to have the AI react to an image. Here is a simple example of a request that generates a response using only text.
The model setup is not included in this code block, but it is still part of the code.
const prompt = 'Tell me something about the moon';
const result = await model.generateContent(prompt);
let text = result.response.text();
// Log the response
console.log(text);
A prompt string is passed into the model's generateContent method. Once the response is returned, the response can be accessed on the response properties text method. The result of this response was: 'The Moon's surface is covered in a layer of fine dust called regolith, formed by billions of years of micrometeorite impacts. This dust is incredibly fine and clings to everything, posing challenges for astronauts and lunar equipment.' Pretty cool huh? This is a super simple example, but there are more possibilities.
Text Streaming and Chatting
The model waits for the entire response text to be generated before returning the response. Obviously right? If you didn't want to wait on the entire response to be generated, you can use text streaming to receive a faster response by not waiting on the entire result. This can be achieved using the streamGenerateContent method.
Here is an example from the Gemini API docs.
import { GoogleGenerativeAI } from '@google/generative-ai';
const genAI = new GoogleGenerativeAI('GEMINI_API_KEY');
const model = genAI.getGenerativeModel({ model: 'gemini-1.5-flash' });
const prompt = 'Explain how AI works';
const result = await model.generateContentStream(prompt);
for await (const chunk of result.stream) {
const chunkText = chunk.text();
process.stdout.write(chunkText);
}
The package also offers the capability for keeping track of conversation. This helps users solve multistep problems by 'allowing users to step incrementally toward answers.' This is a relatively advanced feature of the Gemini API. More can be read about creating a chat and other text generation capabilities on the Gemini API Docs.
Conclusion
The GoogleGenerativeAi package makes it easy for javaScript developers to incorporate AI technologies into their applications. The package has numerous capabilities in AI generation, including text, video, and images. Gemini's ability to process a large amount of text at a time is a huge development in AI generation. With node's GoogleGenerativeAI, developers are able to include advanced AI technologies in their projects in a much easier way.
Top comments (1)
Gemini is making AI more accessible for JavaScript developers with the GoogleGenerativeAI package. The latest Gemini 1.5 Flash model supports processing up to 1 million tokens, offering huge improvements in AI capabilities. Developers can easily generate text, analyze images, and stream content with this package. To get started, simply create an API key, install the package, and integrate it into your project. Explore more about the setup and its features in the https://[thefrlegendsapk.com]