कोर API
पूर्ण Gemini CLI कोर API संदर्भ और उपयोग गाइड
API मॉड्यूल
मुख्य API मॉड्यूल और विधियां
GeminiClient
मुख्य Gemini API क्लाइंट क्लास
src/core/gemini.tsconstructor
constructor(config: GeminiConfig)Gemini क्लाइंट इंस्टेंस बनाएं
पैरामीटर:
configGeminiConfigक्लाइंट कॉन्फ़िगरेशन ऑब्जेक्ट
उदाहरण:
const client = new GeminiClient({
apiKey: process.env.GEMINI_API_KEY,
model: 'gemini-pro',
temperature: 0.7
})chat
async chat(message: string, context?: Context): Promise<ChatResponse>चैट संदेश भेजें और प्रतिक्रिया प्राप्त करें
पैरामीटर:
messagestringउपयोगकर्ता इनपुट संदेश
contextContextवैकल्पिक संदर्भ जानकारी
रिटर्न:
Promise<ChatResponse>चैट प्रतिक्रिया ऑब्जेक्ट
उदाहरण:
const response = await client.chat('Hello, Gemini!', {
files: ['src/index.ts'],
history: previousMessages
})stream
async stream(message: string, context?: Context): AsyncGenerator<string>स्ट्रीमिंग चैट प्रतिक्रिया
पैरामीटर:
messagestringउपयोगकर्ता इनपुट संदेश
contextContextवैकल्पिक संदर्भ जानकारी
रिटर्न:
AsyncGenerator<string>स्ट्रीमिंग प्रतिक्रिया जेनरेटर
उदाहरण:
for await (const chunk of client.stream('Explain this code')) {
process.stdout.write(chunk)
}SessionManager
सेशन स्थिति प्रबंधक
src/core/session.tscreateSession
createSession(id?: string): Sessionनया सेशन बनाएं
पैरामीटर:
idstringवैकल्पिक सेशन ID
रिटर्न:
Sessionनया बनाया गया सेशन ऑब्जेक्ट
उदाहरण:
const session = sessionManager.createSession('my-session')getSession
getSession(id: string): Session | nullनिर्दिष्ट सेशन प्राप्त करें
पैरामीटर:
idstringसेशन ID
रिटर्न:
Session | nullसेशन ऑब्जेक्ट या null
उदाहरण:
const session = sessionManager.getSession('my-session')saveSession
async saveSession(session: Session): Promise<void>सेशन को स्थायी भंडारण में सहेजें
पैरामीटर:
sessionSessionसहेजने के लिए सेशन ऑब्जेक्ट
उदाहरण:
await sessionManager.saveSession(session)
ContextManager
संदर्भ प्रसंस्करण प्रबंधक
src/core/context.tsaddFile
async addFile(filePath: string): Promise<void>संदर्भ में फ़ाइल जोड़ें
पैरामीटर:
filePathstringफ़ाइल पथ
उदाहरण:
await contextManager.addFile('src/utils/helper.ts')addDirectory
async addDirectory(dirPath: string, options?: DirOptions): Promise<void>संदर्भ में निर्देशिका जोड़ें
पैरामीटर:
dirPathstringनिर्देशिका पथ
optionsDirOptionsनिर्देशिका स्कैनिंग विकल्प
उदाहरण:
await contextManager.addDirectory('src/', {
exclude: ['*.test.ts'],
maxDepth: 3
})getContext
getContext(): Contextवर्तमान संदर्भ प्राप्त करें
रिटर्न:
Contextवर्तमान संदर्भ ऑब्जेक्ट
उदाहरण:
const context = contextManager.getContext()
प्रकार परिभाषाएं
कोर API के लिए TypeScript प्रकार परिभाषाएं
GeminiConfig
Gemini क्लाइंट कॉन्फ़िगरेशन इंटरफेस
apiKeystringGoogle AI API कुंजी
modelstringउपयोग करने के लिए मॉडल नाम, डिफ़ॉल्ट "gemini-pro"
temperaturenumberजेनरेशन तापमान, 0-1 के बीच
maxTokensnumberटोकन की अधिकतम संख्या
timeoutnumberमिलीसेकंड में अनुरोध समय सीमा
ChatResponse
चैट प्रतिक्रिया इंटरफेस
contentstringप्रतिक्रिया सामग्री
usageTokenUsageटोकन उपयोग जानकारी
modelstringउपयोग किया गया मॉडल
finishReasonstringसमाप्ति कारण
Context
संदर्भ जानकारी इंटरफेस
filesFileContext[]फ़ाइल संदर्भ सूची
historyMessage[]संदेश इतिहास
metadataRecord<string, any>मेटाडेटा
Session
सेशन इंटरफेस
idstringअद्वितीय सेशन पहचानकर्ता
messagesMessage[]संदेश इतिहास
contextContextसेशन संदर्भ
createdAtDateनिर्माण समय
updatedAtDateअपडेट समय
उपयोग उदाहरण
सामान्य API उपयोग परिदृश्य और कोड उदाहरण
बुनियादी चैट
सरल चैट इंटरैक्शन उदाहरण
import { GeminiClient } from '@gemini/core'
const client = new GeminiClient({
apiKey: process.env.GEMINI_API_KEY,
model: 'gemini-pro'
})
async function basicChat() {
try {
const response = await client.chat('Hello, how are you?')
console.log('AI:', response.content)
} catch (error) {
console.error('Chat error:', error.message)
}
}फ़ाइल संदर्भ
फ़ाइल संदर्भ के साथ चैट
import { GeminiClient, ContextManager } from '@gemini/core'
const client = new GeminiClient({ apiKey: process.env.GEMINI_API_KEY })
const contextManager = new ContextManager()
async function chatWithFiles() {
// Add files to context
await contextManager.addFile('src/utils/helper.ts')
await contextManager.addDirectory('src/components/')
const context = contextManager.getContext()
const response = await client.chat(
'Explain the helper functions in this codebase',
context
)
console.log(response.content)
}सेशन प्रबंधन
स्थायी सेशन का प्रबंधन
import { SessionManager } from '@gemini/core'
const sessionManager = new SessionManager()
async function manageSessions() {
// Create new session
const session = sessionManager.createSession('project-review')
// Add messages to session
session.messages.push({
role: 'user',
content: 'Review this code for best practices'
})
// Save session
await sessionManager.saveSession(session)
// Restore session later
const restored = sessionManager.getSession('project-review')
console.log('Session messages:', restored?.messages.length)
}स्ट्रीमिंग प्रतिक्रिया
रियल-टाइम स्ट्रीमिंग आउटपुट
import { GeminiClient } from '@gemini/core'
const client = new GeminiClient({ apiKey: process.env.GEMINI_API_KEY })
async function streamingChat() {
console.log('AI: ')
for await (const chunk of client.stream('Write a short story')) {
process.stdout.write(chunk)
}
console.log('\n\nStream completed!')
}त्रुटि प्रबंधन
API त्रुटि प्रकार और प्रबंधन विधियां
AuthenticationError
अमान्य या समाप्त API कुंजी
GEMINI_AUTH_ERRORसमाधान:
जांचें कि API कुंजी सही तरीके से सेट है, सुनिश्चित करें कि कुंजी वैध है और समाप्त नहीं हुई है
RateLimitError
अनुरोध आवृत्ति सीमा से अधिक है
GEMINI_RATE_LIMITसमाधान:
एक्सपोनेंशियल बैकऑफ रिट्राई मैकेनिज्म लागू करें, या उच्चतर कोटा प्लान में अपग्रेड करें
ContextTooLargeError
संदर्भ सामग्री मॉडल सीमा से अधिक है
GEMINI_CONTEXT_TOO_LARGEसमाधान:
संदर्भ फ़ाइलों की संख्या कम करें या फ़ाइल सारांश सुविधा का उपयोग करें
NetworkError
नेटवर्क कनेक्शन समस्याएं
GEMINI_NETWORK_ERRORसमाधान:
नेटवर्क कनेक्शन जांचें, प्रॉक्सी सेटिंग्स कॉन्फ़िगर करें, या टाइमआउट बढ़ाएं
अगले चरण
इन संसाधनों के साथ अपनी विकास यात्रा जारी रखें