viscribe 1.0.5 → 1.1.0

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.cjs CHANGED
@@ -45,12 +45,8 @@ module.exports = __toCommonJS(index_exports);
45
45
  // src/images.ts
46
46
  var images_exports = {};
47
47
  __export(images_exports, {
48
- ask: () => ask,
49
48
  assertImagePathExists: () => assertImagePathExists,
50
49
  buildImageSource: () => buildImageSource,
51
- classify: () => classify,
52
- compare: () => compare,
53
- describe: () => describe,
54
50
  extract: () => extract
55
51
  });
56
52
 
@@ -153,67 +149,13 @@ CRITICAL INSTRUCTIONS:
153
149
  - Do not include explanatory text, code blocks, or markdown formatting.
154
150
  - Your response must be a single JSON object that exactly matches the provided schema.
155
151
 
156
- TASK:
152
+ GOAL:
157
153
  - Analyze the image carefully and extract data according to the provided schema.
158
154
  - Be precise and accurate.
159
155
  - If a value is not visible or cannot be inferred from the image, use null
160
156
  when the schema allows it.
161
157
  - Ensure all required fields are included.
162
158
  `.trim();
163
- var DESCRIBE_SYSTEM_PROMPT = `
164
- You are an expert image analyst.
165
-
166
- CRITICAL INSTRUCTIONS:
167
- - Respond with valid JSON only.
168
- - Do not include explanatory text, code blocks, or markdown formatting.
169
- - Your response must be a single JSON object that exactly matches the provided schema.
170
- - Follow any user-provided instruction closely.
171
-
172
- TASK:
173
- - Provide a detailed, objective description of the image.
174
- - Focus on main elements, context, and notable details.
175
- - If tags are requested, generate up to 5 relevant tags.
176
- `.trim();
177
- var CLASSIFY_SYSTEM_PROMPT = `
178
- You are an expert at image classification.
179
-
180
- CRITICAL INSTRUCTIONS:
181
- - Respond with valid JSON only.
182
- - Do not include explanatory text, code blocks, or markdown formatting.
183
- - Your response must be a single JSON object that exactly matches the provided schema.
184
-
185
- TASK:
186
- - Analyze the image and classify it according to the requested categories or task.
187
- - Return only categories that are supported by the image.
188
- - If the image cannot be classified into a requested category, return an empty list.
189
- `.trim();
190
- var ASK_SYSTEM_PROMPT = `
191
- You are an expert at analyzing images and answering related questions.
192
-
193
- CRITICAL INSTRUCTIONS:
194
- - Respond with valid JSON only.
195
- - Do not include explanatory text, code blocks, or markdown formatting.
196
- - Your response must be a single JSON object that exactly matches the provided schema.
197
-
198
- TASK:
199
- - Carefully examine the provided image.
200
- - Deliver a clear, accurate, and detailed response to the question.
201
- - If the answer cannot be determined from the image, state that the information is not available.
202
- `.trim();
203
- var COMPARE_SYSTEM_PROMPT = `
204
- You are an expert at comparing images.
205
-
206
- CRITICAL INSTRUCTIONS:
207
- - Respond with valid JSON only.
208
- - Do not include explanatory text, code blocks, or markdown formatting.
209
- - Your response must be a single JSON object that exactly matches the provided schema.
210
-
211
- TASK:
212
- - Analyze two provided images, focusing on their visual elements.
213
- - Identify and describe similarities.
214
- - Highlight differences in color, texture, composition, subject matter, and context.
215
- - Point out notable features unique to each image.
216
- `.trim();
217
159
 
218
160
  // src/schema.ts
219
161
  function buildSchemaFromFields(fields) {
@@ -290,78 +232,6 @@ var CLIENT_CONFIG_KEYS = /* @__PURE__ */ new Set([
290
232
  "fetch",
291
233
  "httpAgent"
292
234
  ]);
293
- var DESCRIBE_SCHEMA = {
294
- title: "ImageDescription",
295
- description: "Structured description of an image.",
296
- type: "object",
297
- properties: {
298
- image_description: {
299
- type: "string",
300
- description: "Objective description of the image."
301
- },
302
- tags: {
303
- type: "array",
304
- items: { type: "string" },
305
- maxItems: 5,
306
- description: "Relevant tags describing the image."
307
- }
308
- },
309
- required: ["image_description", "tags"],
310
- additionalProperties: false
311
- };
312
- var DESCRIBE_NO_TAGS_SCHEMA = {
313
- title: "ImageDescription",
314
- description: "Structured description of an image.",
315
- type: "object",
316
- properties: {
317
- image_description: {
318
- type: "string",
319
- description: "Objective description of the image."
320
- }
321
- },
322
- required: ["image_description"],
323
- additionalProperties: false
324
- };
325
- var CLASSIFY_SCHEMA = {
326
- title: "ImageClassification",
327
- description: "Structured classification of an image.",
328
- type: "object",
329
- properties: {
330
- classification: {
331
- type: "array",
332
- items: { type: "string" },
333
- description: "Predicted class names."
334
- }
335
- },
336
- required: ["classification"],
337
- additionalProperties: false
338
- };
339
- var ASK_SCHEMA = {
340
- title: "ImageQuestionAnswer",
341
- description: "Answer to a question about an image.",
342
- type: "object",
343
- properties: {
344
- answer: {
345
- type: "string",
346
- description: "Answer to the question based on the image."
347
- }
348
- },
349
- required: ["answer"],
350
- additionalProperties: false
351
- };
352
- var COMPARE_SCHEMA = {
353
- title: "ImageComparison",
354
- description: "Structured comparison of two images.",
355
- type: "object",
356
- properties: {
357
- comparison_result: {
358
- type: "string",
359
- description: "Textual comparison of the two images."
360
- }
361
- },
362
- required: ["comparison_result"],
363
- additionalProperties: false
364
- };
365
235
  var StructuredOutputError = class extends Error {
366
236
  };
367
237
  var StructuredOutputRefusalError = class extends StructuredOutputError {
@@ -388,11 +258,7 @@ var ViscribeAI = class {
388
258
  this.strict = options.strict ?? true;
389
259
  this.client = options.client;
390
260
  this.images = {
391
- extract: (extractOptions) => this.extractImage(extractOptions),
392
- describe: (describeOptions) => this.describeImage(describeOptions),
393
- classify: (classifyOptions) => this.classifyImage(classifyOptions),
394
- ask: (askOptions) => this.askImage(askOptions),
395
- compare: (compareOptions) => this.compareImages(compareOptions)
261
+ extract: (extractOptions) => this.extractImage(extractOptions)
396
262
  };
397
263
  }
398
264
  async extractImage(options) {
@@ -406,45 +272,6 @@ var ViscribeAI = class {
406
272
  schema
407
273
  });
408
274
  }
409
- async describeImage(options) {
410
- return this.structuredImageRequest({
411
- messages: singleImageMessages({
412
- systemPrompt: DESCRIBE_SYSTEM_PROMPT,
413
- userPrompt: options.instruction ?? "Describe this image.",
414
- imageSource: options
415
- }),
416
- schema: options.generateTags ?? true ? DESCRIBE_SCHEMA : DESCRIBE_NO_TAGS_SCHEMA
417
- });
418
- }
419
- async classifyImage(options) {
420
- const classes = validateClassificationOptions(options);
421
- return this.structuredImageRequest({
422
- messages: singleImageMessages({
423
- systemPrompt: CLASSIFY_SYSTEM_PROMPT,
424
- userPrompt: classificationPrompt({ ...options, classes }),
425
- imageSource: options
426
- }),
427
- schema: classificationSchema({ multiLabel: options.multiLabel ?? false }),
428
- transform: (data) => normalizeClassification(data, { classes })
429
- });
430
- }
431
- async askImage(options) {
432
- const question = validateQuestion(options.question);
433
- return this.structuredImageRequest({
434
- messages: singleImageMessages({
435
- systemPrompt: ASK_SYSTEM_PROMPT,
436
- userPrompt: question,
437
- imageSource: options
438
- }),
439
- schema: ASK_SCHEMA
440
- });
441
- }
442
- async compareImages(options) {
443
- return this.structuredImageRequest({
444
- messages: compareMessages(options),
445
- schema: COMPARE_SCHEMA
446
- });
447
- }
448
275
  async structuredImageRequest(options) {
449
276
  const response = await this.getClient().chat.completions.create({
450
277
  messages: options.messages,
@@ -452,10 +279,7 @@ var ViscribeAI = class {
452
279
  response_format: responseFormatForSchema(options.schema, this.strict),
453
280
  ...this.modelConfig.request
454
281
  });
455
- return resultFromResponse(response, {
456
- rawSchema: options.schema,
457
- transform: options.transform
458
- });
282
+ return resultFromResponse(response);
459
283
  }
460
284
  getClient() {
461
285
  if (!this.client) {
@@ -515,114 +339,6 @@ function singleImageMessages(options) {
515
339
  }
516
340
  ];
517
341
  }
518
- function compareMessages(options) {
519
- const image1Source = buildNumberedImageSource("image1", {
520
- imageUrl: options.image1Url,
521
- imageBase64: options.image1Base64,
522
- imagePath: options.image1Path
523
- });
524
- const image2Source = buildNumberedImageSource("image2", {
525
- imageUrl: options.image2Url,
526
- imageBase64: options.image2Base64,
527
- imagePath: options.image2Path
528
- });
529
- return [
530
- { role: "system", content: COMPARE_SYSTEM_PROMPT },
531
- {
532
- role: "user",
533
- content: [
534
- {
535
- type: "text",
536
- text: options.instruction ?? "Describe the similarities and differences between these two images."
537
- },
538
- { type: "image_url", image_url: { url: image1Source } },
539
- { type: "image_url", image_url: { url: image2Source } }
540
- ]
541
- }
542
- ];
543
- }
544
- function buildNumberedImageSource(label, input) {
545
- const provided = [
546
- input.imageUrl !== void 0,
547
- input.imageBase64 !== void 0,
548
- input.imagePath !== void 0
549
- ].filter(Boolean).length;
550
- if (provided !== 1) {
551
- throw new Error(
552
- `Provide exactly one source for '${label}': '${label}Url', '${label}Base64', or '${label}Path'.`
553
- );
554
- }
555
- return buildImageSource(input);
556
- }
557
- function classificationSchema(options) {
558
- const schema = structuredClone(CLASSIFY_SCHEMA);
559
- const properties = schema.properties;
560
- if (!options.multiLabel && isPlainObject(properties)) {
561
- const classification = properties.classification;
562
- if (isPlainObject(classification)) {
563
- classification.maxItems = 1;
564
- }
565
- }
566
- return schema;
567
- }
568
- function classificationPrompt(options) {
569
- const parts = [options.instruction ?? "Classify this image."];
570
- if (options.classes && options.classes.length > 0) {
571
- parts.push(`Allowed classes: ${options.classes.join(", ")}.`);
572
- } else if (options.classes) {
573
- parts.push("No allowed classes were provided; return an empty list.");
574
- }
575
- if (options.classDescriptions) {
576
- const descriptions = Object.entries(options.classDescriptions).map(([className, description]) => `- ${className}: ${description}`).join("\n");
577
- parts.push(`Class descriptions:
578
- ${descriptions}`);
579
- }
580
- parts.push(
581
- options.multiLabel ? "Return every relevant class in the classification list." : "Return at most one class in the classification list."
582
- );
583
- return parts.join("\n");
584
- }
585
- function validateClassificationOptions(options) {
586
- const classes = options.classes ? [...options.classes] : void 0;
587
- if (options.classDescriptions) {
588
- if (!classes || classes.length === 0) {
589
- throw new Error(
590
- "'classDescriptions' can only be provided when 'classes' is provided."
591
- );
592
- }
593
- const invalidClasses = Object.keys(options.classDescriptions).filter(
594
- (className) => !classes.includes(className)
595
- );
596
- if (invalidClasses.length > 0) {
597
- throw new Error(
598
- `'classDescriptions' contains entries not present in 'classes': ${invalidClasses.sort().join(", ")}.`
599
- );
600
- }
601
- }
602
- return classes;
603
- }
604
- function normalizeClassification(data, options) {
605
- const rawClassification = data.classification;
606
- let classification;
607
- if (typeof rawClassification === "string") {
608
- classification = rawClassification ? [rawClassification] : [];
609
- } else if (Array.isArray(rawClassification)) {
610
- classification = rawClassification.filter((value) => typeof value === "string").map((value) => value);
611
- } else {
612
- classification = [];
613
- }
614
- if (options.classes) {
615
- const allowedClasses = new Set(options.classes);
616
- classification = classification.filter((className) => allowedClasses.has(className));
617
- }
618
- return { classification };
619
- }
620
- function validateQuestion(question) {
621
- if (typeof question !== "string" || question.trim().length === 0) {
622
- throw new Error("'question' must be provided.");
623
- }
624
- return question.trim();
625
- }
626
342
  function responseFormatForSchema(rawSchema, strict) {
627
343
  if (rawSchema.type === "json_schema" && typeof rawSchema.json_schema === "object" && rawSchema.json_schema !== null) {
628
344
  const jsonSchema = { ...rawSchema.json_schema };
@@ -682,10 +398,10 @@ function ensureStrictJsonSchema(schema) {
682
398
  delete schema.default;
683
399
  }
684
400
  }
685
- function resultFromResponse(response, context) {
401
+ function resultFromResponse(response) {
686
402
  const parsed = parseStructuredResponse(response);
687
403
  return {
688
- data: context.transform ? context.transform(parsed) : parsed,
404
+ data: parsed,
689
405
  raw: response,
690
406
  usageMetadata: usageMetadata(response)
691
407
  };
@@ -786,22 +502,6 @@ async function extract(options) {
786
502
  const client = new ViscribeAI(options);
787
503
  return client.images.extract(options);
788
504
  }
789
- async function describe(options) {
790
- const client = new ViscribeAI(options);
791
- return client.images.describe(options);
792
- }
793
- async function classify(options) {
794
- const client = new ViscribeAI(options);
795
- return client.images.classify(options);
796
- }
797
- async function ask(options) {
798
- const client = new ViscribeAI(options);
799
- return client.images.ask(options);
800
- }
801
- async function compare(options) {
802
- const client = new ViscribeAI(options);
803
- return client.images.compare(options);
804
- }
805
505
  // Annotate the CommonJS export names for ESM import in node:
806
506
  0 && (module.exports = {
807
507
  DEFAULT_MODEL,
package/dist/index.d.cts CHANGED
@@ -25,45 +25,10 @@ interface ImageResult<T = Record<string, unknown>> {
25
25
  interface ExtractResult<T = Record<string, unknown>> extends ImageResult<T> {
26
26
  }
27
27
  type OutputSchema = ExtractField[] | JsonSchema;
28
- interface DescribeData {
29
- image_description: string;
30
- tags?: string[];
31
- }
32
- interface ClassificationData {
33
- classification: string[];
34
- }
35
- interface AskData {
36
- answer: string;
37
- }
38
- interface CompareData {
39
- comparison_result: string;
40
- }
41
28
  interface ExtractOptions extends ImageSourceInput {
42
29
  outputSchema?: OutputSchema;
43
30
  instruction?: string;
44
31
  }
45
- interface DescribeOptions extends ImageSourceInput {
46
- instruction?: string;
47
- generateTags?: boolean;
48
- }
49
- interface ClassifyOptions extends ImageSourceInput {
50
- classes?: string[];
51
- classDescriptions?: Record<string, string>;
52
- instruction?: string;
53
- multiLabel?: boolean;
54
- }
55
- interface AskOptions extends ImageSourceInput {
56
- question: string;
57
- }
58
- interface CompareOptions {
59
- image1Url?: string;
60
- image1Base64?: string;
61
- image1Path?: string;
62
- image2Url?: string;
63
- image2Base64?: string;
64
- image2Path?: string;
65
- instruction?: string;
66
- }
67
32
  interface ViscribeAIOptions {
68
33
  modelConfig?: ModelConfig;
69
34
  apiKey?: string;
@@ -73,10 +38,6 @@ interface ViscribeAIOptions {
73
38
  }
74
39
  interface ImagesClient {
75
40
  extract<T = Record<string, unknown>>(options: ExtractOptions): Promise<ExtractResult<T>>;
76
- describe(options: DescribeOptions): Promise<ImageResult<DescribeData>>;
77
- classify(options: ClassifyOptions): Promise<ImageResult<ClassificationData>>;
78
- ask(options: AskOptions): Promise<ImageResult<AskData>>;
79
- compare(options: CompareOptions): Promise<ImageResult<CompareData>>;
80
41
  }
81
42
  type OpenAILike = {
82
43
  chat: {
@@ -106,30 +67,18 @@ declare class ViscribeAI {
106
67
  private client?;
107
68
  constructor(options?: ViscribeAIOptions);
108
69
  private extractImage;
109
- private describeImage;
110
- private classifyImage;
111
- private askImage;
112
- private compareImages;
113
70
  private structuredImageRequest;
114
71
  private getClient;
115
72
  }
116
73
 
117
74
  declare function extract<T = Record<string, unknown>>(options: ExtractOptions & ViscribeAIOptions): Promise<ExtractResult<T>>;
118
- declare function describe(options: DescribeOptions & ViscribeAIOptions): Promise<ImageResult<DescribeData>>;
119
- declare function classify(options: ClassifyOptions & ViscribeAIOptions): Promise<ImageResult<ClassificationData>>;
120
- declare function ask(options: AskOptions & ViscribeAIOptions): Promise<ImageResult<AskData>>;
121
- declare function compare(options: CompareOptions & ViscribeAIOptions): Promise<ImageResult<CompareData>>;
122
75
 
123
76
  type images_ImageSourceInput = ImageSourceInput;
124
- declare const images_ask: typeof ask;
125
77
  declare const images_assertImagePathExists: typeof assertImagePathExists;
126
78
  declare const images_buildImageSource: typeof buildImageSource;
127
- declare const images_classify: typeof classify;
128
- declare const images_compare: typeof compare;
129
- declare const images_describe: typeof describe;
130
79
  declare const images_extract: typeof extract;
131
80
  declare namespace images {
132
- export { type images_ImageSourceInput as ImageSourceInput, images_ask as ask, images_assertImagePathExists as assertImagePathExists, images_buildImageSource as buildImageSource, images_classify as classify, images_compare as compare, images_describe as describe, images_extract as extract };
81
+ export { type images_ImageSourceInput as ImageSourceInput, images_assertImagePathExists as assertImagePathExists, images_buildImageSource as buildImageSource, images_extract as extract };
133
82
  }
134
83
 
135
- export { type AskData, type AskOptions, type ClassificationData, type ClassifyOptions, type CompareData, type CompareOptions, DEFAULT_MODEL, type DescribeData, type DescribeOptions, type ExtractField, type ExtractOptions, type ExtractResult, type FieldType, type ImageResult, type ImagesClient, type JsonSchema, type ModelConfig, type OpenAILike, type OutputSchema, StructuredOutputError, StructuredOutputFinishReasonError, StructuredOutputParseError, StructuredOutputRefusalError, StructuredOutputValidationError, ViscribeAI, type ViscribeAIOptions, buildSchemaFromFields, images };
84
+ export { DEFAULT_MODEL, type ExtractField, type ExtractOptions, type ExtractResult, type FieldType, type ImageResult, type ImagesClient, type JsonSchema, type ModelConfig, type OpenAILike, type OutputSchema, StructuredOutputError, StructuredOutputFinishReasonError, StructuredOutputParseError, StructuredOutputRefusalError, StructuredOutputValidationError, ViscribeAI, type ViscribeAIOptions, buildSchemaFromFields, images };
package/dist/index.d.ts CHANGED
@@ -25,45 +25,10 @@ interface ImageResult<T = Record<string, unknown>> {
25
25
  interface ExtractResult<T = Record<string, unknown>> extends ImageResult<T> {
26
26
  }
27
27
  type OutputSchema = ExtractField[] | JsonSchema;
28
- interface DescribeData {
29
- image_description: string;
30
- tags?: string[];
31
- }
32
- interface ClassificationData {
33
- classification: string[];
34
- }
35
- interface AskData {
36
- answer: string;
37
- }
38
- interface CompareData {
39
- comparison_result: string;
40
- }
41
28
  interface ExtractOptions extends ImageSourceInput {
42
29
  outputSchema?: OutputSchema;
43
30
  instruction?: string;
44
31
  }
45
- interface DescribeOptions extends ImageSourceInput {
46
- instruction?: string;
47
- generateTags?: boolean;
48
- }
49
- interface ClassifyOptions extends ImageSourceInput {
50
- classes?: string[];
51
- classDescriptions?: Record<string, string>;
52
- instruction?: string;
53
- multiLabel?: boolean;
54
- }
55
- interface AskOptions extends ImageSourceInput {
56
- question: string;
57
- }
58
- interface CompareOptions {
59
- image1Url?: string;
60
- image1Base64?: string;
61
- image1Path?: string;
62
- image2Url?: string;
63
- image2Base64?: string;
64
- image2Path?: string;
65
- instruction?: string;
66
- }
67
32
  interface ViscribeAIOptions {
68
33
  modelConfig?: ModelConfig;
69
34
  apiKey?: string;
@@ -73,10 +38,6 @@ interface ViscribeAIOptions {
73
38
  }
74
39
  interface ImagesClient {
75
40
  extract<T = Record<string, unknown>>(options: ExtractOptions): Promise<ExtractResult<T>>;
76
- describe(options: DescribeOptions): Promise<ImageResult<DescribeData>>;
77
- classify(options: ClassifyOptions): Promise<ImageResult<ClassificationData>>;
78
- ask(options: AskOptions): Promise<ImageResult<AskData>>;
79
- compare(options: CompareOptions): Promise<ImageResult<CompareData>>;
80
41
  }
81
42
  type OpenAILike = {
82
43
  chat: {
@@ -106,30 +67,18 @@ declare class ViscribeAI {
106
67
  private client?;
107
68
  constructor(options?: ViscribeAIOptions);
108
69
  private extractImage;
109
- private describeImage;
110
- private classifyImage;
111
- private askImage;
112
- private compareImages;
113
70
  private structuredImageRequest;
114
71
  private getClient;
115
72
  }
116
73
 
117
74
  declare function extract<T = Record<string, unknown>>(options: ExtractOptions & ViscribeAIOptions): Promise<ExtractResult<T>>;
118
- declare function describe(options: DescribeOptions & ViscribeAIOptions): Promise<ImageResult<DescribeData>>;
119
- declare function classify(options: ClassifyOptions & ViscribeAIOptions): Promise<ImageResult<ClassificationData>>;
120
- declare function ask(options: AskOptions & ViscribeAIOptions): Promise<ImageResult<AskData>>;
121
- declare function compare(options: CompareOptions & ViscribeAIOptions): Promise<ImageResult<CompareData>>;
122
75
 
123
76
  type images_ImageSourceInput = ImageSourceInput;
124
- declare const images_ask: typeof ask;
125
77
  declare const images_assertImagePathExists: typeof assertImagePathExists;
126
78
  declare const images_buildImageSource: typeof buildImageSource;
127
- declare const images_classify: typeof classify;
128
- declare const images_compare: typeof compare;
129
- declare const images_describe: typeof describe;
130
79
  declare const images_extract: typeof extract;
131
80
  declare namespace images {
132
- export { type images_ImageSourceInput as ImageSourceInput, images_ask as ask, images_assertImagePathExists as assertImagePathExists, images_buildImageSource as buildImageSource, images_classify as classify, images_compare as compare, images_describe as describe, images_extract as extract };
81
+ export { type images_ImageSourceInput as ImageSourceInput, images_assertImagePathExists as assertImagePathExists, images_buildImageSource as buildImageSource, images_extract as extract };
133
82
  }
134
83
 
135
- export { type AskData, type AskOptions, type ClassificationData, type ClassifyOptions, type CompareData, type CompareOptions, DEFAULT_MODEL, type DescribeData, type DescribeOptions, type ExtractField, type ExtractOptions, type ExtractResult, type FieldType, type ImageResult, type ImagesClient, type JsonSchema, type ModelConfig, type OpenAILike, type OutputSchema, StructuredOutputError, StructuredOutputFinishReasonError, StructuredOutputParseError, StructuredOutputRefusalError, StructuredOutputValidationError, ViscribeAI, type ViscribeAIOptions, buildSchemaFromFields, images };
84
+ export { DEFAULT_MODEL, type ExtractField, type ExtractOptions, type ExtractResult, type FieldType, type ImageResult, type ImagesClient, type JsonSchema, type ModelConfig, type OpenAILike, type OutputSchema, StructuredOutputError, StructuredOutputFinishReasonError, StructuredOutputParseError, StructuredOutputRefusalError, StructuredOutputValidationError, ViscribeAI, type ViscribeAIOptions, buildSchemaFromFields, images };