ai-function-helper-gemini
v0.0.7
Published
A NodeJS utility to simplify interaction with Gemini Pro Vision.
Downloads
15
Readme
ai-function-helper-gemini
Introduction
ai-function-helper-gemini
is a NodeJS package for creating custom AI functions using Google's Vertex AI with the Gemini model. It simplifies interactions with AI models for a wide range of applications.
Installation
Install the package via npm:
npm install ai-function-helper-gemini
Usage
Examples are provided to demonstrate the package's functionality in diverse scenarios. Below are the available options for configuring the AI function.
Options
When configuring the AI function created by ai-function-helper-gemini
, the following options can be specified:
functionName (string): The name given to the AI function. It's used for identification and logging purposes.
- Default:
""
(Empty string)
- Default:
args (object): Arguments to be passed into the AI function. These are typically the parameters required by your specific AI task.
- Default:
{}
(Empty object)
- Default:
model (string): The name of the AI model to use. This determines the underlying model powering your AI function.
- Default:
"gemini-pro-vision"
- Default:
description (string): A descriptive text explaining the purpose and behavior of the AI function. This helps guide the AI model in generating appropriate responses.
- Default:
""
(Empty string)
- Default:
showDebug (boolean): A flag to enable debug information. When set to true, additional debug information will be printed to the console.
- Default:
false
- Default:
debugLevel (number): Specifies the amount of debug information to display. Higher values provide more detailed debug outputs.
- Default:
0
(Basic information)
- Default:
funcReturn (Zod schema): A Zod schema that defines the expected return structure of the AI function's output. This is used for output validation and ensuring consistency.
- Default:
null
- Default:
blockHijack (boolean): When set to true, the function will reject any attempts to alter its intended behavior or hijack its process.
- Default:
false
- Default:
promptVars (object): A set of variables used to dynamically replace placeholders in the function's description.
- Default:
{}
(Empty object)
- Default:
imagePrompt (string | array): Image data (either in base64 encoding or a URL) to be included as part of the AI function's input.
- Default:
null
- Default:
current_date_time (string): The current date and time, used as part of the function's context or for time-based operations.
- Default:
new Date().toISOString()
- Default:
minifyJSON (boolean): Determines whether the JSON output should be minified. Minified JSON is more compact but less readable.
- Default:
false
- Default:
strictReturn (boolean): When true, the function will strictly validate the returned data against the
funcReturn
Zod schema, ensuring the output precisely matches the expected format.- Default:
false
- Default:
Examples
Example 1: Generating Quiz Questions
const { z } = require('zod');
const { createAiFunctionInstance } = require('ai-function-helper-gemini');
const aiFunction = createAiFunctionInstance('your-project-id', 'us-central1');
const options = {
functionName: 'generateQuiz',
args: { topic: 'science', difficulty: 'easy', numQuestions: 5 },
description: 'Generate a quiz on a specified topic and difficulty.',
funcReturn: z.object({
questions: z.array(z.object({
question: z.string(),
answers: z.array(z.string()),
correctAnswer: z.string()
}))
}),
showDebug: true
};
aiFunction(options).then(result => console.log(result));
Expected Output:
{
"questions": [
{
"question": "What is the chemical symbol for water?",
"answers": ["H2O", "CO2", "O2", "H2"],
"correctAnswer": "H2O"
},
// More questions...
]
}
Example 2: Text Summarization
const { z } = require('zod');
const { createAiFunctionInstance } = require('ai-function-helper-gemini');
const aiFunction = createAiFunctionInstance('your-project-id', 'us-central1');
const options = {
functionName: 'summarizeText',
args: { text: 'Long text to summarize...' },
description: 'Summarize the provided text into a concise paragraph.',
funcReturn: z.object({
summary: z.string()
})
};
aiFunction(options).then(result => console.log(result.summary));
Expected Output:
{
"summary": "This is a summarized version of the provided long text..."
}
Example 3: Image Description
const { z } = require('zod');
const { createAiFunctionInstance } = require('ai-function-helper-gemini');
const aiFunction = createAiFunctionInstance('your-project-id', 'us-central1');
const options = {
functionName: 'describeImage',
args: {},
imagePrompt: 'image-data-base64-or-url',
description: 'Generate a description for the provided image.',
funcReturn: z.object({
description: z.string()
}),
showDebug: false
};
aiFunction(options).then(result => console.log(result.description));
Expected Output:
{
"description": "A scenic view of mountains under a clear blue sky."
}
funcReturn
Explanation
The funcReturn
option in the aiFunction
specifies the expected return format of the AI function. It is validated against a Zod schema, ensuring that the returned data conforms to the defined structure.
funcReturn
Explanation
The funcReturn
option in the aiFunction
is used to specify the expected return format of the AI function. It is validated against a Zod schema, ensuring that the returned data conforms to the defined structure.
Understanding funcReturn
funcReturn
is defined using Zod, a TypeScript-first schema declaration and validation library.- It helps in structuring the expected output of the AI function.
Examples
Quiz Question Format
funcReturn: z.object({ questions: z.array(z.object({ question: z.string(), answers: z.array(z.string()), correctAnswer: z.string() })) })
This schema expects an object with a
questions
array, where each question is an object withquestion
,answers
, andcorrectAnswer
fields.Summarized Text Format
funcReturn: z.object({ summary: z.string() })
Expects an object with a single field
summary
containing a string.Image Description Format
funcReturn: z.object({ description: z.string() })
Expects an object with a
description
field as a string.
Contributing
We welcome contributions. Please fork the repository, make your changes, and submit a pull request.