कोर API

पूर्ण Gemini CLI कोर API संदर्भ और उपयोग गाइड

API संदर्भ30 मिनट पढ़ना

API मॉड्यूल

मुख्य API मॉड्यूल और विधियां

GeminiClient

मुख्य Gemini API क्लाइंट क्लास

src/core/gemini.ts

constructor

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.ts

createSession

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.ts

addFile

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 क्लाइंट कॉन्फ़िगरेशन इंटरफेस

apiKeystring
आवश्यक

Google 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

समाधान:

नेटवर्क कनेक्शन जांचें, प्रॉक्सी सेटिंग्स कॉन्फ़िगर करें, या टाइमआउट बढ़ाएं

अगले चरण

इन संसाधनों के साथ अपनी विकास यात्रा जारी रखें