도구 API
AI 상호작용을 위한 사용자 정의 도구를 생성하고 관리합니다. 파일 작업, 셸 명령, 웹 요청 등을 위한 강력한 도구로 Gemini CLI를 확장하세요.
내장 도구
Gemini CLI에서 즉시 사용할 수 있는 강력한 도구
파일 시스템 도구
파일 읽기, 쓰기 및 관리를 위한 도구
read_file
파일에서 내용 읽기
시그니처:
read_file(path: string): Promise<string>예제:
const content = await tools.read_file('./package.json');
console.log(content);write_file
파일에 내용 쓰기
시그니처:
write_file(path: string, content: string): Promise<void>예제:
await tools.write_file('./output.txt', 'Hello, World!');list_files
디렉토리의 파일 목록 표시
시그니처:
list_files(path: string): Promise<string[]>예제:
const files = await tools.list_files('./src');
console.log(files);셸 도구
셸 명령을 실행하기 위한 도구
run_shell_command
셸 명령 실행
시그니처:
run_shell_command(command: string, options?: ShellOptions): Promise<ShellResult>예제:
const result = await tools.run_shell_command('ls -la');
console.log(result.stdout);run_script
스크립트 파일 실행
시그니처:
run_script(scriptPath: string, args?: string[]): Promise<ShellResult>예제:
const result = await tools.run_script('./build.sh', ['--production']);웹 도구
웹 요청 및 검색을 위한 도구
web_fetch
URL에서 콘텐츠 가져오기
시그니처:
web_fetch(url: string, options?: FetchOptions): Promise<string>예제:
const content = await tools.web_fetch('https://api.example.com/data');
console.log(content);web_search
웹에서 정보 검색
시그니처:
web_search(query: string, options?: SearchOptions): Promise<SearchResult[]>예제:
const results = await tools.web_search('Gemini CLI documentation');
console.log(results);사용자 정의 도구 개발
Gemini CLI 기능을 확장하는 자체 도구 생성
사용자 정의 도구 생성
사용자 정의 날씨 도구를 만드는 완전한 예제입니다:
import { Tool, ToolDefinition } from '@google/generative-ai-cli';
// 사용자 정의 도구 정의
const weatherTool: ToolDefinition = {
name: 'get_weather',
description: '위치의 현재 날씨 가져오기',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: '도시와 주, 예: 서울, 한국'
},
unit: {
type: 'string',
enum: ['celsius', 'fahrenheit'],
description: '온도 단위'
}
},
required: ['location']
},
execute: async ({ location, unit = 'celsius' }) => {
// 여기에 날씨 API 로직
const response = await fetch(`https://api.weather.com/v1/current?location=${location}&unit=${unit}`);
const data = await response.json();
return `${location}의 현재 날씨: ${data.temperature}°${unit === 'celsius' ? 'C' : 'F'}, ${data.description}`;
}
};
// 도구 등록
gemini.registerTool(weatherTool);
// 대화에서 도구 사용
const response = await gemini.ask("서울 날씨는 어떤가요?", {
tools: ['get_weather']
});도구 등록 및 관리
Gemini CLI 인스턴스에서 도구 관리
도구 등록
// 단일 도구 등록
gemini.registerTool(myTool);
// 여러 도구 등록
gemini.registerTools([tool1, tool2, tool3]);
// 플러그인에서 로드
gemini.loadPlugin('./my-tools-plugin');도구 목록 및 관리
// 사용 가능한 모든 도구 나열
const tools = gemini.listTools();
// 도구 정보 가져오기
const toolInfo = gemini.getTool('tool_name');
// 도구 등록 해제
gemini.unregisterTool('tool_name');모범 사례
효과적인 도구 생성을 위한 가이드라인
도구 설계
- • 도구를 단일 책임에 집중시키기
- • 명확하고 설명적인 이름과 설명 제공
- • 포괄적인 매개변수 스키마 정의
- • 오류를 우아하게 처리하고 의미 있는 메시지 제공
성능
- • 비동기 작업을 올바르게 구현
- • 장시간 작업에 대한 타임아웃 처리 추가
- • 적절할 때 결과 캐시
- • 처리 전에 입력 검증