@dataclouder/nest-vertex 0.0.8 → 0.0.9
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/index.d.ts +1 -0
- package/index.js +1 -0
- package/models/gemini-models.d.ts +5 -0
- package/models/gemini-models.js +10 -0
- package/package.json +1 -1
- package/services/gemini-chat.service.d.ts +2 -1
- package/services/gemini-chat.service.js +21 -3
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -21,4 +21,5 @@ __exportStar(require("./services/nest-tts.service"), exports);
|
|
|
21
21
|
__exportStar(require("./services/gemini-chat.service"), exports);
|
|
22
22
|
__exportStar(require("./services/vertex-image.service"), exports);
|
|
23
23
|
__exportStar(require("./dto/generate-image.dto"), exports);
|
|
24
|
+
__exportStar(require("./models/gemini-models"), exports);
|
|
24
25
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GeminiModels = void 0;
|
|
4
|
+
var GeminiModels;
|
|
5
|
+
(function (GeminiModels) {
|
|
6
|
+
GeminiModels["Flash2_5"] = "gemini-2.5-flash-preview-04-17";
|
|
7
|
+
GeminiModels["Flash2_5Pro"] = "gemini-2.5-pro-preview-05-06";
|
|
8
|
+
GeminiModels["Flash2_0Lite"] = "gemini-2.0-flash-lite";
|
|
9
|
+
})(GeminiModels || (exports.GeminiModels = GeminiModels = {}));
|
|
10
|
+
//# sourceMappingURL=gemini-models.js.map
|
package/package.json
CHANGED
|
@@ -20,6 +20,7 @@ export declare class GeminiChatService {
|
|
|
20
20
|
chat(messages: ChatCompletionMessageParam[], model?: string): Promise<ChatMessageDict>;
|
|
21
21
|
chatStream(messages: ChatCompletionMessageParam[]): Promise<AsyncIterable<ChatMessageDict>>;
|
|
22
22
|
listModels(): Promise<Record<string, string>[]>;
|
|
23
|
+
private extractJsonFromResponseBackup;
|
|
23
24
|
private extractJsonFromResponse;
|
|
24
|
-
chatAndExtractJson(messages: ChatCompletionMessageParam[], model?:
|
|
25
|
+
chatAndExtractJson(messages: ChatCompletionMessageParam[], model?: string): Promise<any>;
|
|
25
26
|
}
|
|
@@ -14,6 +14,7 @@ exports.GeminiChatService = exports.ChatMessageDict = exports.ChatRole = void 0;
|
|
|
14
14
|
const common_1 = require("@nestjs/common");
|
|
15
15
|
const genai_1 = require("@google/genai");
|
|
16
16
|
const nest_core_1 = require("@dataclouder/nest-core");
|
|
17
|
+
const gemini_models_1 = require("../models/gemini-models");
|
|
17
18
|
var ChatRole;
|
|
18
19
|
(function (ChatRole) {
|
|
19
20
|
ChatRole["System"] = "system";
|
|
@@ -149,7 +150,8 @@ let GeminiChatService = GeminiChatService_1 = class GeminiChatService {
|
|
|
149
150
|
{ id: 'gemini-2.0-flash-live-001' },
|
|
150
151
|
];
|
|
151
152
|
}
|
|
152
|
-
|
|
153
|
+
extractJsonFromResponseBackup(content) {
|
|
154
|
+
console.log(content);
|
|
153
155
|
const jsonMatch = content.match(/(\{[\s\S]*?\}|\[[\s\S]*?\])/);
|
|
154
156
|
if (!jsonMatch)
|
|
155
157
|
return null;
|
|
@@ -161,7 +163,23 @@ let GeminiChatService = GeminiChatService_1 = class GeminiChatService {
|
|
|
161
163
|
throw new Error(`Failed to parse JSON: ${error.message}`);
|
|
162
164
|
}
|
|
163
165
|
}
|
|
164
|
-
|
|
166
|
+
extractJsonFromResponse(content) {
|
|
167
|
+
let cleanedContent = content.trim();
|
|
168
|
+
if (cleanedContent.startsWith('```json')) {
|
|
169
|
+
cleanedContent = cleanedContent.substring('```json'.length);
|
|
170
|
+
}
|
|
171
|
+
if (cleanedContent.endsWith('```')) {
|
|
172
|
+
cleanedContent = cleanedContent.substring(0, cleanedContent.length - '```'.length);
|
|
173
|
+
}
|
|
174
|
+
cleanedContent = cleanedContent.trim();
|
|
175
|
+
try {
|
|
176
|
+
return JSON.parse(cleanedContent);
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
return this.extractJsonFromResponseBackup(cleanedContent);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async chatAndExtractJson(messages, model = gemini_models_1.GeminiModels.Flash2_5) {
|
|
165
183
|
const response = await this.chat(messages, model);
|
|
166
184
|
this.logger.debug(`Initial response content: ${response.content}`);
|
|
167
185
|
try {
|
|
@@ -174,7 +192,7 @@ let GeminiChatService = GeminiChatService_1 = class GeminiChatService {
|
|
|
174
192
|
catch (initialError) {
|
|
175
193
|
this.logger.warn(`Initial JSON extraction failed: ${initialError.message}. Attempting recovery.`);
|
|
176
194
|
try {
|
|
177
|
-
const fixPrompt = `
|
|
195
|
+
const fixPrompt = `I cant extract the json from the following text, something is wrong, please analyze the following text, correct any JSON formatting errors invalid characters or whatever thing is wrong, change or complete the text if needed, and return ONLY the valid JSON object or array. Do not include any explanatory text before or after the JSON itself. JSON to fix: ${response.content}`;
|
|
178
196
|
const fixResponse = await this.chat([{ role: 'user', content: fixPrompt }], model);
|
|
179
197
|
this.logger.debug(`Recovery response content: ${fixResponse.content}`);
|
|
180
198
|
const fixedJson = this.extractJsonFromResponse(fixResponse.content);
|