@jnode/gemini
v1.0.16
Published
Simple Gemini API package for Node.js.
Downloads
39
Maintainers
Readme
JustGemini
Simple Gemini API package for Node.js.
npm install @jnode/geminiBasic Usage
Import JustGemini
const gemini = require('@jnode/gemini');Create a Client
const client = new gemini.Client('YOUR_API_KEY');Generate Content with a Model
const model = client.model('gemini-pro');
async function generateText() {
const result = await model.generate('Hello');
console.log(result.text);
}
generateText();Class: GeminiClient
The main class for interacting with the Gemini API.
Constructor
new gemini.Client(key, options = {})key: Your Gemini API key.options: An optional object for setting various client options:apiBase: The base URL of the Gemini API. Default isgenerativelanguage.googleapis.com.apiThrowError: Whether to throw errors when the API status code is not 2xx. Default istrue.fileUnsupportError: Whether to throw errors when the file type is not supported. Default istrue.fileActiveCheckDelay: Delay in milliseconds between file status checks. Default is1500.fileMaxActiveCheck: Maximum number of file status checks. Default is15.
Methods
apiUrl(path = '/', query = ''): Returns the full API URL with the base, path, and API key.path: API endpoint path.query: Query string.- Returns:
string- The full API URL.
async apiRequest(method = 'GET', path = '/', query = '', body): Makes an HTTP request to the Gemini API.method: HTTP method (e.g.,GET,POST,PUT,DELETE). Default isGET.path: API endpoint path. Default is/.query: Query string.body: Request body data (will be stringified).- Returns:
Promise<RequestResponse>- A promise that resolves to aRequestResponseobject.
model(model, options): Returns aGeminiModelinstance for interacting with a specific model.model: The name of the model (e.g.,gemini-pro).options: Model options.- Returns:
GeminiModel- AGeminiModelinstance.
fileManager: An instance ofGeminiFileManagerfor managing files.
Class: GeminiModel
Represents a specific Gemini model.
Constructor
new gemini.Model(client, model, options = {})client: AGeminiClientinstance.model: The name of the model.options: An optional object for setting model options:tools: Custom tools to be used by the model.functions: An array ofGeminiFunctioninstances.functionMode: Function calling mode ('NONE', 'AUTO', 'ANY').allowedFunctions: Allowed function names for function calling.safetySettings: Safety settings for the model.systemInstruction: System instruction text.jsonMode: Enable JSON mode (boolean or object with response schema).jsonMode: Enable image generate (boolean).stopSequences: Stop sequences for generation.candidateCount: Number of candidate responses.maxOutputTokens: Maximum number of output tokens.temperature: Temperature for generation.topP: Top-p value for generation.topK: Top-k value for generation.generationConfig: Overwrite the config with original format.imageGenerate: Boolean. Use image generate.
Methods
optionsToApiFormat(options = {}): Converts simple options to API format.options: Model options.- Returns:
object- Options in API format.
async contentToApiFormat(content): Converts simple content to API format.content: Content string, array, or object.- Returns:
Promise<array>- Content in API format.
apiRequest(method, action, body): Makes an API request for this model.method: HTTP method.action: API action.body: Request body.- Returns:
Promise<RequestResponse>- A promise that resolves to aRequestResponseobject.
generate(content, optionsOverwrite = {}): Generates content using the model.content: Input content, which can be one of the following formats:- String: A single-turn text input.
- Example:
'What is the meaning of life?'
- Example:
- Array of strings: A multi-turn conversation where each string represents a turn. The turns alternate between user and model.
- Example:
['What is the capital of France?', 'Paris.', 'And what about Spain?']
- Example:
- Array of arrays/objects: A multi-turn conversation with more complex inputs, including text, booleans (to set the role), and file data.
- String: a text part.
- Boolean: (Optional, default
truewhich means is a "user" turn) set role to "user" (true) or "model" (false). - Object: can be one of the following:
{ text: string }: A text part.- Example:
{ text: 'What is the weather like today?' }
- Example:
{ filePath: string }: A local file to be uploaded and used as input. The file will be automatically uploaded if it hasn't been uploaded already.- Example:
{ filePath: './image.png' }
- Example:
{ fileUrl: string }: A web file URL to be used as input. The file will be automatically uploaded if it hasn't been uploaded already.- Example:
{ fileUrl: 'https://example.com/image.jpg' }
- Example:
{ file: { mimeType: string, data: Buffer } }: File data directly provided as input.- Example:
{ file: { mimeType: 'image/png', data: imageBuffer } }
- Example:
- Other objects will be treated as original API part format.
- Example:
[['What is in this image?', true, { filePath: './image.png' }], false, 'This is a picture of a cat.', ['And this one?', { filePath: './image2.jpg' }]]
- Array of objects: A multi-turn conversation in the original API content format. Each object should have
roleandpartsproperties.- Example:
[{ role: 'user', parts: [{ text: 'Hello' }] }, { role: 'model', parts: [{ text: 'Hi there!' }] }]
- Example:
- String: A single-turn text input.
optionsOverwrite: Optional options to overwrite model options.- Returns:
Promise<GeminiContents>- A promise that resolves to aGeminiContentsinstance.
Class: GeminiContents
Represents the contents generated by a model.
Constructor
new gemini.Contents(model, contents)model: AGeminiModelinstance.contents: Initial contents array.
Methods
async generate(content, optionsOverwrite): Generates content and updates theGeminiContentsinstance.content: Input content.optionsOverwrite: Optional options to overwrite model options.- Returns:
Promise<this>- The updatedGeminiContentsinstance.
async runFunctions(extraData): Executes function calls and continues generation.extraData: Extra data to be passed to functions.- Returns:
Promise<GeminiContents>- A promise that resolves to a newGeminiContentsinstance with the function responses.
Properties
model: TheGeminiModelinstance.contents: The full contents array.text: The generated text.functionCalls: An array of function calls.usage: Usage metadata.feedback: Prompt feedback.status: Finish reason.attachments: An array of inline data respond by model.
Class: GeminiFileManager
Manages file uploads and retrieval for the Gemini API.
Constructor
new gemini.FileManager(client)client: AGeminiClientinstance.
Methods
async uploadFile(file, isWebFile = false, displayName): Uploads a local or web file to Gemini API.file: File path or URL.isWebFile: Whether the file is a web file. Default isfalse.displayName: Optional display name for the file.- Returns:
Promise<object>- A promise that resolves to the uploaded file object.
async getFilesList(): Retrieves the list of uploaded files.- Returns:
Promise<array>- A promise that resolves to an array of file objects.
- Returns:
apiDeleteFile(file): Deletes an uploaded file.file: File name or object.- Returns:
Promise<RequestResponse>- A promise that resolves to aRequestResponseobject.
async getSmartFile(file, isWebFile = false): Retrieves a file object if it's already uploaded, otherwise returnsundefined.file: File path or URL.isWebFile: Whether the file is a web file.- Returns:
Promise<object|undefined>- A promise that resolves to the file object orundefined.
getSmartName(file, isWebFile, mtime): Generates a smart display name for a file.file: File path or URL.isWebFile: Whether the file is a web file.mtime: File modification time.- Returns:
string- The smart display name.
Properties
client: TheGeminiClientinstance.filesCache: An array of cached file objects.
Class: GeminiFunction
Represents a function that can be called by the Gemini model.
Constructor
new gemini.Function(name, description, parameters = null, func)name: Function name.description: Function description.parameters: Function parameters (in OpenAPI format).func: The actual function to be executed.
Methods
toApiFormat(): Converts theGeminiFunctioninstance to API format.- Returns:
object- Function in API format.
- Returns:
Class: GeminiAPIError
An error that is thrown when the Gemini API returns a non-2xx status code.
Constructor
new GeminiAPIError(code, body, headers)code: The error code.body: The response body.headers: The response headers.
Properties
message: The error message.code: The error code.body: The response body.headers: The response headers.
Helper functions
RequestResponseclass with properties like:code: Status code.headers: Response headers.text(encoding): Return response body in string, with optional encoding. Example:res.text('utf-8').json(): Return response body in JSON format, orundefinedwhen cannot parse JSON. Example:res.json().
Constants
supportMimeTypes: An object mapping file extensions to MIME types.
