टूल डेवलपमेंट
Gemini CLI की क्षमताओं को बढ़ाने के लिए कस्टम टूल्स बनाएं। AI बातचीत के साथ निर्बाध रूप से एकीकृत होने वाले टूल्स को डिज़ाइन, लागू, परीक्षण और तैनात करना सीखें।
विकास प्रक्रिया
कस्टम टूल्स बनाने के लिए चरणबद्ध गाइड
टूल इंटरफेस परिभाषित करें
पैरामीटर और विवरण के साथ टूल परिभाषा बनाएं
उदाहरण:
const toolDefinition = {
name: 'calculate_age',
description: 'Calculate age based on birth date',
parameters: {
type: 'object',
properties: {
birthDate: {
type: 'string',
format: 'date',
description: 'Birth date in YYYY-MM-DD format'
},
currentDate: {
type: 'string',
format: 'date',
description: 'Current date (optional, defaults to today)'
}
},
required: ['birthDate']
}
};टूल लॉजिक लागू करें
टूल के कार्य को निष्पादित करने वाला निष्पादन फ़ंक्शन लिखें
उदाहरण:
const executeFunction = async ({ birthDate, currentDate }) => {
try {
const birth = new Date(birthDate);
const current = currentDate ? new Date(currentDate) : new Date();
if (birth > current) {
throw new Error('Birth date cannot be in the future');
}
const ageMs = current.getTime() - birth.getTime();
const ageYears = Math.floor(ageMs / (1000 * 60 * 60 * 24 * 365.25));
return `Age: ${ageYears} years old`;
} catch (error) {
return `Error: ${error.message}`;
}
};टूल पंजीकृत करें
टूल को Gemini CLI के टूल रजिस्ट्री में जोड़ें
उदाहरण:
import { GeminiCLI } from '@google/generative-ai-cli';
const cli = new GeminiCLI();
// Create complete tool
const ageTool = {
...toolDefinition,
execute: executeFunction
};
// Register the tool
cli.registerTool(ageTool);
// Verify registration
console.log('Registered tools:', cli.listTools().map(t => t.name));टूल का परीक्षण करें
यह सुनिश्चित करने के लिए अपने टूल का परीक्षण करें कि यह सही तरीके से काम करता है
उदाहरण:
// Test the tool directly
const result = await cli.executeTool('calculate_age', {
birthDate: '1990-05-15'
});
console.log(result); // "Age: 34 years old"
// Test with AI
const response = await cli.ask(
"How old would someone born on May 15, 1990 be today?",
{ tools: ['calculate_age'] }
);टूल टेम्प्लेट
अपने कस्टम टूल डेवलपमेंट को जल्दी शुरू करने के लिए इस टेम्प्लेट का उपयोग करें
बेसिक टूल टेम्प्लेट
इस टेम्प्लेट को कॉपी करें और अपनी आवश्यकताओं के अनुसार इसे संशोधित करें। नाम, विवरण, पैरामीटर और निष्पादन तर्क को अपडेट करना सुनिश्चित करें।
import { Tool, ToolDefinition } from '@google/generative-ai-cli';
export const myCustomTool: ToolDefinition = {
name: 'my_custom_tool',
description: 'Description of what this tool does',
parameters: {
type: 'object',
properties: {
// Define your parameters here
input: {
type: 'string',
description: 'Input parameter description'
}
},
required: ['input']
},
execute: async (params) => {
try {
// Your tool logic here
const { input } = params;
// Process the input
const result = processInput(input);
// Return the result
return result;
} catch (error) {
// Handle errors gracefully
return `Error: ${error.message}`;
}
}
};
function processInput(input: string): string {
// Your processing logic
return `Processed: ${input}`;
}सर्वोत्तम प्रथाएं
उच्च गुणवत्ता, विश्वसनीय टूल्स बनाने के लिए इन दिशानिर्देशों का पालन करें
टूल डिज़ाइन
- टूल्स को एकल जिम्मेदारी पर केंद्रित रखें
- स्पष्ट, वर्णनात्मक नाम और विवरण का उपयोग करें
- व्यापक पैरामीटर स्कीमा परिभाषित करें
- पैरामीटर विवरण में उदाहरण शामिल करें
- किनारे के मामलों को सुंदर तरीके से संभालें
त्रुटि प्रबंधन
- हमेशा try-catch ब्लॉक में निष्पादन को लपेटें
- अर्थपूर्ण त्रुटि संदेश वापस करें
- इनपुट पैरामीटर को मान्य करें
- नेटवर्क टाइमआउट और विफलताओं को संभालें
- डिबगिंग के लिए त्रुटियों को लॉग करें
प्रदर्शन
- एसिंक ऑपरेशन को सही तरीके से लागू करें
- लंबे ऑपरेशन के लिए टाइमआउट हैंडलिंग जोड़ें
- उपयुक्त होने पर परिणामों को कैश करें
- बाहरी निर्भरताओं को कम करें
- बड़े रिस्पॉन्स के लिए स्ट्रीमिंग का उपयोग करें
सुरक्षा
- सभी इनपुट को मान्य और साफ करें
- मनमाने कोड निष्पादन से बचें
- फ़ाइल सिस्टम एक्सेस को सीमित करें
- सुरक्षित API एंडपॉइंट का उपयोग करें
- संवेदनशील डेटा को सावधानी से संभालें
अपने टूल्स का परीक्षण करें
तैनाती से पहले सुनिश्चित करें कि आपके टूल्स सही तरीके से काम करते हैं
यूनिट टेस्टिंग
// Test tool execution directly
import { myCustomTool } from './my-tool';
describe('myCustomTool', () => {
test('should process input correctly', async () => {
const result = await myCustomTool.execute({
input: 'test data'
});
expect(result).toBe('Processed: test data');
});
test('should handle errors gracefully', async () => {
const result = await myCustomTool.execute({
input: null
});
expect(result).toMatch(/Error:/);
});
});एकीकरण परीक्षण
// Test tool with Gemini CLI
import { GeminiCLI } from '@google/generative-ai-cli';
const cli = new GeminiCLI();
cli.registerTool(myCustomTool);
// Test tool registration
const tools = cli.listTools();
expect(tools.find(t => t.name === 'my_custom_tool')).toBeDefined();
// Test tool execution
const result = await cli.executeTool('my_custom_tool', {
input: 'test'
});
expect(result).toBeDefined();परीक्षण और डिबगिंग
आपके टूल्स को विश्वसनीय रूप से चलाने के लिए टिप्स
परीक्षण रणनीतियां
- टूल फ़ंक्शन को अलग से टेस्ट करें
- विभिन्न इनपुट पैरामीटर के साथ टेस्ट करें
- त्रुटि स्थितियों और एज केसेस का परीक्षण करें
- AI के साथ एकीकरण को सत्यापित करें
डिबगिंग टिप्स
- विस्तृत लॉगिंग जोड़ें
- डिबगिंग के लिए console.log का उपयोग करें
- पैरामीटर सत्यापन की जांच करें
- प्रदर्शन मेट्रिक्स की निगरानी करें
सामान्य टूल पैटर्न
विभिन्न प्रकार के टूल्स के लिए पुन: उपयोग योग्य पैटर्न
API एकीकरण टूल
बाहरी API के साथ एकीकृत होने वाले टूल्स के लिए पैटर्न
const apiTool = {
name: 'api_call',
description: 'Make API calls to external services',
parameters: {
type: 'object',
properties: {
endpoint: { type: 'string', description: 'API endpoint URL' },
method: { type: 'string', enum: ['GET', 'POST'], default: 'GET' },
data: { type: 'object', description: 'Request payload' }
},
required: ['endpoint']
},
execute: async ({ endpoint, method = 'GET', data }) => {
try {
const response = await fetch(endpoint, {
method,
headers: { 'Content-Type': 'application/json' },
body: data ? JSON.stringify(data) : undefined
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return await response.json();
} catch (error) {
return `API Error: ${error.message}`;
}
}
};डेटा प्रोसेसिंग टूल
डेटा को प्रोसेस और ट्रांसफॉर्म करने वाले टूल्स के लिए पैटर्न
const processingTool = {
name: 'data_processor',
description: 'Process and transform data',
parameters: {
type: 'object',
properties: {
data: { type: 'array', description: 'Data to process' },
operation: {
type: 'string',
enum: ['sort', 'filter', 'map', 'reduce'],
description: 'Operation to perform'
},
criteria: { type: 'string', description: 'Processing criteria' }
},
required: ['data', 'operation']
},
execute: async ({ data, operation, criteria }) => {
try {
switch (operation) {
case 'sort':
return data.sort();
case 'filter':
return data.filter(item => item.includes(criteria));
case 'map':
return data.map(item => `${criteria}: ${item}`);
case 'reduce':
return data.reduce((acc, item) => acc + item, '');
default:
throw new Error(`Unknown operation: ${operation}`);
}
} catch (error) {
return `Processing Error: ${error.message}`;
}
}
};