@tstdl/base 0.92.2 → 0.92.3
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/ai/ai.service.d.ts +4 -4
- package/ai/ai.service.js +5 -4
- package/package.json +1 -1
package/ai/ai.service.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import '../polyfills.js';
|
|
|
2
2
|
import { GoogleGenerativeAI } from '@google/generative-ai';
|
|
3
3
|
import { type FileMetadataResponse, GoogleAIFileManager } from '@google/generative-ai/server';
|
|
4
4
|
import { Resolvable, type resolveArgumentType } from '../injector/interfaces.js';
|
|
5
|
-
import { SchemaTestable } from '../schema/index.js';
|
|
5
|
+
import { OneOrMany, SchemaTestable } from '../schema/index.js';
|
|
6
6
|
import { Enumeration as EnumerationType, EnumerationValue } from '../types.js';
|
|
7
7
|
import { LiteralUnion } from 'type-fest';
|
|
8
8
|
export type FileInput = {
|
|
@@ -22,15 +22,15 @@ export declare class AiService implements Resolvable<AiServiceArgument> {
|
|
|
22
22
|
readonly model: import("@google/generative-ai").GenerativeModel;
|
|
23
23
|
readonly [resolveArgumentType]: AiServiceArgument;
|
|
24
24
|
getFile(fileInput: FileInput): Promise<FileMetadataResponse>;
|
|
25
|
-
getFiles(files: FileInput[]): Promise<FileMetadataResponse[]>;
|
|
26
|
-
classify<T extends EnumerationType>(fileInput: FileInput
|
|
25
|
+
getFiles(files: readonly FileInput[]): Promise<FileMetadataResponse[]>;
|
|
26
|
+
classify<T extends EnumerationType>(fileInput: OneOrMany<FileInput>, types: T): Promise<{
|
|
27
27
|
reasoning: string;
|
|
28
28
|
types: {
|
|
29
29
|
type: EnumerationValue<T>;
|
|
30
30
|
confidence: 'high' | 'medium' | 'low';
|
|
31
31
|
}[] | null;
|
|
32
32
|
}>;
|
|
33
|
-
extractData<T>(fileInput: FileInput
|
|
33
|
+
extractData<T>(fileInput: OneOrMany<FileInput>, schema: SchemaTestable<T>): Promise<T>;
|
|
34
34
|
waitForFileActive(fileMetadata: FileMetadataResponse): Promise<FileMetadataResponse>;
|
|
35
35
|
waitForFilesActive(...files: FileMetadataResponse[]): Promise<FileMetadataResponse[]>;
|
|
36
36
|
}
|
package/ai/ai.service.js
CHANGED
|
@@ -68,6 +68,7 @@ import { Singleton } from '../injector/decorators.js';
|
|
|
68
68
|
import { injectArgument } from '../injector/inject.js';
|
|
69
69
|
import { convertToOpenApiSchema } from '../schema/converters/openapi-converter.js';
|
|
70
70
|
import { array, enumeration, nullable, object, Schema, string } from '../schema/index.js';
|
|
71
|
+
import { toArray } from '../utils/array/array.js';
|
|
71
72
|
import { digest } from '../utils/cryptography.js';
|
|
72
73
|
import { timeout } from '../utils/timing.js';
|
|
73
74
|
import { tryIgnoreAsync } from '../utils/try-ignore.js';
|
|
@@ -130,7 +131,7 @@ let AiService = class AiService {
|
|
|
130
131
|
return Promise.all(files.map(async (file) => this.getFile(file)));
|
|
131
132
|
}
|
|
132
133
|
async classify(fileInput, types) {
|
|
133
|
-
const
|
|
134
|
+
const files = await this.getFiles(toArray(fileInput));
|
|
134
135
|
const resultSchema = object({
|
|
135
136
|
reasoning: string({ description: 'Reasoning for classification. Use to be more confident, if unsure. Reason for every somewhat likely document type.' }),
|
|
136
137
|
types: nullable(array(object({
|
|
@@ -151,7 +152,7 @@ let AiService = class AiService {
|
|
|
151
152
|
{
|
|
152
153
|
role: 'user',
|
|
153
154
|
parts: [
|
|
154
|
-
{ fileData: { mimeType: file.mimeType, fileUri: file.uri } },
|
|
155
|
+
...files.map((file) => ({ fileData: { mimeType: file.mimeType, fileUri: file.uri } })),
|
|
155
156
|
{ text: `Classify the document. Output as JSON using the following schema:\n${JSON.stringify(responseSchema, null, 2)}\n\nIf none of the provided document types are a suitable match, return null for types.` }
|
|
156
157
|
]
|
|
157
158
|
}
|
|
@@ -160,7 +161,7 @@ let AiService = class AiService {
|
|
|
160
161
|
return resultSchema.parse(JSON.parse(result.response.text()));
|
|
161
162
|
}
|
|
162
163
|
async extractData(fileInput, schema) {
|
|
163
|
-
const
|
|
164
|
+
const files = await this.getFiles(toArray(fileInput));
|
|
164
165
|
const responseSchema = convertToOpenApiSchema(schema);
|
|
165
166
|
const result = await this.model.generateContent({
|
|
166
167
|
generationConfig: {
|
|
@@ -182,7 +183,7 @@ You *MUST* output the reasoning first.`,
|
|
|
182
183
|
{
|
|
183
184
|
role: 'user',
|
|
184
185
|
parts: [
|
|
185
|
-
{ fileData: { mimeType: file.mimeType, fileUri: file.uri } },
|
|
186
|
+
...files.map((file) => ({ fileData: { mimeType: file.mimeType, fileUri: file.uri } })),
|
|
186
187
|
{ text: `Classify the document. Output as JSON using the following schema:\n${JSON.stringify(responseSchema, null, 2)}` }
|
|
187
188
|
]
|
|
188
189
|
}
|