@umituz/react-native-ai-gemini-provider 3.0.20 → 3.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/infrastructure/services/ChatSession.d.ts +6 -15
- package/dist/infrastructure/services/index.d.ts +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/infrastructure/services/ChatSession.ts +12 -17
- package/src/infrastructure/services/index.ts +1 -1
- package/README.md +0 -191
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
export type { GeminiConfig, GeminiGenerationConfig, GeminiHarmCategory, GeminiHarmBlockThreshold, GeminiContent, GeminiPart, GeminiInlineDataPart, GeminiMessagePart, GeminiSafetySetting, GeminiModelOptions, GeminiChatConfig, GeminiResponse, GeminiCandidate, GeminiFinishReason, GeminiSafetyRating, GeminiUsageMetadata, GeminiErrorInfo, GeminiApiError, } from "./domain/entities";
|
|
6
6
|
export { GeminiErrorType, GeminiError, GEMINI_MODELS, DEFAULT_MODELS } from "./domain/entities";
|
|
7
7
|
export { geminiClient } from "./infrastructure/services/GeminiClient";
|
|
8
|
-
export { createChatSession } from "./infrastructure/services/ChatSession";
|
|
8
|
+
export { createChatSession, type ChatSendResult } from "./infrastructure/services/ChatSession";
|
|
9
9
|
export { textGeneration } from "./infrastructure/services/TextGeneration";
|
|
10
10
|
export { structuredText } from "./infrastructure/services/StructuredText";
|
|
11
11
|
export { streaming } from "./infrastructure/services/Streaming";
|
|
@@ -1,24 +1,15 @@
|
|
|
1
1
|
import type { GeminiChatConfig, GeminiMessagePart } from "../../domain/entities";
|
|
2
|
+
export interface ChatSendResult {
|
|
3
|
+
text: string;
|
|
4
|
+
finishReason?: string;
|
|
5
|
+
}
|
|
2
6
|
/**
|
|
3
7
|
* Creates a Gemini chat session with full support for system instructions,
|
|
4
8
|
* safety settings, generation config, and multi-turn conversation history.
|
|
5
|
-
*
|
|
6
|
-
* Usage:
|
|
7
|
-
* ```ts
|
|
8
|
-
* const session = createChatSession({
|
|
9
|
-
* model: "gemini-2.5-flash",
|
|
10
|
-
* systemInstruction: "You are a helpful assistant.",
|
|
11
|
-
* generationConfig: { temperature: 0.7, maxOutputTokens: 512 },
|
|
12
|
-
* history: geminiHistory,
|
|
13
|
-
* });
|
|
14
|
-
*
|
|
15
|
-
* const text = await session.send([{ text: "Hello" }]);
|
|
16
|
-
* ```
|
|
17
9
|
*/
|
|
18
10
|
export declare function createChatSession(config?: GeminiChatConfig): {
|
|
19
11
|
/**
|
|
20
|
-
* Send a message and return the
|
|
21
|
-
* Accepts text parts and/or inline data parts (images, audio).
|
|
12
|
+
* Send a message and return the response text + finish reason.
|
|
22
13
|
*/
|
|
23
|
-
send(parts: GeminiMessagePart[]): Promise<
|
|
14
|
+
send(parts: GeminiMessagePart[]): Promise<ChatSendResult>;
|
|
24
15
|
};
|
|
@@ -3,4 +3,4 @@ export { textGeneration } from "./TextGeneration";
|
|
|
3
3
|
export { structuredText } from "./StructuredText";
|
|
4
4
|
export { streaming } from "./Streaming";
|
|
5
5
|
export { geminiProvider, GeminiProvider } from "./GeminiProvider";
|
|
6
|
-
export { createChatSession } from "./ChatSession";
|
|
6
|
+
export { createChatSession, type ChatSendResult } from "./ChatSession";
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -34,7 +34,7 @@ export {
|
|
|
34
34
|
|
|
35
35
|
// Services
|
|
36
36
|
export { geminiClient } from "./infrastructure/services/GeminiClient";
|
|
37
|
-
export { createChatSession } from "./infrastructure/services/ChatSession";
|
|
37
|
+
export { createChatSession, type ChatSendResult } from "./infrastructure/services/ChatSession";
|
|
38
38
|
export { textGeneration } from "./infrastructure/services/TextGeneration";
|
|
39
39
|
export { structuredText } from "./infrastructure/services/StructuredText";
|
|
40
40
|
export { streaming } from "./infrastructure/services/Streaming";
|
|
@@ -3,25 +3,17 @@ import { geminiClient } from "./GeminiClient";
|
|
|
3
3
|
import { DEFAULT_MODELS } from "../../domain/entities";
|
|
4
4
|
import type {
|
|
5
5
|
GeminiChatConfig,
|
|
6
|
-
GeminiContent,
|
|
7
6
|
GeminiMessagePart,
|
|
8
7
|
} from "../../domain/entities";
|
|
9
8
|
|
|
9
|
+
export interface ChatSendResult {
|
|
10
|
+
text: string;
|
|
11
|
+
finishReason?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
10
14
|
/**
|
|
11
15
|
* Creates a Gemini chat session with full support for system instructions,
|
|
12
16
|
* safety settings, generation config, and multi-turn conversation history.
|
|
13
|
-
*
|
|
14
|
-
* Usage:
|
|
15
|
-
* ```ts
|
|
16
|
-
* const session = createChatSession({
|
|
17
|
-
* model: "gemini-2.5-flash",
|
|
18
|
-
* systemInstruction: "You are a helpful assistant.",
|
|
19
|
-
* generationConfig: { temperature: 0.7, maxOutputTokens: 512 },
|
|
20
|
-
* history: geminiHistory,
|
|
21
|
-
* });
|
|
22
|
-
*
|
|
23
|
-
* const text = await session.send([{ text: "Hello" }]);
|
|
24
|
-
* ```
|
|
25
17
|
*/
|
|
26
18
|
export function createChatSession(config: GeminiChatConfig = {}) {
|
|
27
19
|
const model = geminiClient.getModel({
|
|
@@ -42,13 +34,16 @@ export function createChatSession(config: GeminiChatConfig = {}) {
|
|
|
42
34
|
|
|
43
35
|
return {
|
|
44
36
|
/**
|
|
45
|
-
* Send a message and return the
|
|
46
|
-
* Accepts text parts and/or inline data parts (images, audio).
|
|
37
|
+
* Send a message and return the response text + finish reason.
|
|
47
38
|
*/
|
|
48
|
-
async send(parts: GeminiMessagePart[]): Promise<
|
|
39
|
+
async send(parts: GeminiMessagePart[]): Promise<ChatSendResult> {
|
|
49
40
|
const result = await chat.sendMessage(parts as Part[]);
|
|
50
41
|
if (!result.response) throw new Error("No response from Gemini SDK");
|
|
51
|
-
|
|
42
|
+
const candidate = result.response.candidates?.[0];
|
|
43
|
+
return {
|
|
44
|
+
text: result.response.text(),
|
|
45
|
+
finishReason: candidate?.finishReason ?? undefined,
|
|
46
|
+
};
|
|
52
47
|
},
|
|
53
48
|
};
|
|
54
49
|
}
|
|
@@ -3,4 +3,4 @@ export { textGeneration } from "./TextGeneration";
|
|
|
3
3
|
export { structuredText } from "./StructuredText";
|
|
4
4
|
export { streaming } from "./Streaming";
|
|
5
5
|
export { geminiProvider, GeminiProvider } from "./GeminiProvider";
|
|
6
|
-
export { createChatSession } from "./ChatSession";
|
|
6
|
+
export { createChatSession, type ChatSendResult } from "./ChatSession";
|
package/README.md
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
# @umituz/react-native-ai-gemini-provider
|
|
2
|
-
|
|
3
|
-
Production-ready Google Gemini AI provider for React Native applications with built-in error handling, rate limiting, and caching.
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- ✅ **Production Ready** - Real API integration with proper error handling
|
|
8
|
-
- ✅ **Type-Safe** - Full TypeScript support with structured output
|
|
9
|
-
- ✅ **Cost Optimized** - Uses `gemini-2.5-flash-lite` by default (1000 free requests/day)
|
|
10
|
-
- ✅ **Rate Limiting** - Built-in rate limiter to prevent API throttling
|
|
11
|
-
- ✅ **Caching** - LRU cache with TTL for performance optimization
|
|
12
|
-
- ✅ **Retry Logic** - Exponential backoff with jitter for resilient API calls
|
|
13
|
-
- ✅ **No Mock Data** - Production-grade implementation
|
|
14
|
-
|
|
15
|
-
## Installation
|
|
16
|
-
|
|
17
|
-
```bash
|
|
18
|
-
npm install @umituz/react-native-ai-gemini-provider
|
|
19
|
-
npm install @google/generative-ai
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Configuration
|
|
23
|
-
|
|
24
|
-
### 1. Get API Key
|
|
25
|
-
|
|
26
|
-
Visit [Google AI Studio](https://aistudio.google.com/apikey) and create an API key.
|
|
27
|
-
|
|
28
|
-
### 2. Environment Setup
|
|
29
|
-
|
|
30
|
-
Create `.env` file:
|
|
31
|
-
|
|
32
|
-
```env
|
|
33
|
-
EXPO_PUBLIC_GEMINI_API_KEY=your_api_key_here
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
### 3. Initialize Provider
|
|
37
|
-
|
|
38
|
-
```typescript
|
|
39
|
-
import { geminiClientCoreService } from '@umituz/react-native-ai-gemini-provider';
|
|
40
|
-
|
|
41
|
-
geminiClientCoreService.initialize({
|
|
42
|
-
apiKey: process.env.EXPO_PUBLIC_GEMINI_API_KEY,
|
|
43
|
-
textModel: 'gemini-2.5-flash-lite',
|
|
44
|
-
maxRetries: 3,
|
|
45
|
-
baseDelay: 1000,
|
|
46
|
-
maxDelay: 10000,
|
|
47
|
-
});
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
## Usage
|
|
51
|
-
|
|
52
|
-
### Simple Text Generation
|
|
53
|
-
|
|
54
|
-
```typescript
|
|
55
|
-
import { geminiTextService } from '@umituz/react-native-ai-gemini-provider';
|
|
56
|
-
|
|
57
|
-
const response = await geminiTextService.generateText(
|
|
58
|
-
'gemini-2.5-flash-lite',
|
|
59
|
-
'Write a short greeting in Turkish'
|
|
60
|
-
);
|
|
61
|
-
console.log(response); // "Merhaba! Bugün size nasıl yardımcı olabilirim?"
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
### Structured Output (JSON)
|
|
65
|
-
|
|
66
|
-
```typescript
|
|
67
|
-
import { geminiStructuredTextService } from '@umituz/react-native-ai-gemini-provider';
|
|
68
|
-
|
|
69
|
-
interface AnalysisResult {
|
|
70
|
-
score: number;
|
|
71
|
-
feedback: string;
|
|
72
|
-
suggestions: string[];
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const schema = {
|
|
76
|
-
type: 'object',
|
|
77
|
-
properties: {
|
|
78
|
-
score: { type: 'number' },
|
|
79
|
-
feedback: { type: 'string' },
|
|
80
|
-
suggestions: { type: 'array', items: { type: 'string' } },
|
|
81
|
-
},
|
|
82
|
-
required: ['score', 'feedback', 'suggestions'],
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
const result = await geminiStructuredTextService.generateStructuredText<AnalysisResult>(
|
|
86
|
-
'gemini-2.5-flash-lite',
|
|
87
|
-
'Analyze this text',
|
|
88
|
-
schema
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
console.log(result.score); // 85
|
|
92
|
-
console.log(result.feedback); // "Good quality overall..."
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
### With Rate Limiting & Caching
|
|
96
|
-
|
|
97
|
-
```typescript
|
|
98
|
-
import {
|
|
99
|
-
geminiTextService,
|
|
100
|
-
rateLimiter,
|
|
101
|
-
SimpleCache
|
|
102
|
-
} from '@umituz/react-native-ai-gemini-provider';
|
|
103
|
-
|
|
104
|
-
const cache = new SimpleCache<string, string>({
|
|
105
|
-
maxSize: 50,
|
|
106
|
-
ttl: 60000 // 1 minute
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
const cacheKey = `text_${prompt}`;
|
|
110
|
-
const cached = cache.get(cacheKey);
|
|
111
|
-
|
|
112
|
-
if (cached) {
|
|
113
|
-
return cached;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const response = await rateLimiter.execute(() =>
|
|
117
|
-
geminiTextService.generateText('gemini-2.5-flash-lite', prompt)
|
|
118
|
-
);
|
|
119
|
-
|
|
120
|
-
cache.set(cacheKey, response);
|
|
121
|
-
return response;
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
## Production Example
|
|
125
|
-
|
|
126
|
-
See `examples/prod-ai-service.ts` for a complete production-ready implementation with:
|
|
127
|
-
|
|
128
|
-
- ✅ Content analysis
|
|
129
|
-
- ✅ Motivation letter analysis
|
|
130
|
-
- ✅ Chat assistant
|
|
131
|
-
- ✅ Text summarization
|
|
132
|
-
- ✅ Batch processing
|
|
133
|
-
- ✅ Error handling with retry
|
|
134
|
-
|
|
135
|
-
## Models
|
|
136
|
-
|
|
137
|
-
| Model | Input | Output | Free/Day | Best For |
|
|
138
|
-
|-------|-------|--------|-----------|----------|
|
|
139
|
-
| `gemini-2.5-flash-lite` | $0.10 | $0.40 | 1000 | High volume, simple tasks |
|
|
140
|
-
| `gemini-2.5-flash` | $0.15 | $0.60 | 20 | Balanced speed/quality |
|
|
141
|
-
| `gemini-2.5-pro` | $1.25 | $10.00 | 25 | Complex reasoning |
|
|
142
|
-
|
|
143
|
-
**Recommendation:** Use `gemini-2.5-flash-lite` for most use cases.
|
|
144
|
-
|
|
145
|
-
## API Reference
|
|
146
|
-
|
|
147
|
-
### Services
|
|
148
|
-
|
|
149
|
-
- `geminiClientCoreService` - Client initialization and configuration
|
|
150
|
-
- `geminiTextService` - Text generation (alias: `geminiTextGenerationService`)
|
|
151
|
-
- `geminiStructuredTextService` - JSON structured output
|
|
152
|
-
- `geminiRetryService` - Retry logic with exponential backoff
|
|
153
|
-
|
|
154
|
-
### Utilities
|
|
155
|
-
|
|
156
|
-
- `rateLimiter` - Request rate limiting
|
|
157
|
-
- `SimpleCache` - LRU cache with TTL
|
|
158
|
-
- `measureAsync` / `measureSync` - Performance measurement
|
|
159
|
-
|
|
160
|
-
## Error Handling
|
|
161
|
-
|
|
162
|
-
```typescript
|
|
163
|
-
try {
|
|
164
|
-
const result = await geminiTextService.generateText('gemini-2.5-flash-lite', prompt);
|
|
165
|
-
} catch (error) {
|
|
166
|
-
if (error instanceof Error) {
|
|
167
|
-
if (error.message.includes('API key')) {
|
|
168
|
-
// Handle missing/invalid API key
|
|
169
|
-
} else if (error.message.includes('429') || error.message.includes('quota')) {
|
|
170
|
-
// Handle rate limit - wait and retry
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
## Best Practices
|
|
177
|
-
|
|
178
|
-
1. **Always initialize** the client before making requests
|
|
179
|
-
2. **Use rate limiting** to prevent API throttling
|
|
180
|
-
3. **Implement caching** for frequently used prompts
|
|
181
|
-
4. **Handle errors** gracefully with retry logic
|
|
182
|
-
5. **Use structured output** for consistent JSON responses
|
|
183
|
-
6. **Choose the right model** based on complexity and cost
|
|
184
|
-
|
|
185
|
-
## License
|
|
186
|
-
|
|
187
|
-
MIT
|
|
188
|
-
|
|
189
|
-
## Support
|
|
190
|
-
|
|
191
|
-
For detailed integration guide, see: [factory/templates/docs/ai/GEMINI_AI_PROVIDER.md](https://github.com/umituz/react-native-app-factory/blob/main/factory/templates/docs/ai/GEMINI_AI_PROVIDER.md)
|